-
Notifications
You must be signed in to change notification settings - Fork 10
/
upgrade-files.py
78 lines (57 loc) · 1.91 KB
/
upgrade-files.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
from collections import defaultdict
import sys
import json
import os
# os.chdir("db")
rounds = defaultdict(list)
for line in sys.stdin:
q = json.loads(line)
year = "Unknown"
if 'year' in q:
year = q['year']
path = q['type'] + '/' + str(year) + "/" + q['tournament']
# try:
# os.makedirs(path)
# except OSError:
# pass
if 'num' not in q:
q['num'] = len(rounds[path + "/" + q['round']]) + 1
if 'answer' not in q:
continue
rounds[path + "/" + q['round']].append(q)
# print q['round'].split(".")[0].split("-")[0].strip().replace("_", " ")
# rou = path + '/' +
# with open("test.txt", "a") as myfile:
# myfile.write("appended text")
def ser(obj):
return "\n".join([unicode(key) + ": " + unicode(obj[key]) for key in obj])
def squi(q):
keys = ["num", "category", "difficulty", "fixed", "seen", "answer"]
return "\n".join([unicode(key) + ": " + unicode(q[key]) for key in keys if key in q]) + "\n\n" + q['question']
for rou in rounds:
# print rou
questions = sorted(rounds[rou], key=lambda q: int(q['num']))
print len(questions), rou
hed = questions[0]
# header = {
# 'tournament': hed['tournament'],
# 'round': hed['round']
# }
headerkeys = ['tournament', 'round', 'year', 'date']
roundname = hed['round'].split(".")[0] + ".txt"
if hed['type'] == 'jeopardy':
path = hed['type'] + '/' + str(hed['year'])
roundname = hed['date'] + ".txt"
elif 'year' in hed:
# header['year'] = hed['year']
path = hed['type'] + '/' + str(hed['year']) + "/" + hed['tournament']
else:
path = hed['type'] + "/" + hed['tournament']
try:
os.makedirs(path)
except OSError:
pass
with open(path + "/" + roundname, "w") as f:
# f.write(("\n----\n".join([squi(q) for q in questions])).encode('utf-8'))
f.write("####\n" + "\n".join([unicode(key) + ": " + unicode(hed[key]) for key in headerkeys if key in hed]) + "\n####\n\n")
f.write(("\n\n----\n".join([squi(q) for q in questions])).encode('utf-8'))