Skip to content

Instantly share code, notes, and snippets.

@jessedc
Created January 5, 2012 11:02
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 jessedc/1564751 to your computer and use it in GitHub Desktop.
Save jessedc/1564751 to your computer and use it in GitHub Desktop.
Sydney City Rail service updates to JSON
require 'nokogiri'
require 'open-uri'
require 'json'
METRO_URL = 'http://www.cityrail.info/service_updates'
doc = Nokogiri::HTML(open(METRO_URL).read)
lines = []
line_row2 = doc.xpath("//tbody[@id='serviceStatusLines:j_id631:tbody_element']/tr").each do |line_row|
tds = line_row.xpath('.//td')
lines.push({
:line => tds.first.text.gsub(/[\n\t\r]/,''),
:status => tds.last.text.gsub(/[\n\t\r]/,'')
})
end
File.open('cityrail_lines.json', 'w+') {|f| f.write(JSON::generate(lines))}
puts lines
# Output
# {:line=>"Eastern Suburbs & Illawarra Line", :status=>"Good service"}
# {:line=>"Bankstown Line", :status=>"Trackwork"}
# {:line=>"Inner West Line", :status=>"Good service"}
# {:line=>"Airport & East Hills Line", :status=>"Good service"}
# {:line=>"South Line", :status=>"Trackwork"}
# {:line=>"Cumberland Line", :status=>"Trackwork"}
# {:line=>"North Shore Line", :status=>"Good service"}
# {:line=>"Western Line", :status=>"Good service"}
# {:line=>"Carlingford Line", :status=>"Good service"}
# {:line=>"Olympic Park services", :status=>"Good service"}
# {:line=>"Northern Line", :status=>"Good service"}
# {:line=>"South Coast Line", :status=>"Good service"}
# {:line=>"Southern Highlands Line", :status=>"Good service"}
# {:line=>"Blue Mountains Line", :status=>"Good service"}
# {:line=>"Newcastle & Central Coast Line", :status=>"Good service"}
# {:line=>"Hunter Line", :status=>"Good service"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment