Skip to content

Instantly share code, notes, and snippets.

@j-manu
Created November 17, 2011 22:26
Show Gist options
  • Save j-manu/1374753 to your computer and use it in GitHub Desktop.
Save j-manu/1374753 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
require 'rubygems'
require 'cgi'
require 'mechanize'
def check_pnr(pnr1,pnr2)
agent = WWW::Mechanize.new
page = agent.get 'http://www.indianrail.gov.in/pnr_stat.html'
form = page.forms[0]
form.lccp_pnrno1 = pnr1
form.lccp_pnrno2 = pnr2
page = agent.submit(form)
tds = page.search("//table")[4].search("//tr")[2].search("//td")
puts "Train:" + tds[2].innerHTML + " Date:" + tds[3].innerHTML + " From:" + tds[4].innerHTML + " To:" + tds[5].innerHTML + "\n"
trs = page.search("//table[@id='center_table']").search("//tr")
(3..trs.size-3).each do |index|
tr = trs[index]
tds = tr.search("//td")
puts "passenger #{index -2}\t" + clean(tds[1].innerHTML) + "\t" + clean(tds[2].innerHTML) + "\n"
end
puts "\n\n"
end
def clean(chunk)
chunk.gsub("<strong>","").gsub("</strong>","")
end
# Enter the PNRs to check, in this array
pnrs = ["4301157213","4101137451"]
pnrs.each do |pnr|
check_pnr(pnr[0..2],pnr[3..-1])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment