This repository has been archived by the owner on Aug 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_utils.py
332 lines (270 loc) · 11 KB
/
app_utils.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import csv
import json
from bs4 import BeautifulSoup
import datetime
from dateutil.parser import *
import requests
import app_config
from models import db, Joke, Episode, EpisodeJoke, JokeConnection
def setup_tables():
"""
Creates the tables for the Arrested Development database.
"""
db.connect()
Joke.create_table()
Episode.create_table()
EpisodeJoke.create_table()
JokeConnection.create_table()
def build_regression_csv():
"""
Builds an episode-based CSV for @stiles to do our regression.
Outputs a list of episodes with joke counts and ratings.
"""
with open('data/regression.csv', 'wb') as csvfile:
regressionwriter = csv.DictWriter(csvfile, ['episode_name', 'episode_id', 'jokes', 'rating'])
regressionwriter.writerow({'episode_name': 'episode_name', 'episode_id': 'episode_id', 'jokes': 'jokes', 'rating': 'rating'})
for episode in Episode.select():
episode_dict = {}
episode_dict['episode_name'] = episode.title.encode('utf-8')
episode_dict['episode_id'] = episode.number
episode_dict['rating'] = episode.rating
episode_dict['jokes'] = EpisodeJoke.select().where(EpisodeJoke.episode == episode).count()
regressionwriter.writerow(episode_dict)
def build_connections():
for episode_joke in EpisodeJoke.select():
if episode_joke.connection:
joke1 = episode_joke.joke
joke2 = Joke.get(Joke.text == episode_joke.connection.strip())
episode = episode_joke.episode
if joke2.code < joke1.code:
tmp = joke2
joke2 = joke1
joke1 = tmp
JokeConnection(joke1=joke1, joke2=joke2, episode=episode).save()
def write_jokes_json():
"""
Writes the JSON for @onyxfish and @alykat's viz.
Produces a list of jokes and within that a list of episodejokes
where the joke appears, sorted by episode index number.
"""
payload = {
'group_order': app_config.PRIMARY_CHARACTER_LIST,
'jokes': {},
'connections': [],
'episodes': {}
}
for joke in Joke.select().order_by(Joke.primary_character):
if joke.primary_character not in payload['group_order']:
joke.primary_character = 'Miscellaneous'
if joke.primary_character not in payload['jokes']:
payload['jokes'][joke.primary_character] = []
joke_dict = joke.__dict__['_data']
del joke_dict['id']
del joke_dict['blurb']
joke_dict['episodejokes'] = []
for ej in EpisodeJoke.select().join(Joke).where(Joke.code == joke.code):
episode_dict = ej.__dict__['_data']
episode_dict['episode_number'] = ej.episode.number
for k in ['episode', 'joke', 'id', 'origin']:
del episode_dict[k]
joke_dict['episodejokes'].append(episode_dict)
joke_dict['episodejokes'] = sorted(joke_dict['episodejokes'], key=lambda ej: ej['episode_number'])
payload['jokes'][joke.primary_character].append(joke_dict)
if app_config.IMPORT_NEW_SEASON is True:
episodes = Episode().select().order_by(Episode.number)
else:
episodes = Episode().select().where(Episode.season != 4).order_by(Episode.number)
for episode in episodes:
episode_dict = episode.__dict__['_data']
episode_dict['run_date'] = episode_dict['run_date'].strftime('%Y-%m-%d')
for k in ['blurb', 'id', 'production_code', 'rating', 'tvdb_image']:
del episode_dict[k]
payload['episodes'][episode.number] = episode_dict
for primary_character, jokes in payload['jokes'].items():
payload['jokes'][primary_character] = sorted(jokes, key=lambda j: j['episodejokes'][0]['episode_number'])
for connection in JokeConnection.select():
payload['connections'].append({
'joke1_code': connection.joke1.code,
'joke2_code': connection.joke2.code,
'episode_number': connection.episode.number
})
payload['connections'] = sorted(payload['connections'], key=lambda c: c['joke1_code'])
with open('www/live-data/jokes.json', 'wb') as jokefile:
jokefile.write(json.dumps(payload))
def parse_tvdb_xml(xmlfile):
FIELDS_LIST = [
('blurb', 'Overview', 'str'),
('run_date', 'FirstAired', 'date'),
('season', 'Combined_season', 'int'),
('production_code', 'ProductionCode', 'str'),
('episode', 'EpisodeNumber', 'int'),
('season', 'Season', 'int')
]
soup = BeautifulSoup(xmlfile, "xml")
for episode in soup.findAll('Episode'):
season = int(episode.find('Combined_season').text)
if season > 0:
episode_dict = {}
for model_field, xml_field, data_type in FIELDS_LIST:
try:
if episode.find(xml_field).text:
episode_dict[model_field] = episode.find(xml_field).text
if data_type == 'int':
episode_dict[model_field] = int(episode_dict[model_field])
if data_type == 'date':
d = parse(episode_dict[model_field])
episode_dict[model_field] = datetime.date(d.year, d.month, d.day)
except AttributeError:
pass
if episode.find('filename').text:
episode_dict['tvdb_image'] = 'http://thetvdb.com/banners/_cache/%s' % episode.find('filename').text
episode_dict['code'] = 's%se%s' % (
str(episode_dict['season']).zfill(2),
str(episode_dict['episode']).zfill(2))
try:
Episode.get(code=episode_dict['code'])
Episode.update(**episode_dict).where(Episode.code == episode_dict['code']).execute()
except Episode.DoesNotExist:
pass
def update_episode_extras():
try:
with open('data/extras.xml', 'rb') as xmlfile:
parse_tvdb_xml(xmlfile)
except IOError:
r = requests.get('http://thetvdb.com/api/983EF67DBCBB1A2D/series/72173/all/en.xml')
with open('data/extras.xml', 'wb') as xmlfile:
xmlfile.write(r.content)
update_episode_extras()
def import_sheet(sheet):
"""
Writes a CSV file from our Arrested Development Google doc sheets.
"""
r = requests.get('https://docs.google.com/spreadsheet/pub?key=0Akb_aqtU8lsGdGhIRXhsT24taTlXNGI4YndnS1c4eEE&single=true&gid=%s&output=csv' % sheet)
with open('data/arrested-%s.csv' % sheet, 'wb') as csv_file:
csv_file.write(r.content)
def parse_sheet(sheet, model):
"""
Parses sheets with the appropriate parser.
"""
with open('data/arrested-%s.csv' % sheet) as csv_file:
if sheet == '1':
_parse_episodes(csv.DictReader(csv_file))
if sheet == '0':
if model == 'jokes':
_parse_jokes(csv.DictReader(csv_file))
if model == 'episodejokes':
_parse_episodejokes(csv.DictReader(csv_file))
if sheet in ['3', '4', '5']:
_parse_episodejoke_details(csv.DictReader(csv_file), sheet)
if sheet == '7':
_parse_joke_blurbs(csv.DictReader(csv_file))
def _parse_joke_blurbs(sheet):
"""
Grab the joke blurbs sheet from Google docs.
"""
for row in sheet:
joke = Joke.get(Joke.code == row['Code'])
joke.blurb = row['Description']
try:
joke.related_joke_code = int(row['Related'])
except ValueError:
pass
joke.save()
def _parse_episodes(sheet):
"""
Parses episode sheet.
Imports episodes.
Will not update.
"""
episodes = []
seasons = []
ratings = []
names = []
indexes = []
zip_list = [seasons, episodes, ratings, None, names]
counter = 0
for row in sheet:
if counter != 3:
zip_list[counter] += row.values()
counter += 1
indexes = row.keys()
output = []
for episode in zip(episodes, seasons, ratings, names, indexes):
if episode[0] == 'EPISODE':
pass
else:
episode_dict = {}
episode_dict['episode'] = int(episode[0])
episode_dict['season'] = int(episode[1])
episode_dict['title'] = episode[3].decode('utf-8')
episode_dict['rating'] = episode[2]
episode_dict['code'] = 's%se%s' % (episode[1].zfill(2), episode[0].zfill(2))
episode_dict['number'] = int(episode[4])
output.append(episode_dict)
for row in output:
try:
Episode.get(Episode.code == row['code'])
except Episode.DoesNotExist:
Episode.create(**row).save()
def _parse_jokes(sheet):
"""
Parses joke sheet.
Imports jokes.
Will not update.
"""
for row in sheet:
joke_dict = {}
for item in ['code', 'primary_character', 'text']:
try:
joke_dict[item] = int(row[item])
except ValueError:
joke_dict[item] = row[item].decode('utf-8')
try:
Joke.get(Joke.code == joke_dict['code'])
except Joke.DoesNotExist:
Joke.create(**joke_dict).save()
def _parse_episodejoke_details(sheet, sheet_num):
"""
Parses the details, origin and connection sheets.
Adds data to existing episodejokes.
"""
FIELDS = [None, None, None, 'details', None, 'connection']
field = FIELDS[int(sheet_num)]
episodes = list(Episode.select())
for row in sheet:
for episode in episodes:
joke = Joke.get(Joke.code == row['code'])
if episode.code == 's02e03':
episode.title = '\xc2\xa1Amigos!'
if episode.code == 's01e13':
episode.title = 'Beef Consomm\xc3\xa9'
if row[episode.title]:
if field:
ej_code = '%sj%s' % (episode.code, joke.code)
payload = {}
payload[field] = row[episode.title]
EpisodeJoke.update(**payload).where(EpisodeJoke.code == ej_code).execute()
def _parse_episodejokes(sheet):
"""
Parses joke sheet.
Imports episodejokes.
Will not update.
"""
episodes = list(Episode.select())
for row in sheet:
for episode in episodes:
joke = Joke.get(Joke.code == row['code'])
if episode.code == 's02e03':
episode.title = '\xc2\xa1Amigos!'
if episode.code == 's01e13':
episode.title = 'Beef Consomm\xc3\xa9'
if row[episode.title] in ['1', 'f', 'b']:
ej_dict = {}
ej_dict['joke'] = joke
ej_dict['episode'] = episode
ej_dict['joke_type'] = row[episode.title]
ej_dict['code'] = '%sj%s' % (ej_dict['episode'].code, ej_dict['joke'].code)
try:
EpisodeJoke.get(EpisodeJoke.code == ej_dict['code'])
except EpisodeJoke.DoesNotExist:
EpisodeJoke(**ej_dict).save()