-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathsafari.py
71 lines (54 loc) · 1.75 KB
/
safari.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)