forked from cookiedoth/cf-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (51 loc) · 1.4 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
import requests
import csv
from tqdm import tqdm
from problem_parser import parse_problem
parsed_names = set()
try:
csvfile = open('cf.csv', newline='')
reader = csv.DictReader(csvfile)
parsed_names = set(map(lambda dct : dct['name'], reader))
parsed_names.remove('name')
csvfile.close()
except FileNotFoundError:
pass
skip = set()
try:
with open('bad_urls') as f:
skip = set(map(lambda x: x.rstrip(), f.readlines()))
except FileNotFoundError:
pass
f = open('cf.csv', 'a')
writer = csv.writer(f)
headers = ['name', 'legend', 'input', 'output', 'note', 'rating', 'tags']
writer.writerow(headers)
problems = requests.get("https://codeforces.com/api/problemset.problems").json()
for problem in tqdm(problems['result']['problems']):
if problem['name'] in parsed_names:
continue
contestId = problem['contestId']
index = problem['index']
url = f'https://codeforces.com/contest/{contestId}/problem/{index}'
if url in skip:
continue
try:
problem_data = parse_problem(url)
except Exception as e:
with open('err', 'a') as err:
print(f'Cannot parse {url}', file=err)
with open('bad_urls', 'a') as bad_urls:
print(url, file=bad_urls)
continue
row = [
problem['name'],
problem_data.get('legend', ''),
problem_data.get('input', ''),
problem_data.get('output', ''),
problem_data.get('note', ''),
problem.get('rating', ''),
' '.join(problem.get('tags', []))
]
writer.writerow(row)
f.close()