Skip to content

Instantly share code, notes, and snippets.

@arest
Created June 5, 2020 20:24
Show Gist options
  • Save arest/3c9ea54c0d0fbf584319b5bd69aac767 to your computer and use it in GitHub Desktop.
Save arest/3c9ea54c0d0fbf584319b5bd69aac767 to your computer and use it in GitHub Desktop.
Import activity from Polar swatch and upload to Strava
#!/usr/bin/env bash
: "${POLAR_DIR:=$HOME/Sites/polar}"
: "${DOWNLOAD_DIR:=$HOME/Downloads}"
: "${DEVICE_ID:=}"
: "${CLIENT_ID:=}"
: "${CLIENT_SECRET:=}"
: "${REFRESH_TOKEN:=}"
echo "Date?"
read date
echo "Activity type (Possible values : ride, run, swim, workout, hike, walk, nordicski, alpineski, backcountryski, iceskate, inlineskate, kitesurf, rollerski, windsurf, workout, snowboard, snowshoe, ebikeride, virtualride) ?"
read activity_type
echo "Activity Name?"
read activity_name
echo "Activity Description?"
read activity_description
if [ -z "$date" ]
then
date=$(date '+%Y%m%d')
fi
if [ -z "$activity_type" ]
then
activity_type="ebikeride"
fi
if [ -z "$activity_name" ]
then
activity_name="Morning Ride"
fi
ruby $POLAR_DIR/polar_ftp SYNC
basedir="$HOME/Polar/${DEVICE_ID}/U/0/${date}/E/"
subdir=$(find $basedir -maxdepth 1 -mindepth 1 -type d)
delimiter=/
s=$subdir$delimiter
array=();
while [[ $s ]]; do
array+=( "${s%%"$delimiter"*}" );
s=${s#*"$delimiter"};
done;
filename="$DOWNLOAD_DIR/training${date}.gpx"
declare -p array
ruby $POLAR_DIR/polar_training2gpx $HOME/Polar/${DEVICE_ID}/U/0/${date}/E/${array[-1]} $filename
access_token=$(curl --silent -X POST "https://www.strava.com/oauth/token?client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&refresh_token=${REFRESH_TOKEN}&grant_type=refresh_token" | jq -r '.access_token')
request_upload_id=$(curl --silent -X POST "https://www.strava.com/api/v3/uploads" \
-H "Authorization: Bearer ${access_token}" \
-F activity_type="${activity_type}" \
-F name="${activity_name}" \
-F description="${activity_description}" \
-F trainer=0 \
-F data_type="gpx" \
-F file=@$filename \
| jq -r '.id')
# wait for activity to finish
activity_id=$(curl --silent -G "https://www.strava.com/api/v3/uploads/${request_upload_id}" -H "Authorization: Bearer $access_token" | jq -r '.activity_id')
while [[ "$activity_id" == "null" ]]
do
echo "Waiting ${request_upload_id} upload to finish..."
sleep 10
activity_id=$(curl --silent -G "https://www.strava.com/api/v3/uploads/${request_upload_id}" -H "Authorization: Bearer $access_token" | jq -r '.activity_id')
done
echo "Activity ID ${activity_id}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment