Skip to content

Instantly share code, notes, and snippets.

@micahbf
Created April 3, 2014 22:43
Show Gist options
  • Save micahbf/9964367 to your computer and use it in GitHub Desktop.
Save micahbf/9964367 to your computer and use it in GitHub Desktop.
Realtime Caltrain departures
#!/usr/bin/env ruby
require 'open-uri'
require 'nokogiri'
TRANSIT_TOKEN = 'b2b9c476-6788-4a14-857b-fa3e828f99d0'
STOP_CODE = '70091'
# San Mateo NB: 70091
# San Mateo SB: 70092
# Hayward Park NB: 70101
# Hayward Park SB: 70102
# Hillsdale NB: 70111
# Hillsdale SB: 70112
next_departures_url = "http://services.my511.org/Transit2.0/GetNextDeparturesByStopCode.aspx?token=#{TRANSIT_TOKEN}&stopcode=#{STOP_CODE}"
doc = Nokogiri::XML(open(next_departures_url))
next_departures = []
doc.xpath("//DepartureTime").each do |dt|
minutes = dt.text.to_i
type = dt.ancestors.find { |a| a.name == "Route" }.attribute("Name").text
next_departures << {:type => type, :minutes => minutes}
end
next_departures.sort_by! { |d| d[:minutes] }
next_departures.each do |d|
time = Time.now + d[:minutes] * 60
time_str = time.strftime("%-l:%M")
puts "#{time_str} #{d[:type].capitalize}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment