Skip to content

Instantly share code, notes, and snippets.

@cimm cimm/train.rb
Last active Sep 4, 2015

Embed
What would you like to do?
Gets the NMBS/SNCB trains leaving from a given station in Belgium.
#!/usr/bin/env ruby
require 'rubygems'
require 'cgi'
require 'open-uri'
require 'hpricot'
current_time = Time.now.strftime("%H:%M")
url = ""
user_agent = "Mozilla/5.0 (X11; U; CrOS i686 9.10.0; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.253.0 Safari/532.5"
if Time.parse("17:00") < Time.now && Time.now < Time.parse("18:00")
railtime = Hpricot(open(url, "User-Agent" => user_agent))
puts "Next trains leaving from Louvain-la-Neuve"
puts ""
trains = railtime.search("/html/body/form/table/tr")
trains.each do |train|
departure = train.search("td//a").inner_html
status = " "
if !train.search("td//i").empty?
status = "X"
end
detail = train.search("td[span]").inner_html.gsub(/<\/?[^>]*>/, "").gsub(/&nbsp;/, " ")
puts "#{departure} #{status} #{detail}"
end
else
puts "Off"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.