-
Notifications
You must be signed in to change notification settings - Fork 0
/
zp.py
211 lines (190 loc) · 7.39 KB
/
zp.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# import datetime
# from bs4 import BeautifulSoup
import json
import logging
from time import sleep
# logging.basicConfig(filename='zp.log', level=logging.ERROR)
from requests_html import HTMLSession
import pandas as pd
import streamlit as st
# import s3fs
import requests
def zwiftprofile(username, password):
try:
get_token = requests.get(
f"https://z00pbp8lig.execute-api.us-west-1.amazonaws.com/latest/zwiftId?username={username}&pw={password}"
)
get_profile = requests.get(f"https://zwiftapi.weracehere.org/profile?zid={get_token.json()}")
return get_profile.json()
except Exception as e:
st.write(f"Error:\n{e}.")
class ZwiftLogin(object):
def __init__(self):
self.login_data = {}
self.login_data.update(st.secrets["zwiftpower"])
def get(self, api_urls: dict, content=False) -> dict:
session = HTMLSession()
z = session.get("https://zwiftpower.com")
# logging.info(z.cookies.get('phpbb3_lswlk_sid'))
self.login_data["sid"] = z.cookies.get("phpbb3_lswlk_sid")
if "Login Required" in z.text: # get logged in
try:
session.post("https://zwiftpower.com", data=self.login_data)
assert "Profile" in session.get("https://zwiftpower.com/events.php").text
# print('login success')
# logging.info('Login successful')
except Exception as e:
print("Login error")
# logging.error(f"Failed to login: {e}")
data = {}
for name, url in api_urls.items():
response = session.get(url)
# logging.info("Status", response.status_code)
try:
res = response.json()
data[f"{name}_json"] = res
data[f"{name}_df"] = pd.DataFrame(res["data"])
except Exception as e:
print("Error in json", e)
data[f"{name}_json"] = None
sleep(1)
return data
def fix_columns(df, col):
"""Fix columns that have a list of data.
We will use the first value as replacement for the column value and the second as NAME_2
"""
try:
split_df = pd.DataFrame(df[col].tolist(), columns=[col, f"{col}_2"])
# concat df and split_df
df.drop(col, axis=1, inplace=True)
return pd.concat([df, split_df], axis=1)
except Exception as e:
print(f"Problem converting: {col}")
print(e)
return df
def event_results(event_url):
"""Get the results of an event.
Event URLS
Main event page
https://zwiftpower.com/cache3/results/3438682_zwift.json
https://zwiftpower.com/cache3/results/3438682_view.json
Sprint & KOMs
https://zwiftpower.com/api3.php?do=event_sprints&zid=3438615
Primes
https://zwiftpower.com/api3.php?do=event_primes&zid=3438615
Power Curve, for event, needs rider ID or get all.
https://zwiftpower.com/api3.php?do=critical_power_event&zwift_id=507355&zwift_event_id=3438615&type=watts
Analysis
https://zwiftpower.com/api3.php?do=analysis_event_list&zwift_event_id=3438615
"""
try:
assert "https://zwiftpower.com/events.php?zid=" in event_url
except AssertionError:
print("Invalid event URL")
return None
# parse url
event_id = event_url.split("=")[-1].split("&")[0]
api_urls = dict(
view=f"https://zwiftpower.com/cache3/results/{event_id}_view.json",
zwift=f"https://zwiftpower.com/cache3/results/{event_id}_zwift.json",
sprints=f"https://zwiftpower.com/api3.php?do=event_sprints&zid={event_id}",
primes=f"https://zwiftpower.com/api3.php?do=event_primes&zid={event_id}",
analysis=f"https://zwiftpower.com/api3.php?do=analysis_event_list&zwift_event_id={event_id}",
)
z = ZwiftLogin()
results = z.get(api_urls)
print(results.keys())
for name in results.keys():
if "_df" in name:
for c in results[name].columns:
if isinstance(results[name][c][0], list):
results[name] = fix_columns(results[name], c)
if "zwid" in results[name].columns:
results[name]["zwid"] = results[name]["zwid"].astype(str)
results["frr_df"] = results_frr(results["view_df"], results["zwift_df"])
results["event_id"] = event_id
return results
def racer_results(event_url):
"""Get the results of an event.
https://zwiftpower.com/profile.php?z=
User profile URLS
Main uprofile page
https://zwiftpower.com/cache3/profile/110649_all.json
https://zwiftpower.com/cache3/profile/110649_rider_compare_victims.json
Power Curve
https://zwiftpower.com/api3.php?do=critical_power_profile&zwift_id=110649&zwift_event_id=&type=watts
ZPoints:
https://zwiftpower.com/api3.php?do=profile_zpoints&z=110649
https://zwiftpower.com/api3.php?do=profile_zpoints_power&z=110649
Courses:
https://zwiftpower.com/api3.php?do=course_records&z=110649
"""
try:
assert "https://zwiftpower.com/profile.php?z" in event_url
except AssertionError:
print("Invalid event URL")
return None
# parse url
profile_id = event_url.split("=")[-1].split("&")[0]
api_urls = dict(
results=f"https://zwiftpower.com/cache3/profile/{profile_id}_all.json",
compare=f"https://zwiftpower.com/cache3/profile/{profile_id}_rider_compare_victims.json",
sprints=f"https://zwiftpower.com/api3.php?do=critical_power_profile&zwift_id={profile_id}&zwift_event_id=&type=watts",
zpoints=f"https://zwiftpower.com/api3.php?do=profile_zpoints&z={profile_id}",
zpoints_power=f"https://zwiftpower.com/api3.php?do=profile_zpoints_power&z={profile_id}",
courses=f"https://zwiftpower.com/api3.php?do=course_records&z={profile_id}",
)
z = ZwiftLogin()
results = z.get(api_urls)
for name in results.keys():
if "_df" in name:
for c in results[name].columns:
if isinstance(results[name][c][0], list):
results[name] = fix_columns(results[name], c)
results["profile_id"] = profile_id
return results
def results_frr(view_results: pd.DataFrame, zwift_results: pd.DataFrame) -> pd.DataFrame:
"""Custom results_view for frr input view_results and zwift_results json data"""
view_results = view_results.copy()
view_results["id"] = ""
zwift_results = zwift_results.copy()
zwift_results.drop(
columns=[c for c in zwift_results.columns if c not in ["name", "zwid"]],
inplace=True,
)
zwift_results.rename(
columns={
"name": "ZWIFT_NAME",
},
inplace=True,
)
results = view_results.merge(zwift_results, on="zwid", how="left")
results.rename(
columns={
"zid": "ZEVENT",
"zwid": "ZWID",
"category": "PEN",
"position_in_cat": "Pen position",
"time_gun": "Stage Time",
"avg_power": "WATT",
"np": "NP WATT",
"avg_wkg": "WKG",
"name": "VIEW_NAME",
},
inplace=True,
)
keep = [
"id",
"ZEVENT",
"VIEW_NAME",
"ZWIFT_NAME",
"ZWID",
"PEN",
"Pen position",
"Stage Time",
"WATT",
"NP WATT",
"WKG",
]
results = results[keep]
return results