-
Notifications
You must be signed in to change notification settings - Fork 0
/
peloton_client.py
212 lines (179 loc) · 7.12 KB
/
peloton_client.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
212
"""
Based on endpoints documented at
https://app.swaggerhub.com/apis/DovOps/peloton-unofficial-api/0.2.3
"""
import requests
# Global vars
USER_AGENT = 'web'
API_BASE_URL = 'https://api.onepeloton.com'
API_HEADERS = {
'Content-Type': 'application/json',
'Peloton-Platform': 'web',
'User-Agent': USER_AGENT,
}
class PelotonClient:
"""Primary client class for accessing Peloton API methods in Python."""
def __init__(self, username=None, password=None):
if not (username and password):
raise ValueError('Peloton credentials not provided.')
self.user_id = None
self.username = username
self.password = password
self.session = self._init_session()
def _init_session(self):
"""Initializes API client with provided credentials"""
api_session = requests.Session()
request_uri = '/auth/login'
request_parameters = {
'username_or_email': self.username,
'password': self.password,
}
response = api_session.post(
API_BASE_URL + request_uri,
json=request_parameters,
headers=API_HEADERS,
)
if response.status_code != 200:
raise ValueError(
'Session authorization failed. Check credential validity.')
self.user_id = response.json().get('user_id')
return api_session
def _call_api(self, request_uri, request_parameters, fetch_all=False):
"""Performs calls against Peloton API endpoints."""
response_data = []
initial_response = self.session.get(
API_BASE_URL + request_uri,
headers=API_HEADERS,
params=request_parameters,
)
if fetch_all:
total_pages = initial_response.json().get('page_count')
while request_parameters['page'] <= total_pages:
response = self.session.get(
API_BASE_URL + request_uri,
headers=API_HEADERS,
params=request_parameters,
)
response_data.append(response.json())
request_parameters['page'] += 1
else:
response_data.append(initial_response.json())
return response_data
def fetch_workouts(self, limit=10, fetch_all=False, page=0):
"""Method to fetch user's workout data. Defaults to 10 workouts."""
workouts = []
request_uri = '/api/user/%s/workouts' % self.user_id
request_parameters = {
'page': page,
'limit': limit,
'joins': 'ride,ride.instructor'
}
response = self._call_api(request_uri,
request_parameters,
fetch_all=fetch_all)
for response_data in response:
workouts.extend(response_data.get('data'))
return workouts
def fetch_user_data(self):
"""Method to fetch user's account data."""
request_uri = '/api/user/%s' % self.user_id
request_parameters = {}
response = self._call_api(request_uri, request_parameters)
return response
def fetch_user_achievements(self):
"""Method to fetch user's achievement data."""
achievements = []
request_uri = '/api/user/%s/achievements' % self.user_id
request_parameters = {}
response = self._call_api(request_uri, request_parameters)
for response_data in response:
achievement_categories = response_data.get('categories')
for achievement_category in achievement_categories:
achievements.append({
'type':
achievement_category.get('slug'),
'achievements':
achievement_category.get('achievements'),
})
return achievements
def fetch_user_overview(self):
"""Method to fetch an overview of a given user's account data."""
request_uri = '/api/user/%s/overview' % self.user_id
request_parameters = {}
response = self._call_api(request_uri, request_parameters)
return response[0]
def fetch_user_subscriptions(self):
"""Method to fetch user's subscription data."""
request_uri = '/api/v2/user/subscriptions'
request_parameters = {}
response = self._call_api(request_uri, request_parameters)
return response[0]
def fetch_user_followers(self, limit=10, fetch_all=False):
"""Method to fetch user's follower data. Defaults to 10 followers."""
followers = []
request_uri = '/api/user/%s/followers' % self.user_id
request_parameters = {'page': 0, 'limit': limit}
response = self._call_api(request_uri,
request_parameters,
fetch_all=fetch_all)
for response_data in response:
followers.extend(response_data.get('data'))
return followers
def fetch_user_following(self, fetch_all=False):
"""
Method to fetch users that are being followed. Defaults to 10 users.
"""
following = []
request_uri = '/api/user/%s/following' % self.user_id
request_parameters = {'page': 0}
response = self._call_api(request_uri,
request_parameters,
fetch_all=fetch_all)
for response_data in response:
following.extend(response_data.get('data'))
return following
def fetch_user_calendar(self):
"""Method to fetch user's calendar data."""
request_uri = '/api/user/%s/calendar' % self.user_id
request_parameters = {}
response = self._call_api(
request_uri,
request_parameters,
)
return response[0]
def fetch_user_challenges_past(self):
"""Method to fetch challenges user has joined in the past."""
request_uri = '/api/user/%s/challenges/past' % self.user_id
request_parameters = {'has_joined': True}
response = self._call_api(
request_uri,
request_parameters,
)
return response[0]
def fetch_user_challenges_current(self):
"""Method to fetch challenges user is currently participating in"""
request_uri = '/api/user/%s/challenges/current' % self.user_id
request_parameters = {'has_joined': True}
response = self._call_api(
request_uri,
request_parameters,
)
return response[0]
def fetch_ride(self, ride_id):
"""Method to fetch individual ride data."""
request_uri = '/api/ride/%s/details' % ride_id
request_parameters = {}
response = self._call_api(
request_uri,
request_parameters,
)
return response[0]
def fetch_workout_metrics(self, workout_id):
"""Method to fetch individual workout data."""
request_uri = '/api/workout/%s/performance_graph' % workout_id
request_parameters = {}
response = self._call_api(
request_uri,
request_parameters,
)
return response[0]