Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KonstantinosSykas/dfe4c5e392e299ab9341d6e16299454f to your computer and use it in GitHub Desktop.
Save KonstantinosSykas/dfe4c5e392e299ab9341d6e16299454f to your computer and use it in GitHub Desktop.
Download all workouts from sports-tracker
// based entirely on this blog post:
// http://druss.co/2016/04/export-all-workouts-from-sports-tracker/
// unfortunately the original script no longer works, moslty because jQuery is
// no longer available on sports-tracker pages.
//
// I've compiled the changes proposed in the comments in the script below.
// to use the script, login to your sports-tracker account
// change URL to http://www.sports-tracker.com/diary/workout-list
// open browser console (Cmd-Shift-I)
// paste the script, hit enter - it'll run and print something like this to the cosole:
// curl -o SportsTracker-<..id..>.gpx "http://www.sports-tracker.com/apiserver....."
// right-click on the colsole and save the contents to a file, call it download-all-workouts.sh
// open terminal, change to the directory where you saved the contents of the console
// edit the file - remove the javascript at the beginning of the file leaving only curl commands
// fix permissions:
// $>chmod +x download-all-workouts.sh
// run the script:
// $>./download-all-workouts.sh
// KonstantinosSykas 2020-04-21:
// (1) Server name for exports has changed from 'www.sports-tracker.com' to
// 'api.sports-tracker.com'.
// (2) Updated code to include activity type and date in the filename.
// (3) To export in .fit format, in the URL path replace the 'exportGpx' with
// 'exportFit' (without quotes) and replace the filename suffix '.gpx'
// with '.fit' accordingly.
// (4) All of the above tested successfully on Win10/PowerShell with Chrome.
var key = "sessionkey=";
var valueStartIndex = document.cookie.indexOf(key) + key.length;
const token_len = 32;
var token = document.cookie.substr(valueStartIndex, token_len);
function downloadOne(item) {
// Get activity type
var activities = ["Walking", "Running", "Cycling", "CrossCountrySkiing", "Other1", "Other2", "Other3", "Other4", "Other5", "Other6", "MountainBiking", "Hiking", "RollerSkating", "AlpineSkiing", "Paddling", "Rowing", "Golf", "Indoor", "Parkour", "BallGames", "OutdoorGym", "PoolSwimming", "TrailRunning", "Gym", "NordicWalking", "HorsebackRiding", "Motorsports", "Skateboarding", "WaterSports", "Climbing", "Snowboarding", "SkiTouring", "FitnessClass", "Soccer", "Tennis", "Basketball", "Badminton", "Baseball", "Volleyball", "AmericanFootball", "TableTennis", "RacquetBall", "Squash", "Floorball", "Handball", "Softball", "Bowling", "Cricket", "Rugby", "IceSkating", "IceHockey", "Yoga", "IndoorCycling", "Treadmill", "Crossfit", "Crosstrainer", "RollerSkiing", "IndoorRowing", "Stretching", "TrackAndField", "Orienteering", "StandupPaddling", "MartialArts", "Kettlebell", "Dancing", "SnowShoeing", "Frisbee", "Futsal", "Multisport", "Aerobics", "Trekking", "Sailing", "Kayaking", "CircuitTraining", "Triathlon", "Undefined1", "Cheerleading", "Boxing", "ScubaDiving", "FreeDiving", "AdventureRacing", "Gymnastics", "Canoeing", "Mountaineering", "TelemarkSkiing", "OpenwaterSwimming", "Windsurfing", "KitesurfingKiting", "Paragliding", "Undefined2", "Snorkeling", "Surfing", "Swimrun", "Duathlon", "Aquathlon", "ObstacleRacing", "Fishing", "Hunting"];
var activity_type_id = item.children[0].attributes[1].value;
var activity_type = activities[activity_type_id];
// Get activity date and convert it to YYYYMMDD format
var activity_summary = item.innerText.substr(item.innerText.indexOf('\n') + 1);
var activity_date= activity_summary.substr(0, activity_summary.indexOf('\n'));
var activity_year = activity_date.substr(activity_date.lastIndexOf(',') + 2, 4);
var activity_month = '00';
switch(activity_date.substr(0, 3)) {
case 'Jan':
activity_month = '01';
break;
case 'Feb':
activity_month = '02';
break;
case 'Mar':
activity_month = '03';
break;
case 'Apr':
activity_month = '04';
break;
case 'May':
activity_month = '05';
break;
case 'Jun':
activity_month = '06';
break;
case 'Jul':
activity_month = '07';
break;
case 'Aug':
activity_month = '08';
break;
case 'Sep':
activity_month = '09';
break;
case 'Oct':
activity_month = '10';
break;
case 'Nov':
activity_month = '11';
break;
case 'Dec':
activity_month = '12';
break;
}
var activity_day = activity_date.substr(4, 2);
var twodigits = activity_day.lastIndexOf(',') - 1;
if (!twodigits) {
activity_day = '0' + activity_day[0];
}
activity_date = activity_year + activity_month + activity_day;
var href = item.href;
var id = href.substr(href.lastIndexOf('/') + 1, 24);
var url = 'http://api.sports-tracker.com/apiserver/v1/workout/exportGpx/' + id + '?token=' + token;
var filename = 'SportsTracker-' + activity_type + '-' + activity_date + '-' + id + '.gpx';
console.log('curl -o ' + filename + ' "' + url + '";sleep 2');
}
function loopThroughItems(items)
{
var i = 0;
for (i = 0; i < items.length; i++) {
downloadOne(items[i]);
}
}
var items = document.querySelectorAll("ul.diary-list__workouts li a");
document.body.innerHtml = '';
loopThroughItems(items);
@bobika99
Copy link

