forked from ouuan/Get-CF-Historical-TOP10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getCountries.py
55 lines (50 loc) · 1.45 KB
/
getCountries.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
import requests
import json
import time
with open("handles.json", "r") as handlesjson:
handles = json.load(handlesjson)
countries = {}
url = "https://codeforces.com/api/user.info?handles="
flag = False
for i in range(len(handles)):
if flag:
url = url + ";"
else:
flag = True
url = url + handles[i]
if i % 100 == 99:
while True:
try:
response = requests.get(url)
except:
time.sleep(1)
else:
break
data = json.loads(response.text)
for j in data["result"]:
print(j["handle"])
if "country" in j.keys():
countries[j["handle"]] = j["country"]
else:
countries[j["handle"]] = "Unknown"
url = "https://codeforces.com/api/user.info?handles="
if len(handles) % 100 != 0:
while True:
try:
response = requests.get(url)
except:
print("failed")
time.sleep(1)
else:
print("success")
break
data = json.loads(response.text)
for j in data["result"]:
print(j["handle"])
if "country" in j.keys():
countries[j["handle"]] = j["country"]
else:
countries[j["handle"]] = "Unknown"
url = "https://codeforces.com/api/user.info?handles="
with open("countries.json", "w") as countriesjson:
countriesjson.write(json.dumps(countries))