-
Notifications
You must be signed in to change notification settings - Fork 3
/
spider_json.py
33 lines (27 loc) · 874 Bytes
/
spider_json.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
# coding=utf-8
import json
import sys
import requests
# Get official rating changes data from Codeforces API
def get_rating(contest_id):
url = 'http://codeforces.com/api/contest.ratingChanges?contestId={}'.format(contest_id)
rst = requests.get(url)
json_response = rst.content.decode()
dict_json = json.loads(json_response)
if dict_json['status'] == 'OK':
return dict_json['result']
else:
return None
def work(contest_id):
data = get_rating(contest_id)
if data:
with open('tests/cf_rating_official_{}.json'.format(contest_id), 'w') as f:
f.write(json.dumps(data, indent=4))
else:
print('error!')
if __name__ == '__main__':
if len(sys.argv) < 2:
print('Usage: python3 file_name.py [codeforces_contest_id]')
sys.exit(1)
contest_id = sys.argv[1]
work(contest_id)