Has anyone tried running the script lately? I'm getting a 403 error.

@KonstantinosSykas
Copy link
Author

KonstantinosSykas commented Aug 30, 2022

Hi @bobika99 , I just tried it successfully on a Win10 Pro (21H1) using Chrome Version 104.0.5112.102 (Official Build) (64-bit).

EDIT: I succesfully executed the curl command in Windows PowerShell and visually checked the .gpx at GPS Visualizer online service.

@KonstantinosSykas
Copy link
Author

KonstantinosSykas commented Aug 31, 2022

Hi @bobika99,

I thought a bit more on that 403 error and a scenario came up where your case could have something to do with exceeded quota while hitting the sports-tracker api server or service.

What you could do, if that's the case, is to avoid exporting all your workouts at once. Split them in smaller groups and execute them one group at a time (or execute each group during different hours in a day, or even next day; I don't know how the quota could be configured by the sports-tracker web admin).

I tried to simulate the case exporting a larger group of workouts. There was no problem exporting 364 of them (sorry, I don't have more to test). I wonder how many workouts you are trying to export or if you have fiddled a bit with the sleep command of the script.

Drop a note to share the outcome of your attempts.

@bobika99
Copy link

I'm sorry, but it was my fault. I used the command line instead of powershell.

@KonstantinosSykas
Copy link
Author

@bobika99 excellent news! Have a great day! 🥳

@DeR-RoN
Copy link

DeR-RoN commented Mar 1, 2023

Worked like a charm with Chrome under Win11 Pro 22H2! Thank you very much! Now that Sports Tracker has gone Premium on all of their usefull features its time finally export all my workouts and move on. 😄

EDIT: With 450+ workouts, I copy-pasted 100 lines directly into the powershell prompt, executed them, and repeated the process until all workouts were downloaded.

@KonstantinosSykas
Copy link
Author

@DeR-RoN it's good that this still works. 💪

@alexvdvalk
Copy link

if you're getting 307 redirects, add -L to your commands. Example:

curl -L -o SportsTracker-Walking-.gpx "http://api.sports-tracker.com/apiserver/v1/workout/exportGpx/?token=";sleep 2

@mjb101
Copy link

mjb101 commented Dec 8, 2024

Thanks very much for your code. Now I am able to download all my workouts which is great.
I am a novice here, trying to understand the code.
Is there a command (or short script) I can use to retrieve a dictionary of all available Sports Tracker activity ids and their corresponding names.
I see you have a correctly ordered activity list 'activities' in your code, but how did you create it?

@mjb101
Copy link

mjb101 commented Dec 9, 2024

Further to my comment yesterday...

From manually inspecting the website html, there seem to be a few changes the activity list.
From what I can tell:

activity id number 75 is now 'Padel' (was previously 'Undefined1') - this uses the same activity icon as Tennis, ie icon number 34
activity id number 99 is 'Gravel Cycling' - this uses the activity icon as Cycling, ie icon number 2
there is a new activity named 'Transition' - don't know it's id
I presume there is a new activity id number 98

Ideally there is a command that can be used to definitively retrieve all activity names and their id numbers and their icon numbers.

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment