Skip to content

Instantly share code, notes, and snippets.

@paulkaplan
Created October 30, 2011 03:39
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 paulkaplan/1325436 to your computer and use it in GitHub Desktop.
Save paulkaplan/1325436 to your computer and use it in GitHub Desktop.
Track UChicago local bus system using defined 'stops'
# TODO html/jquery for getting all the points on the route
require 'yaml'
require 'json'
require 'open-uri'
require 'redis'
ROUTE = 1000299
ERROR = 0.0001 #This is radial wiggle room for arriving buses, in lat/lng
def get_data(route)
vehicles_url = "http://api.transloc.com/1.0/vehicles.json?agencies=100"
vehicles_data = open("http://api.transloc.com/1.0/vehicles.json?agencies=100").read
data = JSON.parse(vehicles_data)["data"]["100"]
proper_route = data.collect { |x| x["route_id"] == "#{ROUTE}" ? x : nil}.compact[0]
end
class Stop
attr_accessor :name, :latitude, :longitude
def initialize args
args.each do |k, v|
instance_variable_set("@#{k}", v) unless v.nil?
end
end
def arrived?
vehicle_data = get_data ROUTE
(vehicle_data["location"]["lat"] - self.latitude).abs < ERROR and (vehicle_data["location"]["lng"] - self.longitude).abs < ERROR
end
end
# Load em up, stopify, collect
stops = YAML::load(File.open('central.yml')).collect {|s| Stop.new(s) }
# And a redis server for the data
redis = Redis.new
stop_number = 0
# wait for the bus to reach the initial stop
puts "Waiting to begin the magical journey (ie for the bus to reach a stop)"
while !stops[stop_number].arrived?
sleep 1
end
while true
start_route_time = Time.now
until stops[stop_number+1].arrived?
puts "... Been traveling for #{(Time.now - start_route_time).to_i} seconds"
sleep 1
end
route_time = Time.now - start_route_time
puts "Bus made it to the next stop"
unless stop_number == 0
redis.set "#{stop_number}", JSON.parse(redis.get "#{stop_number}") << route_time
else redis.set "#{stop_number}", [route_time].to_json
end
stop_number = (stop_number + 1)%stops.count
end
@paulkaplan
Copy link
Author

This and all my other work on transloc api has been moved here https://github.com/paulkaplan/Transloc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment