Skip to content

Instantly share code, notes, and snippets.

@gudmundur
Created January 28, 2012 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gudmundur/1696305 to your computer and use it in GitHub Desktop.
Save gudmundur/1696305 to your computer and use it in GitHub Desktop.
Strætó Rauntímakort
qs = require 'querystring'
request = require 'request'
cradle = require 'cradle'
http = require 'http'
client = new cradle.Connection
db = client.database 'bus-realtime'
url = "http://www.straeto.is/bitar/bus/livemap/json.jsp"
activeRoutes = (callback) ->
request url, (err, res, body) ->
try
obj = JSON.parse body
callback null, (r.id for r in obj.routes)
catch error
callback error
fetchLocations = (routes, callback) ->
params = qs.encode { routes: routes.join ',' }
request "#{url}?#{params}", (err, res, body) ->
try
obj = JSON.parse body
obj.date = new Date
db.save obj, callback
catch error
callback error
tick = ->
activeRoutes (err, routes) ->
fetchLocations routes, (err, res) ->
if err
return console.log err
console.log res
tick()
setInterval(tick, 15000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment