-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_csv.py
73 lines (57 loc) · 1.94 KB
/
get_csv.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 datetime
import sys
from bs4 import BeautifulSoup
import requests
months = {
"Jan.": 1,
"Feb.": 2,
"March": 3,
"April": 4,
"May": 5,
"June": 6,
"July": 7,
"Aug.": 8,
"Sept.": 9,
"Oct.": 10,
"Nov.": 11,
"Dec.": 12,
}
def parse_date(s: str) -> datetime.date:
if s == "today":
return datetime.date.today()
spl = s.split()
if len(spl) != 3:
raise ValueError("unrecognized date string", s)
return datetime.date(int(spl[2]), months[spl[0]], int(spl[1][:-1]))
def get_results(user: str):
url = "https://data.typeracer.com/pit/race_history?user=" + user + "&n=100&startDate="
results = {}
while True:
req = requests.get(url)
soup = BeautifulSoup(req.text, "html.parser")
rows = soup.select("div[class=Scores__Table__Row]")
for row in rows:
race_number = int(
row.select("div[class=profileTableHeaderUniverse]")[0].find("a").string.strip()
)
wpm = int(row.select("div[class=profileTableHeaderRaces]")[0].string.strip().split()[0])
acc = (
float(row.select("div[class=profileTableHeaderRaces]")[1].string.strip()[:-1]) / 100
)
date = row.select("div[class=profileTableHeaderDate]")[0].string.strip()
date = parse_date(date)
results[race_number] = {
"wpm": wpm,
"acc": acc,
"date": date,
}
new_url = soup.find("a", href=True, string=lambda s: "load older results" in str(s))
if new_url is None:
return results
url = url.split("?")[0] + new_url["href"]
if __name__ == "__main__":
results = get_results(sys.argv[1])
print("race number,date,wpm,acc")
for race_number in sorted(results.keys()):
r = results[race_number]
print(f"{race_number},{r['date'].strftime('%Y-%m-%d')},{r['wpm']},{r['acc']}")