Create Strava account on www.strava.com and add/track at least one activity
Log in and create an API application on https://www.strava.com/settings/api, fill out a form, use ālocalhostā as the Authorization Callback Domain
You will get API Application, go to My API Application (hover over your profile icon in the top tight corner and go to settings). If you donāt see this as an option in the navigation bar, go back to https://www.strava.com/settings/api and make sure the application was set up.
You want to record Client ID and Client Secret
You will need access token to get your data ( Unfortunately, we canāt use the one on this page. That is because the provided tokens give us a scope/access level of āread.ā The way Strava has set up their authorization, we need a scope of āread allā to work with our activities.)
Replace your_client_id with your actual client id in the following link. Then paste it into your browser search bar and hit enter. You will see something like a screen below, click on Authorize button
Your browser will probably error. Thatās ok. Within this error, we actually get the piece of information that we want from Strava. Url should now look like:
Copy down whatever the code is between code= and the & sign (where my snippet says āsomecodeā).
Copy the code below from our notebook and in auth_url (line 5) use your client_id from strava API application (replace your_client_id), your client_secret from strava API application (replace your_client_secret) and your access token from step 7 (replace your_acces_token)
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
auth_url = "https://www.strava.com/oauth/token?client_id=your_client_id&client_secret=your_client_secret&code=your_acces_token&grant_type=authorization_code"
activites_url = "https://www.strava.com/api/v3/athlete/activities"
res = requests.post(auth_url, data=payload, verify=False)
resJson = res.json()
access_token = resJson['access_token']
header = {'Authorization': 'Bearer ' + access_token}
param = {'per_page': 200, 'page': 1}
my_dataset = requests.get(activites_url, headers=header, params=param).json()
import pandas as pd
from pandas.io.json import json_normalize
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime
activities = json_normalize(my_dataset)
#Create new dataframe with only columns I care about
cols = ['name', 'type', 'distance', 'moving_time', 'average_speed', 'total_elevation_gain']
activities = activities[cols]
# Uncomment line below and select number instead of 50 how much activities do you want to show
activities.head(50)
DO NOT FORGET You have to refresh your access token any time you call it (execute the code) (you will go to step 6 again), but you can just download your data and upload the file to project instead by clicking on arrow on right down corner under the table.
#import libraries
import pandas as pd
import numpy as np
#load data
df = pd.read_csv("strava_data.csv")
df
# Renaming the column names
df = df.rename(columns={ 'name' : 'day' , 'type' : 'activity' })
df.head(5)
# Box Plot HP
import plotly.express as px
fig = px.box(df['distance'])
fig.show()