Skip to content

Instantly share code, notes, and snippets.

@miccolis
Created May 20, 2012 22:07
Show Gist options
  • Save miccolis/2759712 to your computer and use it in GitHub Desktop.
Save miccolis/2759712 to your computer and use it in GitHub Desktop.
WMATA NextBus script
#!/usr/bin/env node
/**
* Gets the number of minutes before a bus shows up at a particular bus stop.
*
* Usage: ./nextbus.js API-KEY STOP-ID
*/
// Since WMATA uses Mashery you need to get a Mashery account, and then register
// your application. See http://developer.wmata.com/
var apiKey = process.argv[2];
// WMATA has an ID for every stop. You can get these off the stops themselves or
// get them from http://www.wmata.com/nextbus
var stopId = process.argv[3];
var opts = {
host: 'api.wmata.com',
path: '/NextBusService.svc/json/JPredictions?StopID='+ stopId +'&api_key='+ apiKey
};
var http = require('http');
http.get(opts, function(res) {
if (res.statusCode != 200) {
console.warn('Got %d from wmata', res.statusCode);
process.exit(1);
}
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function() {
data = JSON.parse(data);
if (data.Predictions.length) {
console.log(data.Predictions[0].Minutes);
}
else {
console.log('No approaching vehicles.');
}
});
}).on('error', function(err){
console.warn(err)
process.exit(1)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment