-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_schedule_v2.py
195 lines (166 loc) · 5.27 KB
/
get_schedule_v2.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
import requests
import peloton as Peloton
import json
import google_cal_event_creator as _cal_creator
import datetime as _date_time
import google_cal_event_creator as event_creator
from dotenv import load_dotenv
import os
from enum import Enum as enum
"""
Created on Wed Sep 7 08:45PM 2022
Copyright 2022 Mio Diaz
"""
base_url = 'https://api.onepeloton.com'
load_dotenv()
USERNAME = os.environ.get('_username')
PASSWORD = os.environ.get('_password')
CAL_ID = os.environ.get('_cal_id')
class Emoji(enum):
cycling = '🚴♀'
meditation = '🧘♀️'
strength = '🏋️♀️'
cardio = '🥊'
running = '🏃♀️'
walking = '🚶♀️'
def _peloton_session(s):
'''
Start peloton api session using username & password
save the user id returned in the json data for usage
later
Parameters
----------
s
session.request()
Returns
-------
dict
user id, session id, etc
'''
payload = {'username_or_email': USERNAME, 'password': PASSWORD}
headers = {
"Content-Type": "application/json",
}
response = s.post(base_url + '/auth/login', json=payload, headers=headers)
usr_info = response.json()
return usr_info
def _get_reservations(session, usr_id, headers):
'''
Using the session started and the user id retrieved, get
the reservations for this user - reservations = upcoming classes
on the user's schedule - send each reservation id and get the
ride id
Parameters
----------
session
session started in _peloton_session
usr_id
user id retrieved when session started
headers
headers used for all calls asking for json content
'''
reservation_url = base_url + '/api/user/' + usr_id + '/reservations'
response = session.get(reservation_url, headers=headers)
reservations = response.json()
for reservation in reservations['data']:
_get_ride_id(session, reservation['peloton_id'], headers)
'''
Using the reservation id, get the ride id for the specified reservations
use the ride id to retrieve ride details including instructor, description, etc
Parameters
----------
session
session started in _peloton_session
reservation_id
passed from _get_reservations
headers
headers used for all calls asking for json
'''
def _get_ride_id(session, reservation_id, headers):
ride_url = base_url + '/api/peloton/' + reservation_id
response = session.get(ride_url, headers=headers)
rides = response.json()
class_category = rides['live_class_category']
if class_category != "":
start_time = rides['scheduled_start_time']
else:
start_time = rides['pedaling_start_time']
_get_ride_details(session, rides['ride_id'], headers, start_time)
'''
Retrieves ride details using the ride_id passed from _get_ride_id
in order to begin building calendar api response body properties
Parameters
----------
session
session started in _peloton_session
ride_id
passed from _get_ride_id
headers
headers used for all calls asking for json
'''
def _get_ride_details(session, ride_id, headers, start_time):
ride_url = base_url + '/api/ride/' + ride_id + '/details'
response = session.get(ride_url, headers=headers)
rides = response.json()
discipline = rides['ride']['fitness_discipline']
duration = rides['ride']['duration']
emoji = _get_emoji(rides['ride']['fitness_discipline'])
summary = str(emoji) + ' ' + rides['ride']['title'] + ' ' + rides['ride']['instructor']['name']
print(summary)
start_time = _date_time.datetime.fromtimestamp(start_time)
print(start_time)
end_time = _get_end_time(start_time, duration)
new_format = "%Y-%m-%dT%H:%M:%S-05:00"
_cal_creator._calendar_api_call(summary, start_time.strftime(new_format), end_time.strftime(new_format), CAL_ID)
'''
Calculates end time of ride based on start time and duration
Parameters
----------
start_timestamp
start time converted to datetime object
duration
duration of ride in seconds
Returns
----------
datetime object
calculated end time of ride
'''
def _get_end_time(start_timestamp, duration):
return start_timestamp + _date_time.timedelta(seconds=duration)
'''
Selects appropriate emoji for calendar event summary based on fitness
discipline of class
Parameters
----------
fitness_discipline
string
'''
def _get_emoji(fitness_discipline):
match fitness_discipline:
case 'cycling':
return Emoji.cycling.value
case 'cardio':
return Emoji.cardio.value
case 'meditation':
return Emoji.meditation.value
case 'strength':
return Emoji.strength.value
case 'running':
return Emoji.running.value
case 'walking':
return Emoji.walking.value
case _:
print(" ")
'''
main method which asks for a session to start and stores the user id required
for the next api call to get reservation ids
'''
def main():
s = requests.Session()
usr_info = _peloton_session(s)
usr_id = usr_info['user_id']
headers = {
"Content-Type": "application/json",
}
_get_reservations(s, usr_id, headers)
main()