-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_activities.py
executable file
·48 lines (32 loc) · 1.18 KB
/
get_activities.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
#!/usr/bin/env python3
from stravaauth import get_tokens
import os
import sys
import requests
import json
import time
###########################
base_path = os.path.dirname(os.path.realpath(__file__))
def get_activities(strava_tokens, per_page=20, start=1, end=2, actfile='{}/data/activities0.json'.format(base_path)):
# pages: 200 results per page, so by default we fetch 20 pages.
# there is a small bug, outputting "][" in between each JSON dump
# should append array and then dump at the end.
data = []
# Writing the latest into an JSON file
f = open(actfile, 'w')
for page in range (start,end):
print(page)
# Get activities
url = "https://www.strava.com/api/v3/activities?access_token="
pstr = '&page={}&per_page={}'.format(page,per_page)
res = requests.get(url + strava_tokens['access_token'] + pstr)
data = res.json()
# Write activities json to my activities json file
json.dump(data,f)
f.close()
# return the last page of activities as an array of JSON dicts
return data
###########################
if __name__ == '__main__':
tokens = get_tokens()
get_activities(tokens)