Skip to content

Instantly share code, notes, and snippets.

@rennerocha
Created March 30, 2017 10:39
Show Gist options
  • Save rennerocha/b68bddd9162722cbadaffda3064cf891 to your computer and use it in GitHub Desktop.
Save rennerocha/b68bddd9162722cbadaffda3064cf891 to your computer and use it in GitHub Desktop.
Check today's free ebook from Packpub
import datetime
import os
import re
import socket
import Tkinter as Tk
import urllib2
import webbrowser
FREE_EBOOK_URL = 'https://www.packtpub.com/packt/offers/free-learning'
def open_url():
webbrowser.open_new(FREE_EBOOK_URL)
timeout = 2
socket.setdefaulttimeout(timeout)
today = datetime.datetime.today()
formatted_today = today.strftime('%d-%m-%Y')
last_date = ''
if os.path.exists('/home/rocha/last_ebook'):
with open('/home/rocha/last_ebook', 'r') as last_file:
last_ebook = last_file.read()
last_date, last_title = last_ebook.split('|')
if formatted_today != last_date:
try:
request = urllib2.Request(FREE_EBOOK_URL)
response = urllib2.urlopen(request)
except:
pass
else:
html = response.read()
html = html.replace('\n', '')
html = html.replace('\t', '')
book_title_regex = '<div class="dotd-title"><h2>(.+?)</h2></div>'
pattern = re.compile(book_title_regex)
title = re.findall(pattern, html)
if title:
today_title = title.pop(0)
message = 'Free e-book for {0}: {1}'.format(formatted_today, today_title)
with open('/home/rocha/last_ebook', 'w') as last_file:
last_file.write('|'.join([formatted_today, today_title]))
else:
message = 'Unable to identify {0} free e-book'.format(formatted_today)
root = Tk.Tk()
root.after(5000, lambda: root.destroy())
label = Tk.Label(root, text=message, font=('Helvetica', 20))
label.pack()
button = Tk.Button(root, text="ABRIR", command=open_url)
button.pack()
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment