-
-
Save pascalopitz/d7f271dc69ab55d9c0587a93cd70e634 to your computer and use it in GitHub Desktop.
Strava TCX crawling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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