-
Notifications
You must be signed in to change notification settings - Fork 0
/
strava.py
91 lines (78 loc) · 3.32 KB
/
strava.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
import stravalib
import time
import config as c
class Strava(object):
def __init__(self):
self.client_id = c.get_setting(c.SECTION_SETTINGS, c.CLIENT_ID)
self.client_secret = c.get_setting(c.SECTION_SETTINGS, c.CLIENT_SECRET)
self.access_token = c.get_setting(c.SECTION_SETTINGS, c.ACCESS_TOKEN)
self.refresh_token = c.get_setting(c.SECTION_SETTINGS, c.REFRESH_TOKEN)
self.expires_at = float(c.get_setting(c.SECTION_SETTINGS, c.EXPIRES_AT))
self.total_meters = 0
self.last_activity_date = None
self.sc = stravalib.Client(self.access_token)
self.start_time = time.time()
def check_token(self):
if time.time() > self.expires_at:
self.access_token = self.sc.refresh_access_token(
self.client_id, self.client_secret, self.refresh_token
)
c.update_setting(c.SECTION_SETTINGS, c.ACCESS_TOKEN, str(self.access_token))
def get_athlete(self):
self.check_token()
athlete = self.sc.get_athlete()
return athlete
def get_activities(self):
try:
self.check_token()
for activity in self.sc.get_activities(after="2019-03-05 22:25:41+00:00"):
print(
"{0.name} {0.moving_time} {0.distance} {0.start_date}".format(
activity
)
)
except Exception as x:
print(x)
def get_total_distance(self):
try:
if time.time() > self.start_time:
self.start_time = time.time() + 900
self.check_token()
if self.total_meters == 0:
for activity in self.sc.get_activities():
self.total_meters += float(
stravalib.unithelper.miles(activity.distance)
)
if (self.last_activity_date is None) or (
activity.start_date > self.last_activity_date
):
self.last_activity_date = activity.start_date
print(
"First Run: {0.name} {0.moving_time} {0.distance} {0.start_date}".format(
activity
)
)
for activity in self.sc.get_activities(after=self.last_activity_date):
self.total_meters += float(
stravalib.unithelper.miles(activity.distance)
)
if (self.last_activity_date is None) or (
activity.start_date > self.last_activity_date
):
self.last_activity_date = activity.start_date
print(
"Second Run: {0.name} {0.moving_time} {0.distance} {0.start_date}".format(
activity
)
)
except Exception as x:
print(x)
return self.total_meters
def main():
strava = Strava()
athlete = strava.get_athlete()
print("Hello, {}. I know your email is {}".format(athlete.firstname, athlete.email))
print(strava.get_total_distance())
print(strava.get_total_distance())
if __name__ == "__main__":
main()