Skip to content

Instantly share code, notes, and snippets.

@pascalopitz
Last active March 22, 2018 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pascalopitz/d7f271dc69ab55d9c0587a93cd70e634 to your computer and use it in GitHub Desktop.
Save pascalopitz/d7f271dc69ab55d9c0587a93cd70e634 to your computer and use it in GitHub Desktop.
Strava TCX crawling
STRAVA_CLIENT_SECRET=Your client Secret
STRAVA_ACCESS_TOKEN=Your Access Token
STRAVA_CLIENT_ID=Your client ID
STRAVA_REDIRECT_URI=Something you won't need
COOKIE=Cookie from website
require('dotenv').config();
const fs = require('fs');
const strava = require('strava-v3');
const fetch = require('node-fetch');
const moment = require('moment');
function listActivitiesAsync() {
return new Promise((resolve, reject) => {
strava.athlete.listActivities({
per_page: 200
},function(err,payload,limits) {
if(!err) {
resolve(payload);
}
else {
reject(err);
}
});
})
}
function sleep(time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time);
});
}
const FORMAT = 'YYYY-MM-DD';
async function main() {
const activities = await listActivitiesAsync();
const activityIds = activities.map(a => a.id);
await Promise.all(activities.map(async a => {
const {id, start_date, name} = a;
try {
const response = await fetch(`https://www.strava.com/activities/${id}/export_tcx`, {
headers: {
Cookie: process.env.COOKIE
}
});
const disp = response.headers.get('content-disposition');
const [ , token] = String(disp).split('"');
const fileName = `./downloads/${moment(start_date).format(FORMAT)}-${token}`;
fs.writeFileSync(fileName, await response.buffer());
await sleep(1000);
} catch(e) {
console.error(e);
}
}));
}
main();
{
"name": "strava-tcx-export",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"dotenv": "^5.0.1",
"moment": "^2.21.0",
"node-fetch": "^2.1.1",
"strava-v3": "^1.14.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment