The Python code in this folder use Strava's activities API. The main purpose is to retrieve personal activity and store it locally in order to do analysis and visualistion beyond what is offered by Strava. Second, the code also makes it possible to automatically update activities meeting certain criteria. This can be useful if you for instance want consistent naming of commuting activities.
send_strava.py
Uses Strava activities API to retrieve rides for a specified athlete for the last three days. Once the data is received, the module modifies the data structure and publishes the data to a MQTT topic. Contains loops so it runs repeatedly, e.g. in a container or through systemdsend_strava_manual.py
Same assend_strava.py
, execpt that it accepts a date range as input and runs only onceupdate_strava.py
Uses Strava activities API to update recent activities based on user defined criteriaoauth_helper.py
Uses Strava oauth endpoint to retrieve initial access tokenscreate-container-send-strava.sh
Sends commands to the Docker engine to create image and container and to run the container with thesend_strava.py
modulecreate-container-update-strava.sh
Sends commands to the Docker engine to create image and container and to run the container with theupdate_strava.py
modulesend-strava-requirements.txt
Information about Python packages to be included in the docker image for thesend_strava.py
moduleupdate-strava-requirements.txt
Information about Python packages to be included in the docker image for theupdate_strava.py
modulestrava_tokens.json
User specific information needed to authorize with Strava's APIsstrava_gear.json
User specific information needed to update gear in a given ridesend-strava.Dockerfile
Instructions to the docker daemon for building image for thesend_strava.py
moduleupdate-strava.Dockerfile
Instructions to the docker daemon for building image for theupdate_strava.py
module
Except oauth_helper.py
and send_strava_manual.py
, which are written to be run ad-hoc, its best to run the modules either through systemd or Docker. This will ensure that the scripts runs continously and you will also get some built-in error handling, e.g. automatic restart if something unexpected happens. The code in this folder assumes Docker engine is running.
I made this code to hone my Python skills and to experiment with Strava's API. As such there may be flaws and choices in the code that could represent issues with information security. Strava API accesses real data, i.e. actual activity data, so keep this in mind if you would like to use this code
- Copy the files in this folder to your environment. Best would be to clone the entire repository with Git, so you receive updates when the code is improved
- You must have access to Strava's activities API for these modules to work. Strava has a developer portal that provides all the necessary instructions to get started. In particular, you need to obtain credentials from Strava to get the oauth2 process going. This basically means that you need to complete a form available at Strava's developer portal and then use the info from that step to retrieve oauth tokens. Tekk Sparrow Program has made an excellent Youtube-tutorial on how to do this and even provided a script. You can also use the
oauth2_helper.py
script in this folder, which is directly based on the one written by Tekk Sparrow Programs. Strava also has a nice step-by-step guide that explains the process. - Modify
strava_tokens.json
with the info returned from the script you ran in the previous step. This is a one time process. Once this is done, the modules in this folder will use refresh tokens to automatically update your access token when it expires.
- Modify the shell script
create-container-send-strava.sh
according to suit your environment, e.g. with information about the MQTT broker, location of the oauth tokens etc - Run the shell script
create-container-send-strava.sh
. This will create a image and then a container based on that image and run the container
The send_strava_manual.py
script requires the same parameters as the container version of the script, but as the script only runs ad-hoc its easier to just input the parameters whenever you need to run the script, typically to retrieve a batch of Strava activities.
Overview of the main components
Both scripts send data to a MQTT topic and as such do not need to persist data themselves. MQTT is freely available and can for example be run as a Docker container Only creativity limits what is possible to do with those data, once they are published to the topic. For instance you could light up a led when a certain weekly distance is reached, or you could have another piece of code that subscribes to the topic, writes the data to MariaDB and visualises it in Grafana. Information about that code will follow later. But for now, here is an example of how that could look
- Find the section for user variables in
update_strava.py
, around line 127, and adjust as needed, e.g. with activity title, description and gear id - Update
strava_gear.json
with gear id and name of gear. The gear id can be found either through the Strava website, check the URL, or by inspecting the json response from the activities API - Run the shell script
create-container-update-strava.sh
. This will create a image and then a container based on that image and run the container