Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Download all workouts from sports-tracker
// based on this blog post:
// http://druss.co/2016/04/export-all-workouts-from-sports-tracker/
// then based on this gist implementation:
// https://gist.github.com/anonymous/9abc8d9c376bbc6aa853b477a50e8932
//
// I've changed it so you don't need to do the "Load More Workouts" (it is downloaded through the API)
// Added possible customizations (easy to switch between curl and powershell)
// Get the url to use from the current document
// Fix the token parsing, it failed because there is no trailing ';'
// Added a # token at the end of the download command so you don't need to post-process when using firefox
//
// to use the script, login to your sports-tracker account in the browser
// 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 console and save the contents to a file, call it download-all-workouts.sh (if using curl), or
// call it download-all-workouts.ps1 if using PowerShell.
// 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/Invoke-WebRequest commands
// - remove the debugger console extras (on my Firefox every line has a trailing ' debugger eval code:..:..' I need to remove.)
// On linux: fix permissions:
// chmod +x download-all-workouts.sh
// run the script:
// - bash: ./download-all-workouts.sh
// - powershell: PowerShell.exe -ExecutionPolicy RemoteSigned -File ./download-all-workouts.ps1
// change this function to change from PowerShell to curl.
function getDownloadStatement(url, filename) {
//return 'curl -o ' + filename + ' "' + url + '";sleep 2;#'
return 'Invoke-WebRequest -Uri ' + ' "' + url + '" -OutFile "' + filename + '" ; Start-Sleep -s 1;# '
}
function commentOnWorkout(workout) {
console.log('# ' + workout.activityId + ' ' + new Date(workout.startTime).toString() + ' ' + workout.description);
}
var key = "sessionkey=";
var valueStartIndex = document.cookie.indexOf(key);
if (valueStartIndex < 0) throw "key '" + key +"' not found in cookie '"+document.cookie+"'";
valueStartIndex += key.length;
var valueEndIndex = document.cookie.indexOf(';', valueStartIndex);
if (valueEndIndex < 0) valueEndIndex = document.cookie.length;
var token = document.cookie.substring(valueStartIndex, valueEndIndex);
//console.log('key: ' + key + '\tvsi: ' + valueStartIndex + '\ttok: ' + token + '\tcki: ' + document.cookie);
//'https://sports-tracker.com/apiserver/v1/'
var apiUrlV1 = window.location.protocol + '//' + window.location.hostname + '/apiserver/v1/'
//console.log(apiUrlV1);
function downloadWorkoutsMetadata() {
var url = apiUrlV1 + 'workouts?limited=true&limit=3&token=' + token;
var filename = 'SportsTracker-workout-list.json';
console.log(getDownloadStatement(url, filename));
return request(url, (xhr) => {
xhr.timeout = 5000;
xhr.responseType = 'json';
});
}
function downloadOne(id) {
var url = apiUrlV1 + 'workout/exportGpx/' + id + '?token=' + token;
var filename = 'SportsTracker-' + id + '.gpx';
console.log(getDownloadStatement(url, filename));
}
function request(url, customizations) {
return new Promise(function(resolve, reject) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(xhr.response);
} else {
reject(xhr.status);
}
}
}
xhr.ontimeout = function () {
reject('timeout')
}
xhr.open('get', url, true);
if (customizations) customizations(xhr);
xhr.send();
});
}
async function doIt() {
const allWorkouts = await downloadWorkoutsMetadata();
console.log(allWorkouts);
if (allWorkouts && allWorkouts.payload && allWorkouts.payload.length > 0) {
var workouts = allWorkouts.payload;
var i;
for (i = 0; i < workouts.length; i++) {
commentOnWorkout(workouts[i]);
downloadOne(workouts[i].workoutKey);
}
} else {
console.log('error downloading workouts');
}
}
doIt();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.