-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
60 lines (46 loc) · 1.98 KB
/
api.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
import requests
import sys
import time
class Client(object):
def __init__(self, token, sleep_time = None):
self._token = token
self._sleep_time = sleep_time
def _get_headers(self):
return {"Authorization": "Bearer {}".format(self._token)}
def _get(self, url):
r = requests.get(url, headers=self._get_headers())
self._sleep()
if r.status_code != 200:
raise ValueError(r.text)
return r.json()
def _put(self, url, data):
r = requests.put(url, data, headers=self._get_headers())
self._sleep()
if r.status_code != 200:
raise ValueError()
def update_activity(self, id, **kwargs):
self._put("https://www.strava.com/api/v3/activities/{}".format(id),
dict(kwargs))
def get_activities_page(self, page, per_page):
return self._get(
"https://www.strava.com/api/v3/athlete/activities" +
"?page={}&per_page={}".format(page, per_page))
def get_activities_after(self, seconds_from_epoch, page, per_page):
activities = self._get(
"https://www.strava.com/api/v3/athlete/activities" +
"?after={}&page={}&per_page={}".format(
seconds_from_epoch, page, per_page))
return activities
def get_activity_detail(self, id):
return self._get(
"https://www.strava.com/api/v3/activities/{}".format(id))
def get_streams(self, id, stream_types = ['time','latlng','altitude']):
return self._get(
"https://www.strava.com/api/v3/activities/{}/streams?keys={}&key_by_type=true".format(id, ','.join(stream_types)))
def get_athlete(self):
return self._get("https://www.strava.com/api/v3/athlete")
def _sleep(self):
#used because of throttling strava api
if self._sleep_time is not None:
print('sleep {}'.format(self._sleep_time), file=sys.stderr)
time.sleep(self._sleep_time)