-
Notifications
You must be signed in to change notification settings - Fork 2
/
mongo_utils.py
58 lines (43 loc) · 2.05 KB
/
mongo_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
import pymongo
from pymongo import MongoClient
try:
conn = MongoClient('localhost', 27017)
db = conn.qbdb
tournaments = db.tournaments
packets = db.packets
tossups = db.tossups
bonuses = db.bonuses
except Exception as ex:
print 'Mongo not available'
def import_json_into_mongo(filename):
if not validate_json(filename):
print 'You have some problems in your JSON file. Correct them and try again.'
return
else:
tournament_json = json.load(open(filename, 'r'))
tournament = tournament_json['tournament']
year = tournament_json['year']
packets_json = tournament_json['packets']
num_packets = len(packets_json)
print 'importing ' + tournament
t_id = tournaments.insert({'tournament': tournament, 'year': year, 'numPackets': num_packets})
for packet in packets_json:
print 'importing packet ' + packet['author']
p_id = packets.insert({'tournament_name': packet['tournament'],
'year': packet['year'],
'author': packet['author'],
'tournament': t_id})
tossups_json = packet['tossups']
bonuses_json = packet['bonuses']
for tossup in tossups_json:
tossup_id = tossups.insert({'question': tossup['question'],
'answer': tossup['answer'],
'packet': p_id,
'tournament': t_id})
for bonus in bonuses_json:
bonus_id = bonuses.insert({'leadin': bonus['leadin'],
'part' : bonus['parts'],
'value' : bonus['values'],
'answer': bonus['answers'],
'packet': p_id,
'tournament': t_id})