-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
executable file
·78 lines (61 loc) · 2.86 KB
/
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
#!/usr/bin/env python3.8
from stravalib.client import Client
import pickle
import time
import pprint
MY_STRAVA_CLIENT_ID, MY_STRAVA_CLIENT_SECRET = open('./client.secret').read().strip().split(',')
#auth based on: https://medium.com/analytics-vidhya/accessing-user-data-via-the-strava-api-using-stravalib-d5bee7fdde17
# One time authorize app:
#
# print ('Client ID and secret read from file'.format(MY_STRAVA_CLIENT_ID) )
# url = client.authorization_url(client_id=MY_STRAVA_CLIENT_ID, redirect_uri='http://127.0.0.1:5000/authorization', scope=['read_all','profile:read_all','activity:read_all'])
# print (url)
# CODE = '' #enter `code` from callback url
# access_token = client.exchange_code_for_token(client_id=MY_STRAVA_CLIENT_ID, client_secret=MY_STRAVA_CLIENT_SECRET, code=CODE)
# with open('./access_token.pickle', 'wb') as f:
# pickle.dump(access_token, f)
client = Client()
with open('./access_token.pickle', 'rb') as f:
access_token = pickle.load(f)
print('Latest access token read from file:')
print(access_token)
if time.time() > access_token['expires_at']:
print('Token has expired, will refresh')
refresh_response = client.refresh_access_token(client_id=MY_STRAVA_CLIENT_ID, client_secret=MY_STRAVA_CLIENT_SECRET, refresh_token=access_token['refresh_token'])
access_token = refresh_response
with open('./access_token.pickle', 'wb') as f:
pickle.dump(refresh_response, f)
print('Refreshed token saved to file')
client.access_token = refresh_response['access_token']
client.refresh_token = refresh_response['refresh_token']
client.token_expires_at = refresh_response['expires_at']
else:
print('Token still valid, expires at {}'
.format(time.strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime(access_token['expires_at']))))
client.access_token = access_token['access_token']
client.refresh_token = access_token['refresh_token']
client.token_expires_at = access_token['expires_at']
athlete = client.get_athlete()
print("Athlete's name is {} {}, based in {}, {}"
.format(athlete.firstname, athlete.lastname, athlete.city, athlete.country))
activities = client.get_activities(limit=20)
pprint.pprint(list(activities))
kudosyosob = {}
kudosyosob["krzysztof"] = 5
kudosyosob["daniel"] = 7
wszystkiekudosy = 0
for act in (list(activities)):
#pprint.pprint(act.highlighted_kudosers)
kudositerator = client.get_activity_kudos(act.id)
for kudos in kudositerator:
if kudos.firstname+kudos.lastname in kudosyosob.keys():
kudosyosob[kudos.firstname+kudos.lastname] += 1
else:
kudosyosob[kudos.firstname+kudos.lastname] = 1
wszystkiekudosy += 1
print(kudos.firstname,kudos.lastname)
print(wszystkiekudosy)
#pprint.pprint (kudosyosob)
#pprint.pprint (sorted(kudosyosob))
for w in sorted(kudosyosob, key=kudosyosob.get, reverse=True):
print(w, kudosyosob[w])