Skip to content

Instantly share code, notes, and snippets.

@cvlong
Created December 11, 2019 16:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvlong/adde425402c38fb2c05a30668968d857 to your computer and use it in GitHub Desktop.
Save cvlong/adde425402c38fb2c05a30668968d857 to your computer and use it in GitHub Desktop.
import datetime
import json
import requests
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient
from config import client_id, client_secret, refresh_token
def run():
# me = requests.get('https://www.strava.com/api/v3/athlete', headers=headers).json()
activities = requests.get(
'https://www.strava.com/api/v3/athlete/activities',
headers=format_header(refresh_token['activity_read']),
params={'per_page': 200, 'page': 1}
).json()
if len(activities) > 200:
# TODO throw an error and paginate results
pass
distance = 0
count = 0
for activity in activities:
if datetime.datetime.strptime(activity['start_date'], '%Y-%m-%dT%H:%M:%SZ') > datetime.datetime(2019, 1, 1) \
and activity['type'] == 'Ride':
distance += activity['distance']
count += 1
print(distance)
print(distance*0.000621371192)
print(count)
def format_header(token):
access_token = refresh_tokens(token)
return {'Authorization': f'Bearer {access_token}'}
def refresh_tokens(refresh_token):
params = {
'client_id': client_id,
'client_secret':client_secret,
'grant_type':'refresh_token',
'refresh_token':refresh_token
}
r = requests.post('https://www.strava.com/oauth/token', params=params)
response = json.loads(r.content)
return response['access_token']
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment