Created
March 14, 2015 15:22
-
-
Save matthewpizza/9401d641f60397b778af to your computer and use it in GitHub Desktop.
Batch upload gpx files to Strava
This file contains hidden or 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
var _ = require( 'underscore' ); | |
var fs = require( 'fs' ); | |
var strava = require( 'strava-v3' ); | |
var logFile = 'data/log.txt'; | |
var requestCount = 1; | |
var runsPath = '../runs/gpx/'; | |
fs.readdir( runsPath, function ( err, files ) { | |
if ( err ) { | |
return console.log( err ); | |
} | |
var exclude = fs.readFileSync( logFile, 'utf8' ).split( '\n' ); | |
files = _.difference( files, exclude ); | |
files.forEach( function ( file, index, array ) { | |
if ( requestCount > 50 ) { | |
return false; | |
} | |
new UploadFile( file ); | |
requestCount++; | |
} ); | |
} ); | |
function UploadFile( file ) { | |
this.file = file; | |
console.log( 'Uploading ' + this.file ); | |
strava.uploads.post( { | |
activity_type: 'run', | |
data_type: 'gpx', | |
file: runsPath + this.file, | |
statusCallback: function ( err, payload ) { | |
if ( err ) { | |
console.log( this.file + ': ' ); | |
return console.log( err ); | |
} | |
return console.log( this.file + ': ' + payload.status ); | |
}.bind( this ) | |
}, function ( err, payload ) { | |
if ( err ) { | |
console.log( this.file + ': ' ); | |
return console.log( err ); | |
} | |
this.record(); | |
return console.log( this.file + ': ' + payload.status ); | |
}.bind( this ) ); | |
} | |
UploadFile.prototype.record = function () { | |
fs.appendFile( logFile, this.file + '\n', function ( err ) { | |
if ( err ) { | |
console.log( this.file + ': ' ); | |
return console.log( err ); | |
} | |
return console.log( this.file + ': Successfully saved.' ); | |
}.bind( this ) ); | |
}; |
This file contains hidden or 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
{ | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"license": "MIT", | |
"dependencies": { | |
"strava-v3": "^1.7.0", | |
"underscore": "^1.8.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment