Skip to content

Instantly share code, notes, and snippets.

@rennerocha
Created March 30, 2017 10:59
Show Gist options
  • Save rennerocha/a57c702e9f4cb43e0eddee25e94dba6f to your computer and use it in GitHub Desktop.
Save rennerocha/a57c702e9f4cb43e0eddee25e94dba6f to your computer and use it in GitHub Desktop.
Return name of today's free ebook of Packtpub
import datetime
import re
import urllib2
def today_packtpub_free_ebook():
FREE_EBOOK_URL = 'https://www.packtpub.com/packt/offers/free-learning'
try:
request = urllib2.Request(FREE_EBOOK_URL)
response = urllib2.urlopen(request)
except:
today_title = 'Request to Packpub website failed'
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)
else:
today_title = 'Unable to retrieve ebook name'
return (datetime.date.today(), FREE_EBOOK_URL, today_title)
@rennerocha
Copy link
Author

Teste

@rennerocha
Copy link
Author

Other test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment