-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.py
50 lines (42 loc) · 1.8 KB
/
sync.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
import os
import csv
import requests
import conf
def sync(token):
headers = {'Authorization': "Bearer {0}".format(token)}
f = conf.ride_file
if not os.path.exists(f):
open(f, 'w')
ids = []
with open(f, 'r') as rides_file:
reader = csv.DictReader(rides_file)
for row in reader:
ids.append(int(row['id']))
mode = 'w' if os.stat(f).st_size == 0 else 'a'
with open(f, mode) as rides_file:
writer = csv.writer(rides_file, delimiter=',')
if mode == 'w':
writer.writerow(['id', 'title', 'dist', 'date', 'max', 'avg', 'net', 'gross', 'elev', 'polyline'])
page = 1
while True:
r = requests.get("https://www.strava.com/api/v3/athlete/activities?page={0}".format(page), headers=headers)
response = r.json()
if len(response) == 0:
break
else:
for activity in [a for a in response if a['type'] == 'Ride' and a['id'] not in ids]:
r = requests.get("https://www.strava.com/api/v3/activities/{0}?include_all_efforts=true"
.format(int(activity['id'])), headers=headers)
json = r.json()
_id = activity['id']
title = json['name']
dist = json['distance']
date = json['start_date_local']
_max = json['max_speed']
avg = json['average_speed']
net = json['moving_time']
gross = json['elapsed_time']
elev = json['total_elevation_gain']
polyline = json['map']['polyline']
writer.writerow([_id, title, dist, date, _max, avg, net, gross, elev, polyline])
page += 1