Skip to content

Instantly share code, notes, and snippets.

@josqu4red
Last active December 29, 2015 08:49
Show Gist options
  • Save josqu4red/7646560 to your computer and use it in GitHub Desktop.
Save josqu4red/7646560 to your computer and use it in GitHub Desktop.
Retrieve Paris RER A/B schedule from your terminal
require 'uri'
require 'net/http'
require 'net/https'
require 'nokogiri'
def get_stations(line)
uri = URI("http://www.ratp.fr/horaires/fr/ratp/rer/choix-arret-direction/#{line}")
response = Net::HTTP.get_response(uri)
if response.is_a? Net::HTTPSuccess
doc = Nokogiri::HTML(response.body)
rows = doc.xpath("//select[@id='rerArretDirectionForm_station']/option/@value")
else
"Error retrieving stations list: #{response.message} (#{response.code})"
end
end
def get_schedule(line, station, terminus)
uri = URI("http://www.ratp.fr/horaires/fr/ratp/rer/prochains_passages/#{line}/#{station.gsub(/ /, '+')}/#{terminus}")
response = Net::HTTP.get_response(uri)
if response.is_a? Net::HTTPSuccess
doc = Nokogiri::HTML(response.body)
rows = doc.xpath("//div[@id='prochains_passages']//tbody/tr")
rows.each do |row|
puts "%4s | %-26s | %s" % row.xpath("./td").map{|col| col.content.strip}
end
elsif response.is_a? Net::HTTPFound
raise "Real-time service information not available"
else
raise "Error retrieving service information: #{response.message} (#{response.code})"
end
end
terminus = {
"RA" => {
"A" => "St-Germain-en-Laye/Poissy/Cergy",
"R" => "Boissy-St-Léger/Marne-la-Vallée",
},
"RB" => {
"A" => "Charles-de-Gaulle/Mitry-Claye",
"R" => "Robinson/Saint-Rémy-lès-Chevreuse",
}
}
header = <<-EOF
--------------------------------------------------------
Name | Terminus | Time
--------------------------------------------------------
EOF
begin
puts "From Arcueil Cachan, Terminus #{terminus["RB"]["A"]}"
puts header
get_schedule("RB", "Arcueil Cachan", "A")
puts
puts "From Chatelet, Terminus #{terminus["RB"]["R"]}"
puts header
get_schedule("RB", "Chatelet", "R")
rescue Exception => e
puts e.message
end
#puts get_stations("RA")
$ ruby rer.rb
From Arcueil Cachan, Terminus Charles-de-Gaulle/Mitry-Claye
--------------------------------------------------------
Name | Terminus | Time
--------------------------------------------------------
EMUR | Aeroport Ch.De Gaulle 2 | Train à quai
ILOU | Mitry-Claye | 19:52
EBOI | Aeroport Ch.De Gaulle 2 | Train sans arrêt
EMUR | Aeroport Ch.De Gaulle 2 | 20:02
IDRE | Mitry-Claye | Train sans arrêt
ILOU | Mitry-Claye | 20:00
From Chatelet, Terminus Robinson/Saint-Rémy-lès-Chevreuse
--------------------------------------------------------
Name | Terminus | Time
--------------------------------------------------------
KUNE | Massy Palaiseau | Train à l'approche
SOUS | Robinson | 19:52
PAKE | Saint Remy les Chevreuse | 19:54
LEVI | Orsay Ville | 19:52
KUNE | Massy Palaiseau | 19:58
SOUS | Robinson | 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment