-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
111 lines (84 loc) · 3.13 KB
/
generate.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
from twython import Twython
import os
import feedparser
import re
import urllib, cStringIO
from PIL import Image, ImageFont, ImageDraw
import string
import dateutil
from dateutil import parser, tz
from time import strftime
import pytz
import random
import warnings
warnings.filterwarnings("ignore")
twitter = Twython(os.environ['CONSUMER_KEY'], os.environ['CONSUMER_SECRET'],
os.environ['ACCESS_TOKEN'], os.environ['ACCESS_TOKEN_SECRET'])
sites = [["http://www.theverge.com/rss/index.xml", "theverge_template.png", "@verge"],
["http://www.vox.com/rss/index.xml", "vox_template.png", "@voxdotcom"],
["http://www.eater.com/rss/index.xml", "eater_template.png", "@eater"],
["http://www.racked.com/rss/index.xml", "racked_template.png", "@racked"],
["http://www.curbed.com/rss/index.xml", "curbed_template.png", "@curbed"],
["http://www.polygon.com/rss/index.xml", "polygon_template.png", "@polygon"],
["http://www.sbnation.com/rss/index.xml", "sbnation_template.png", "@sbnation"]]
rss, template_file, handle = random.choice(sites)
feed = feedparser.parse(rss)
headline = ""
author = ""
image = ""
url = ""
publish_time = None
for entry in feed.entries:
m = re.search('^<img[^>]+ src="([^"]+)"', entry.content[0]['value'])
if len(entry.title) < 78 and m and m.group(1):
image = m.group(1)
headline = entry.title
author = entry.author
url = entry.link
publish_time = parser.parse(entry.published)
break
local_tz = pytz.timezone('US/Eastern')
publish_time = publish_time.astimezone(local_tz)
# print publish_time
# print headline
lines = []
headlines = string.split(headline)
workline = ""
for word in headlines:
if len(workline + word) > 30:
lines.append(workline)
workline = ""
workline = workline + word + " "
if workline:
lines.append(workline)
broken_headline = "\n".join(lines)
# print author
# print image
base = Image.open(template_file)
base = base.convert("1")
imagefile = cStringIO.StringIO(urllib.urlopen(image).read())
img = Image.open(imagefile)
img.thumbnail([250,388])
bwimg = img.convert("1")
base.paste(bwimg,(20,65))
time_image = Image.new("1", (400,15), (1))
time_font = ImageFont.truetype("resources/chicagobold.ttf", 12)
timeline = ImageDraw.Draw(time_image)
timeline = timeline.text((0,0), publish_time.strftime('Published on %A, %B %d, %Y at %-I:%M %p %Z'), font=time_font, fill=(0))
base.paste(time_image,(20,45))
title_image = Image.new("1", (388,60), (1))
title_font = ImageFont.truetype("resources/chicagobold.ttf", 20)
title_line = ImageDraw.Draw(title_image)
title_line = title_line.multiline_text((0,0), broken_headline, font=title_font, fill=(0))
base.paste(title_image,(20,238))
author_image = Image.new("1", (300,12), (1))
author_font = ImageFont.truetype("resources/chicagobold.ttf", 10)
user_byline = ImageDraw.Draw(author_image)
user_byline = user_byline.text((0,0), "By "+author, font=author_font, fill=(0))
base.paste(author_image,(20,300))
base = base.resize((1024,684), Image.ANTIALIAS)
base.save("output.gif")
photo = open('output.gif', 'rb')
response = twitter.upload_media(media=photo)
twitter.update_status(status=headline+" - "+handle+"\n"+url, media_ids=[response['media_id']])