Created
March 30, 2017 10:59
-
-
Save rennerocha/a57c702e9f4cb43e0eddee25e94dba6f to your computer and use it in GitHub Desktop.
Return name of today's free ebook of Packtpub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Other test