Skip to content

Commit

Permalink
017
Browse files Browse the repository at this point in the history
  • Loading branch information
bbelderbos committed Apr 14, 2017
1 parent 32b4831 commit add44c7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions 017/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
recently-added.rss
71 changes: 71 additions & 0 deletions 017/safari.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from datetime import datetime, timedelta
import os
import socket
import ssl
import sys
from urllib.request import urlopen
from xml.etree.ElementTree import parse

cwd = os.getcwd()
project_root = os.path.dirname(cwd)
sys.path.append(project_root)

from common.twitter_config import tweet_status

GO_BACK = timedelta(days=1)
NOW = datetime.now()
RSS = 'https://www.safaribooksonline.com/feeds/recently-added.rss'
LOCAL = 'MacBook' in socket.gethostname()
TWEET = 'New on @safari: {} - {}'


def get_tweets(greps=['Python']):

doc = get_rss_feed()

# Python cookbook 3rd ed
for item in doc.iterfind('channel/item'):
title = item.findtext('title')
date = item.findtext('pubDate')[:-6]
dt = datetime.strptime(date, '%a, %d %b %Y %H:%M:%S')
link = item.findtext('link')
category = item.findtext('category')

if (NOW - dt) > GO_BACK:
continue

if not any(g.lower() in title.lower()
or g.lower() in category.lower()
for g in greps):
continue

title = ' '.join(gen_hashtags(title, greps))
yield TWEET.format(title, link)


def get_rss_feed():
if LOCAL:
with open('recently-added.rss') as f:
return parse(f)
else:
# work around SSL: CERTIFICATE_VERIFY_FAILED
context = ssl._create_unverified_context()
u = urlopen(RSS, context=context)
return parse(u)


def gen_hashtags(title, greps):
for word in title.split():
if any(g.lower() == word.lower() for g in greps):
yield '#' + word
else:
yield word


if __name__ == '__main__':

for tweet in get_tweets():
if LOCAL:
print(tweet)
else:
tweet_status(tweet)
2 changes: 1 addition & 1 deletion LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
| 014 | Apr 12, 2017 | [script to automatically tweet out new @lynda (#Python) titles](014) | feedparser is awesome. Want to run it with filter on Python. Abstracted twitter config away in repo's common dir (re-use). |
| 015 | Apr 13, 2017 | [script to calculate the number of posts on @pybites](015) | small script but some interesting things: urllib.request.urlretrieve (stdlib), test and cache option (using os.start for cache file age), re.findall, dict comprehension. |
| 016 | Apr 14, 2017 | [TITLE](016) | LEARNING |
| 017 | Apr 15, 2017 | [TITLE](017) | LEARNING |
| 017 | Apr 15, 2017 | [script to automatically tweet out new @safari Python titles](017) | like the Lynda one, parsing an RSS feed, but only stdlib, so no feedparser, using xml.etree.ElementTree, no requests, using urllib. Also nice exercise converting and calculating with datetime / timedelta |
| 018 | Apr 16, 2017 | [TITLE](018) | LEARNING |
| 019 | Apr 17, 2017 | [TITLE](019) | LEARNING |
| 020 | Apr 18, 2017 | [TITLE](020) | LEARNING |
Expand Down

0 comments on commit add44c7

Please sign in to comment.