-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
73 lines (65 loc) · 2.62 KB
/
run.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
import sys
import time
import json
import requests
from bs4 import BeautifulSoup
def get_prev_update_time(contestType: str):
assert contestType in ("algo", "heuristic")
try:
with open(f"{contestType}.json", "r", encoding="utf-8") as f:
res = json.load(f)["last_update_time"]
return res
except Exception:
return ""
def get_rankings(contestType: str, sleep_time=3):
assert contestType in ("algo", "heuristic")
prev_update_time = get_prev_update_time(contestType)
page = 1
time.sleep(sleep_time)
response = requests.get(f"https://atcoder.jp/ranking/all?contestType={contestType}&page={page}")
if response.status_code != requests.codes.ok:
print(
f"{response.status_code=} in requests.get(https://atcoder.jp/ranking/all?contestType={contestType}&page={page})",
sys.stderr,
)
return {"success": False, "last_update_time": prev_update_time, "data": {}}
soup = BeautifulSoup(response.text, "html.parser")
tm = soup.select_one("time")
assert tm is not None
last_update_time = str(tm.string)
if last_update_time == prev_update_time:
return {"success": False, "last_update_time": prev_update_time, "data": {}}
result = {"success": True, "last_update_time": last_update_time, "data": {}}
while True:
print(page)
table = soup.select("table")[1].tbody
if table is None:
break
rows = table.select("tr")
if len(rows) == 0:
break
for i in range(len(rows)):
span = rows[i].select("td")[1].select_one("span")
assert span is not None
name = span.string
rating = rows[i].select("td")[3].string
assert rating is not None
# result["data"][str(name)] = {"span": str(span), "rating": int(rating)}
result["data"][str(name)] = int(rating)
page += 1
time.sleep(sleep_time)
response = requests.get(f"https://atcoder.jp/ranking/all?contestType={contestType}&page={page}")
if response.status_code != requests.codes.ok:
print(
f"{response.status_code=} in requests.get(https://atcoder.jp/ranking/all?contestType={contestType}&page={page})",
sys.stderr,
)
break
soup = BeautifulSoup(response.text, "html.parser")
return result
result = get_rankings("heuristic")
if result["success"]:
with open(f"heuristic.json", "w", encoding="utf-8") as f:
json.dump(result, f)
with open(f"heuristic_indent.json", "w", encoding="utf-8") as f:
json.dump(result, f, indent=2)