-
Notifications
You must be signed in to change notification settings - Fork 0
/
zr.py
89 lines (79 loc) · 2.83 KB
/
zr.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from time import sleep
import pandas as pd
from httpx import Client
CATS = ["Diamond", "Ruby", "Emerald", "Sapphire", "Amethyst", "Platinum", "Gold", "Silver", "Bronze", "Copper"]
SORTBY = [
"points",
"rating",
"finishes",
"wins",
"podiums",
"dnfs",
"wkg5",
"wkg15",
"wkg30",
"wkg60",
"wkg120",
"wkg300",
"wkg1200",
]
def get_elo(
all_pages: bool = False,
cat: str = "Diamond",
pagesize: int = 500,
sortby: str = "points",
sortdirection: str = "desc",
page: int = 0,
gender: str = "",
) -> dict:
"""
Get the ZRL ELO data.
"""
# https://www.zwiftracing.app/api/riders?gender=M&page=0&pageSize=50&sortBy=points&sortDirection=asc&cat=Copper
# https://www.zwiftracing.app/api/riders?page=0&pageSize=50&sortBy=points&sortDirection=desc&cat=Diamond
if gender:
url = f"https://www.zwiftracing.app/api/riders?gender={gender}page={page}&pageSize={pagesize}&sortBy={sortby}&sortDirection={sortdirection}&cat={cat}"
else:
url = f"https://www.zwiftracing.app/api/riders?page={page}&pageSize={pagesize}&sortBy={sortby}&sortDirection={sortdirection}&cat={cat}"
client = Client(follow_redirects=True)
response = client.get("https://www.zwiftracing.app")
if all_pages:
# {"riders": [], "totalResults": 12345}
page = 0
has_data = True
data = []
pagesize = 500
while has_data:
url = f"https://www.zwiftracing.app/api/riders?page={page}&pageSize={pagesize}&sortBy={sortby}&sortDirection={sortdirection}&cat={cat}"
response = client.get(url)
if len(response.json()["riders"]) > 0:
data += response.json()["riders"]
page += 1
else:
has_data = False
sleep(1)
else:
data = client.get(url).json()["riders"]
return data
def expand_elo_columns(df: pd.DataFrame) -> pd.DataFrame:
"""
Expand the elo data into a dataframe.
"""
for col in df.columns:
if isinstance(df[col][0], list):
# print(f"Column name: {col}")
df_temp = pd.DataFrame(df[col].tolist(), index=df.index)
df[[f"{col}_{c}" for c in range(len(df_temp.columns))]] = df_temp
df.drop(col, axis=1, inplace=True)
for col in df.columns:
if isinstance(df[col][0], dict):
# print(f"Column name: {col}")
df_temp = pd.DataFrame.from_dict(df[col].to_dict()).transpose()
new_names = {k: f"{col}_{k}" for k in df_temp.columns}
df_temp.rename(columns=new_names, inplace=True)
df = pd.concat([df, df_temp], axis=1)
df.drop(col, axis=1, inplace=True)
return df
if __name__ == "__main__":
df = pd.DataFrame(get_elo(all=False))
expand_elo_columns(df).to_csv("temp_zrl_elo.csv", index=False)