Created
August 16, 2013 04:31
-
-
Save bmander/6247345 to your computer and use it in GitHub Desktop.
Poll the nextbus api and log entries into a mongodb database
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
var http = require('http'); | |
var xml2js = require('xml2js'); | |
var mongodb = require('mongodb') | |
// get mongdb handle | |
var db = new mongodb.Db('test', new mongodb.Server("127.0.0.1", 27017, {})) | |
// keep track of the last time we got a response from nextbus | |
var lastTime = 0; | |
// setInterval callback | |
function createPollNextbusCallback(coll) { | |
var pollNextbus = function(){ | |
var parser = new xml2js.Parser() | |
var options = { | |
host:'webservices.nextbus.com', | |
path:'/service/publicXMLFeed?command=vehicleLocations&a=sf-muni&t='+lastTime | |
} | |
var req = http.request( options, function(res){ | |
var data = ""; | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
data += chunk; | |
}); | |
res.on('end', function () { | |
try{ | |
parser.parseString( data, function( err, result ){ | |
if( !result || !result.hasOwnProperty('body') ){ | |
return; | |
} | |
lastTime = Number( result.body.lastTime[0].$.time ); | |
if( result.body.hasOwnProperty('vehicle') ){ | |
var vehicles = result.body.vehicle; | |
console.log( vehicles.length + " vehicles" ); | |
for(var i=0; i<vehicles.length; i++){ | |
vehicle = vehicles[i].$; | |
vehicle.time = lastTime; | |
if( vehicle.hasOwnProperty('lat') && vehicle.hasOwnProperty('lon') ){ | |
vehicle.loc = [Number(vehicle.lat),Number(vehicle.lon)]; | |
vehicle.lat = Number(vehicle.lat); | |
vehicle.lon = Number(vehicle.lon); | |
} | |
coll.insert( vehicle ); | |
} | |
} | |
console.log( "lastTime: " + lastTime ); | |
console.log( "---" ); | |
}); | |
} catch( err ) { | |
console.log( "exception caught: "+err ); | |
} | |
}); | |
}); | |
req.end() | |
req.on('error', function(e) { | |
console.time( "something went wrong with the request: " + e.message ); | |
}); | |
} | |
return pollNextbus | |
} | |
db.open( function( err, p_client ) { | |
var coll = db.collection( "nextbus_geoms" ); | |
setInterval( createPollNextbusCallback(coll), 2000 ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment