Created
May 14, 2011 14:18
-
-
Save oogali/972253 to your computer and use it in GitHub Desktop.
Things you code when your flight is delayed...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'nokogiri' | |
require 'net/http' | |
require 'uri' | |
GATETIMES_URL = 'http://www.aa.com/travelInformation/gatesTimesSubmit.do' | |
RESULTS_HEADER = 3 | |
TIME_HEADER = 4 | |
CARRIER_INFO = 5 | |
FLIGHT_INFO = 6 | |
def results_group_xpath(row) | |
"//div/table/tr[4]/td/table/tr[2]/td/table/tr[#{row.to_i}]" | |
end | |
def get_headers(doc, time_headers = false) | |
headers = [] | |
headers_row = doc.xpath(results_group_xpath(time_headers ? TIME_HEADER : RESULTS_HEADER)) | |
headers_row.children.each do |child| | |
child = child.text.strip.sub(/\s+/, '') | |
headers.push child if child.length > 0 and child != '' and child != 'FlightStatusNotification' and child != 'Time' | |
headers.push get_headers(doc, true) if child == 'Time' | |
end | |
headers.flatten | |
end | |
abort 'check_flights <flight number> <origin airport> <destination airport>' unless ARGV.length == 3 | |
flight = { | |
'number' => ARGV[0].to_i, | |
'origin' => ARGV[1], | |
'dest' => ARGV[2] | |
} | |
post_params = { | |
'currentCodeForm' => '', | |
'currentCalForm' => 'dep', | |
'originAirport' => flight['origin'], | |
'destinationAirport' => flight['dest'], | |
'flightNumber' => flight['number'], | |
'travelDate' => 'Today', | |
'flightParams.flightDateParams.searchTime' => '080000', | |
'_button_success' => 'GO' | |
} | |
url = URI.parse GATETIMES_URL | |
res = Net::HTTP.post_form url, post_params | |
doc = Nokogiri::HTML res.body | |
puts doc.xpath('//div[@class="cHeader"]').text.gsub(/\s+/, ' ') | |
headers = get_headers doc | |
flight_info = {} | |
flight_info[headers[0]] = doc.xpath(results_group_xpath(CARRIER_INFO)).children.first.children.last.text.strip | |
flight_info_row = doc.xpath(results_group_xpath(FLIGHT_INFO)) | |
flight_info_row.children.each_slice(2) do |child| | |
child = child[0].text.strip | |
flight_info[headers[flight_info.keys.length]] = child | |
end | |
puts | |
headers.each { |k| puts "#{k.ljust(12)}: #{flight_info[k]}" } | |
__END__ | |
$ ./check-flight.rb 319 LGA ORD | |
Flight #319 - DELAYED | |
Carrier : AMERICAN AIRLINES | |
City : LGA New York | |
Date : 05/14/2011 | |
Scheduled : 09:55 AM | |
Estimated : 11:15 AM | |
Actual : | |
Terminal : | |
Gate : D6 | |
BaggageClaim: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment