Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bare bones Strava connector #843

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions Connectors/Strava/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var fs = require('fs'),
url = require('url'),
express = require('express'),
connect = require('connect'),
request = require('request'),
sys = require('sys');

var athleteId = 27487;

exports.rides = function(callback, startDate, endDate) {
if(startDate && !startDate.match(/\d{4}\-\d{2}\-\d{2}/)) {
callback("startDate must be in the format of YYYY-MM-DD");
return;
}
if(endDate && !endDate.match(/\d{4}\-\d{2}\-\d{2}/)) {
callback("endDate must be in the format of YYYY-MM-DD");
return;
}

//create data directory, if it doesn't exist
try {
fs.lstatSync("data");
} catch(e) {
fs.mkdirSync("data", "0777");
console.log("Created data directory.");
}

//go check
request({
uri:"http://www.strava.com/api/v1/rides?athleteId="+athleteId+(startDate ? "&startDate="+startDate : "")+(endDate ? "&endDate="+endDate : ""),
json: true,
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
for(var i=0; i<body.rides.length; i++) {
console.log("Retrieve ride info for id:"+body.rides[i].id+" name:"+body.rides[i].name);
store_ride(body.rides[i].id);
break; //for debugging, don't bother to continue
}
//callback(body);
}
})
}

function store_ride(id) {
request({
uri:"http://www.strava.com/api/v1/rides/"+id,
json: true,
}, function(error, response, the_ride) {
if (!error && response.statusCode == 200) {

//TODO: this is kind of wasteful, there's some duplicated data in the efforts and map_details

//retrieve map_details and put it in with ride
request({
uri:"http://www.strava.com/api/v1/rides/"+id+"/map_details",
json: true,
}, function(error, response, map_details) {
console.log(map_details);
if (!error && response.statusCode == 200)
the_ride.ride.map_details = map_details;
})

//retrieve efforts and put it in with ride
request({
uri:"http://www.strava.com/api/v1/rides/"+id+"/efforts",
json: true,
}, function(error, response, efforts) {
if (!error && response.statusCode == 200)
the_ride.ride.efforts = efforts;
})

//write the whole ride to the filesystem
console.log(the_ride);
fs.writeFileSync("data/ride_"+id+".json", JSON.stringify(the_ride.ride));
}
})
}
9 changes: 9 additions & 0 deletions Connectors/Strava/strava.connector
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"title":"Strava connector",
"action":"Retrieve cycling data from Strava.com",
"desc":"Retrieve cycling data from Strava.com",
"run":"node strava.js",
"status":"unstable",
"handle":"strava",
"provides":["store/strava"]
}
95 changes: 95 additions & 0 deletions Connectors/Strava/strava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
*
* Copyright (C) 2011, The Locker Project
* All rights reserved.
*
* Please see the LICENSE file for more information.
*
*/

/**
* web server/service to wrap interactions w/ GitHub API
*/

var fs = require('fs'),
url = require('url'),
express = require('express'),
connect = require('connect'),
request = require('request'),
sys = require('sys'),
strava = require('./lib'),
app = express.createServer(
connect.bodyParser(),
connect.cookieParser()),
locker = require('../../Common/node/locker.js'),
lfs = require('../../Common/node/lfs.js');

var me;

app.set('views', __dirname);
app.get('/', handleIndex);

var dapp=false;
function handleIndex(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
if(dapp)
{
res.end(fs.readFileSync(__dirname + '/ui/index.html'));
}else{
res.end(fs.readFileSync(__dirname + '/ui/init.html'));
}
}

app.get('/init', function(req, res) {
if(!req.param('athleteId') ) {
res.writeHead(400);
res.end('whats the creds yo?');
return;
}
console.log("initializing for athlete "+req.param('athleteId') );
fs.writeFileSync('athleteId.json', JSON.stringify({athleteId:req.param('athleteId')}));
res.writeHead(200, {'Content-Type': 'text/html'});
res.end("saved athleteId, <a href='./'>continue</a>");
});

app.get('/info', function(req, res) {
res.writeHead(200);
dapp.getAccountInfo(function (err, data) {
if (err) res.end('Error: ' + err)
else res.end(data.display_name + ', ' + data.email)
});
});

app.get('/save', function(req, res) {
if(!req.param('file')){
res.writeHead(400);
res.end('whats the file yo?');
return;
}
console.log("saving "+req.param('file') );
res.writeHead(200);
res.end("ok, background uploading...");
dapp.putFile(req.param('file'), '', function (err, data) {
if (err)
console.log("failed: "+err);
else
console.log("saved!");
});
});


var stdin = process.openStdin();
stdin.setEncoding('utf8');
stdin.on('data', function (chunk) {
var processInfo = JSON.parse(chunk);
locker.initClient(processInfo);
process.chdir(processInfo.workingDirectory);
lfs.readObjectFromFile('auth.json', function(auth) {
if(auth.token) dapp = new dbox(auth.key, auth.ksecret, auth.token, auth.tsecret);
app.listen(processInfo.port,function() {
var returnedInfo = {port: processInfo.port};
console.log(JSON.stringify(returnedInfo));
});
});

});
3 changes: 3 additions & 0 deletions Connectors/Strava/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var s=require("./lib");
console.log("loaded, calling rides");
s.rides( function(a){ console.log(a) });
4 changes: 4 additions & 0 deletions Connectors/Strava/ui/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html>
<h3>Strava is connected :)</h3>
<p><a href='./info'>info check</a>
</html>
6 changes: 6 additions & 0 deletions Connectors/Strava/ui/init.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<h3>Locker needs to know your athlete ID. To find it, click the Profile link from the <a href="http://app.strava.com/dashboard" target="_new">Strava dashboard</a>.</h3>
<form method="get" action="./init">
Enter your athlete ID: <input name="athleteId"><br>
<input type="submit" value="go">
</form>