-
Notifications
You must be signed in to change notification settings - Fork 2
/
botd.py
239 lines (198 loc) · 8.01 KB
/
botd.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from PIL import Image, ImageDraw, ImageFont
import urllib2
from bs4 import BeautifulSoup
from bs4 import BeautifulStoneSoup
import random, re
import twitter
attempts = 0
raw_films = open("films.txt", "r").readlines()
films = []
for film in raw_films:
if "(" in film:
films.append(film[:film.index('(')-1].lower())
else:
films.append(film.lower())
# raw_songs = open("songs", "r").readlines()
# songs = []
# for song in raw_songs:
# # print song
# song = song[song.index(".")+1:song.index(" - ")].lower().strip()
# songs.append(song)
def checkwidth(font, lines, lim):
for line in lines:
if(font.getsize(line)[0] >= lim):
return False
return True
def generate():
raw_idioms = open("idioms.txt", "r").readlines()
idioms = []
for idiom in raw_idioms:
# print song
idioms.append(idiom.strip())
data = idioms + films# + songs
ingredients = ["tomato", "bacon", "pepper", "pea", "peas", "chevre", "fig", "radish", "leek", "olive", "curry", "shallot", "onion", "kale", "fennel", "poutine", "lentil", "egg", "carrot", "cheddar", "thyme",
"feta", "tuna", "corn", "honey", "blue", "chorizo", "rye", "sour", "swiss", "palm", "beet", "bean", "slaw", "cumin", "mint", "brie", "polenta",
"chilli", "yam", "bun", "barley", ]
ingredient = random.choice(ingredients)
ingredients_dict = {
"bacon" : ["streaky bacon", "bacon rashers", "bacon"],
"peas" : ["steamed peas", "peas", "snap peas", "snow peas"],
"pea" : ["steamed peas", "peas", "snap peas", "snow peas"],
"pepper" : ["red pepper", "sliced pepper", "peppers"],
"chevre" : ["chevre cheese", "goat's cheese"],
"fig" : ["a side of figs", "roasted fig"],
"radish" : ["sliced radish", "radishes"],
"leek" : ["braised leek", "steamed leeks"],
"olive" : ["stuffed olives", "spanish olives", "olives"],
"curry" : ["curry sauce"],
"shallot" : ["shallots", "steamed shallots"],
"onion" : ["red onion", "onion rings", "fried onions"],
"kale" : ["kale", "crispy kale", "kale chips"],
"fennel" : ["fennel"],
"poutine" : ["a side of poutine"],
"lentil" : ["green lentils"],
"egg" : ["fried egg", "poached egg", "scrambled egg"],
"carrot" : ["shredded carrot", "grated carrot"],
"cheddar" : ["aged cheddar", "melted cheddar", "slices of cheddar"],
"thyme" : ["thyme"],
"feta" : ["feta cheese"],
"tuna" : ["tuna steak"],
"corn" : ["corn salsa"],
"honey" : ["honey mustard", "a honey glaze"],
"blue" : ["blue cheese"],
"chorizo" : ["chorizo"],
"rye" : ["a rye bun"],
"sour" : ["a sourdough bun", "sour cream"],
"swiss" : ["swiss cheese"],
"palm" : ["hearts of palm"],
"beet" : ["beets"],
"bean" : ["beans", "refried beans", "pinto beans"],
"slaw" : ["coleslaw"],
"cumin" : ["cumin"],
"mint" : ["mint relish"],
"brie" : ["brie"],
"polenta" : ["polenta"],
"chilli" : ["chilli relish", "chillies"],
"yam" : ["yams"],
"bun" : ["a fancy bun"],
"barley" : ["a barley roll"],
"tomato" : ["cherry tomatoes", "a grilled tomato"],
}
#let's get a rhyme set
hdr = { 'User-Agent' : 'grauniadconlums' }
req = urllib2.Request("http://www.rhymezone.com/r/rhyme.cgi?Word="+ingredient+"&typeofrhyme=perfect&org1=syl&org2=l&org3=y", headers=hdr)
try:
response = urllib2.urlopen(req)
except urllib2.HTTPError:
exit()
html = response.read()
FromRaw = lambda r: r if isinstance(r, unicode) else r.decode('utf-8', 'ignore')
html = FromRaw(html)
rhymeset = []
#banlist but also words that have iffy rhymes that we want to avoid
bannedwords = ["our", "hamp", "rom"]
doc = BeautifulSoup(html, features="xml")
for lnk in doc.findAll("a"):
if lnk.parent.name == "b" and ">" in lnk.text:
# print lnk.text
# print lnk.text[lnk.text.index(">")+1:]
if (len(lnk.text[lnk.text.index(">")+1:].lower()) > 2) and (not lnk.text[lnk.text.index(">")+1:].lower() in bannedwords):
rhymeset.append(lnk.text[lnk.text.index(">")+1:].lower())
poss = []
meaning = []
# print rhymeset
for film in data:
for rhyme in rhymeset:
if rhyme == film:
continue
# if rhyme in film and len(film.replace(rhyme, ingredient)) < 60:
# poss.append(film.replace(rhyme, ingredient))
# meaning.append(film)
if re.search(r"\b" + re.escape(rhyme) + r"\b", film): # + r"\b"
poss.append(film.replace(rhyme, ingredient))
meaning.append(film)
if re.search(r"\b" + re.escape(rhyme) + r"[a-zA-Z]", film): # + r"\b"
poss.append(film.replace(rhyme, ingredient+"-"))
meaning.append(film)
if re.search(r"[a-zA-Z]" + re.escape(rhyme) + r"\b", film):
poss.append(film.replace(rhyme, "-"+ingredient))
meaning.append(film)
# print meaning
# print poss
if len(poss) == 0:
print "failed to find for "+ingredient
generate()
return
W, H = (1100,704)
commentimg = Image.open("bobs_base.png")
s1 = [random.choice(poss)+" burger"]
s1[0] = s1[0].lower()
s2 = "("+random.choice(["comes", "served"])+" with "+random.choice(ingredients_dict[ingredient])+")"
#Let's check if we've generated this before
existing = open("existing.txt", "ab+")
already_done = existing.readlines()
if s1[0] in already_done:
existing.close()
attempts += 1
if(attempts > 200):
#We need a bigger corpus
print "Help! I might be exhausting my corpus."
exit()
generate()
return
else:
existing.write(s1[0]+"\n")
existing.close()
import textwrap
if(len(s1[0]) > 35):
s1 = textwrap.wrap(s1[0], width=len(s1[0])/3 + 5)
else:
s1 = textwrap.wrap(s1[0], width=len(s1[0])/2 + 5)
# if len(s1[0]) > 40:
# print "40+"
# s1 = textwrap.wrap(s1[0], width=len(s1[0])/3 + 5)
# elif len(s1[0]) > 20:
# print "20+"
# s1 = textwrap.wrap(s1[0], width=20)
fontsize = 16
fnt1 = ImageFont.truetype("chalk.ttf", fontsize)
while checkwidth(fnt1, s1, 360):
# iterate until the text size is just larger than the criteria
fontsize += 1
fnt1 = ImageFont.truetype("chalk.ttf", fontsize)
fontsize = 16
fnt2 = ImageFont.truetype("chalk.ttf", fontsize)
while fnt2.getsize(s2)[0] < 360:
# iterate until the text size is just larger than the criteria
fontsize += 1
fnt2 = ImageFont.truetype("chalk.ttf", fontsize)
d = ImageDraw.Draw(commentimg)
chalk_color = (213, 220, 223, 255)
line = 0
for s in s1:
delta = (400-fnt1.getsize(s)[0])/2
d.text((360+delta,260+(fnt1.getsize(s)[1]*line)), s, font=fnt1, fill=chalk_color)
line = line+1
delta = (400-fnt2.getsize(s2)[0])/2
d.text((360+delta, 470), s2, font=fnt2, fill=chalk_color)
#sometimes add an extra image in
print random.randint(0,3)
if(random.randint(0, 2) == 1):
imgs = [('char1.png', (25,200)), ('char2.png',(20,320)), ('char3.png',(15,360)), ('char4.png',(10,250)), ('char5.png', (10, 400)), ('char6.png', (10, 300)), ('char7.png', (-180, 405))]
sel = random.choice(imgs)
img = Image.open(sel[0], 'r')
if(sel[0] == 'char7.png'):
commentimg.paste(img, sel[1], img)
elif(sel[0] == 'char6.png' or random.randint(0,1) == 0):
commentimg.paste(img, (750-sel[1][0], sel[1][1]), img)
else:
commentimg.paste(img, sel[1], img)
commentimg.show()
commentimg.save("out.png");
return
api = twitter.Api(
# stuff goes here
# thanks to @v21 there is no longer my twitter api keys here...
);
api.PostMedia("","out.png")
generate()