-
Notifications
You must be signed in to change notification settings - Fork 2
/
packet_parser.py
executable file
·109 lines (75 loc) · 2.89 KB
/
packet_parser.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
#!/usr/bin/env python3
import os
import sys
import re
import shlex
import subprocess
import string
import os.path
import glob
import json
import argparse
import codecs
import requests
from pprint import pprint
from pprint import pprint
from Tossup import Tossup
from Bonus import Bonus
from Packet import Packet
from Tournament import Tournament
from utils import conf_gen
from network import get_season_tournaments
def send_json_data(json_file, url):
data = json.load(open(json_file, 'r'))
tournament = data['tournament']
year = data['year']
print(year, tournament)
r = requests.post(url, json=data)
with open('err.html', 'w') as f:
f.write(r.content)
def parser_driver(doc_file, mode='json'):
p = Packet('test')
p.load_packet_from_file(doc_file)
p.is_valid()
def parse_packets(filenames, mode='json'):
for doc_file in filenames:
tossups, bonuses = parse_packet(docfile)
def parse_directory(dirname, mode='json', reuse_html=False):
tour = Tournament()
tour.create_tournament_from_directory(dirname, reuse_html=reuse_html)
tour_json_file = os.path.join(dirname, '{0} {1}.json'.format(tour.year, tour.tour_name))
if mode == 'json':
if tour.is_valid():
json.dump(tour.to_dict(), open(tour_json_file, 'w'), indent=4)
if __name__ == '__main__':
#reload(sys)
#sys.setdefaultencoding('utf-8')
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', dest='file')
parser.add_argument('-d', '--dir', dest='dir')
parser.add_argument('-o', '--operation', dest='operation')
parser.add_argument('-s', '--spec', dest='spec')
parser.add_argument('-m', '--mode', dest='mode', default='json')
parser.add_argument('-u', '--url', dest='url')
parser.add_argument('-y', '--year', dest='year', type=int)
parser.add_argument('--reuse-html', type=bool, default=False)
parser.add_argument('-op', '--output-file', dest='output_file')
args = parser.parse_args()
if args.dir is not None and args.operation == 'process':
parse_directory(args.dir, args.mode, args.reuse_html)
if args.file is not None and args.operation == 'test':
packet = Packet('test')
packet.load_packet_from_file(args.file, test=True)
if args.file is not None and args.operation is not None:
if args.operation == 'process':
parser_driver(args.file)
if args.operation == 'validate':
validate_json(args.file)
if args.operation == 'import':
import_json_into_mongo(args.file)
if args.operation == 'conf' and args.dir is not None and args.spec is not None:
conf_gen(args.dir, args.spec)
if args.operation == 'send' and args.url is not None and args.file is not None:
send_json_data(args.file, args.url)
if args.operation == 'fetch' and args.year:
get_season_tournaments(args.year)