-
Notifications
You must be signed in to change notification settings - Fork 6
/
update-episodes.py
executable file
·63 lines (49 loc) · 1.54 KB
/
update-episodes.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
#!/usr/bin/env python
import requests, csv, pprint, json
from operator import itemgetter
from datetime import datetime
GOOGLE_SHEETS = 'https://docs.google.com/spreadsheets/d/{0}/export?format=csv'
SHEET_KEY = '1rjtVle08VtK7AM9GMZ1zgQpTpctw0n9mCm8VOYN-Qso'
# Build Doc URL
doc_url = GOOGLE_SHEETS.format(SHEET_KEY)
# Request File
req = requests.get(doc_url)
raw_data = req.text
raw_data = raw_data.encode('utf-8')
raw_data = raw_data.split('\r\n')
# Parse CSV Data
csv_reader = csv.reader(raw_data)
csv_data = list(csv_reader)
# Build Dictionary
episodes = {}
for ep in csv_data:
# Skip first row and empty rows
try:
int(ep[0])
except ValueError:
continue
# Label the data
ep_season = int(ep[0])
episode = {
'id': int(ep[1]),
'title': ep[2],
'date': datetime.strptime(ep[3], '%Y-%m-%d').strftime('%b %-d, %Y'),
'vid': None if (ep[4] == '') else ep[4],
}
# Add the season key if it doesn't exist
if not episodes.get(ep_season):
episodes[ep_season] = []
# Inject the episode into the list
episodes[ep_season].append(episode)
# Transform the episodes dict into the final structure
seasons_coll = []
for season in episodes.keys():
seasons_coll.append({
'id': season,
'episodes': episodes[season],
})
seasons_coll = sorted(seasons_coll, key=itemgetter('id'), reverse=True)
# Output the Episodes Dictionary as JSON file
handle = open('static/episodes.json', 'w')
handle.write(json.dumps(seasons_coll, indent=2))
handle.close()