Skip to content

Instantly share code, notes, and snippets.

@joer14
Last active September 23, 2017 01:48
Show Gist options
  • Save joer14/1105cb1dceebe0f5fb43ebfb4b6ab067 to your computer and use it in GitHub Desktop.
Save joer14/1105cb1dceebe0f5fb43ebfb4b6ab067 to your computer and use it in GitHub Desktop.
import requests
import json
import datetime
import time
from datetime import timedelta, date
import pandas as pd
'''
export all strava data to a csv
'''
BEARER_CODE = 'Bearer {put your code here}'
def getActivies(page_num):
url = 'https://www.strava.com/api/v3/athletes/3587629/activities'
header = {'Authorization':BEARER_CODE}
payload = {'page':page_num, 'per_page':200}
r = requests.get(url,headers=header,data=payload)
return r.content
all_activies_df = pd.DataFrame()
current_page_num = 0
current_page = None
while current_page != '[]':
print 'page num:', current_page_num
current_page = getActivies(current_page_num)
current_page_num += 1
tmp_df = pd.read_json(current_page)
all_activies_df = all_activies_df.append(tmp_df, ignore_index=True)
print 'len all act df', len(all_activies_df)
all_activies_df.to_csv('/Users/joe/projects/strava-7/all_activies_df.csv',index=False, encoding='utf-8')
import requests
import json
import datetime
import time
from datetime import timedelta, date
import pandas as pd
df = pd.read_csv('/Users/joe/projects/strava-7/all_activies_df.csv')
# filter to just runs
df = df[df['type']=='Run']
print len(df)
cols_I_want = ['total_elevation_gain', 'average_speed', 'distance', 'elapsed_time', 'start_date_local']
df = df[cols_I_want]
df['start_date_local'] = pd.to_datetime(df.start_date_local)
df = df.sort_values(by='start_date_local')
df2 = df.groupby([df['start_date_local'].dt.date])['distance','elapsed_time','total_elevation_gain'].sum()
new_idx = pd.date_range(df['start_date_local'].min(), df['start_date_local'].max(), normalize=True)
df2 = df2.reindex(new_idx, fill_value=0)
df2['ave_distance_past_7_days'] = pd.rolling_mean(arg=df2['distance'],window=7,min_periods=7,freq=None,center=False,how=None)
df2['total_distance_past_7_days'] = df2['distance'].rolling(window=7,min_periods=7).sum()
df = df2
del df['total_elevation_gain']
del df['elapsed_time']
df['date']=df.index
df = df[(df['date'] > '2016-01-01')]
print df.loc[df['total_distance_past_7_days'].idxmax()]
import numpy as np
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
labels = []
for col_name in df.columns.values:
print col_name
ax_s = []
if col_name != 'date':
ax = df.plot(ax=ax, kind='line', x='date', y=col_name, grid=True, markersize=10, style=['-'], figsize=(10,4))
labels.append(col_name)
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
plt.subplots_adjust(right=0.7)
plt.savefig("/Users/joe/projects/strava-7/output4.png")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment