Skip to content

Instantly share code, notes, and snippets.

@matthewpizza
Created March 14, 2015 15:22
Show Gist options
  • Save matthewpizza/9401d641f60397b778af to your computer and use it in GitHub Desktop.
Save matthewpizza/9401d641f60397b778af to your computer and use it in GitHub Desktop.
Batch upload gpx files to Strava
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 ) );
};
{
"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