-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
145 lines (140 loc) · 4.77 KB
/
main.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
try:
from BeautifulSoup import BeautifulSoup
except ImportError:
from bs4 import BeautifulSoup
from pprint import pprint
import csv
import datetime
import requests
import json
import sys
VERBOSE = False
#get command line args
n_args = len(sys.argv)
for i in range(1, n_args):
print(sys.argv[i], )
if sys.argv[i] == "--verbose":
VERBOSE = True
#cookies session
cookies = {
'Jax.Q': '',
'_gid': '',
'JSESSIONID': '',
'_ga': ''
}
#current contest files
contests = [
'contests/MC621_MC821_08_11 - Virtual Judge.html',
]
#current username order
usernames = [
'LuizAndia',
'b164923',
'hboschirolli',
];
#usernames list to present
usernames_to_present = [
'<null>',
'b164923',
'hboschirolli',
'LuizAndia',
]
usernames_order = {}
order = 1
for username in usernames:
usernames_order[username] = order
order += 1
#for each contest
for contest in contests:
#setup
f= open(contest,"r")
html =f.read()
f.close()
parsed_html = BeautifulSoup(html, from_encoding="utf-8")
students = {}
jsonData = json.loads(parsed_html.body.find('textarea', attrs = {'name' : "dataJson"}).text)
contest_id = jsonData['id']
n_problems = len(jsonData['problems'])
end_contest_time = jsonData['end']
print(contest)
#iterate over usernames
for username in usernames:
if VERBOSE:
print("\t", username)
student = {
'user': username,
'failed': 0,
'rank': 0,
'out_time': 0,
'accepted': 0
}
for problem in range(n_problems):
problem_letter = chr(ord('A') + problem)
if VERBOSE:
print("\t", problem_letter)
#get request
request = requests.get(
url = 'https://vjudge.net/status/data/',
params = {
'start': 0,
'length': 20,
'un': username,
'num': problem_letter,
'res': 1,
'language': '',
'inContest': True,
'contestId': contest_id
},
headers = {
'accept': 'application/json, text/javascript, */*; q=0.01',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'cookie': "_ga="+cookies['_ga']+"; _gid="+cookies['_gid']+"; Jax.Q="+cookies['Jax.Q']+"; JSESSIONID="+cookies['JSESSIONID']+"; _gat=1",
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'x-requested-with': 'XMLHttpRequest'
}
).text
response = json.loads(request)
if len(response['data']) == 0:
student['failed'] += 1
else:
valid = False
for accepted in response['data']:
if accepted['time'] <= end_contest_time and accepted['userName'].lower() == student['user'].lower():
valid = True
break
if valid:
student['accepted'] += 1
else:
student['out_time'] += 1
students[username] = student
#get username rank
rows = parsed_html.body.find('table', attrs={'id': 'contest-rank-table' }).tbody.find_all('tr')
rank = 1
for row in rows:
if rank > 3:
break;
username = row.find('td', attrs={'class' : 'team'}).div.a.find(text=True, recursive=False).rstrip()
if username in students:
students[username]['rank'] = rank
rank += 1
#write csv
with open(contest.split('/')[1] + ".csv", mode='w') as csv_file:
fieldnames = ['user', 'accepted', 'out_time', 'rank', 'failed']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for username_to_present in usernames_to_present:
row = {'user':username_to_present, 'accepted':0, 'out_time':0, 'rank':'', 'failed':0}
for username in students:
if username == username_to_present:
row = students[username]
if row['rank'] == 1:
row['rank'] = 'FIRST'
elif row['rank'] == 2:
row['rank'] = 'SECOND'
elif row['rank'] == 3:
row['rank'] = 'THIRD'
else:
row['rank'] = ''
break
writer.writerow(row)