|
require 'httparty' |
|
require 'nokogiri' |
|
require 'time' |
|
|
|
class String |
|
def to_readable |
|
squeeze.gsub("\n", "").gsub("\t", "").strip |
|
end |
|
end |
|
|
|
class United |
|
class << self |
|
attr_accessor :flight_number, |
|
:departure_airport, :departure_time, |
|
:arrival_airport, :arrival_time, :arrival_temperature, |
|
:altitude, :ground_speed, |
|
:mileageplus_miles |
|
|
|
def time_in_air |
|
# TODO: Does not account for timezones. |
|
Time.now - departure_time[:actual] |
|
end |
|
|
|
def time_to_destination |
|
arrival_time[:actual] - Time.now |
|
end |
|
|
|
def to_json |
|
{ |
|
basic: { |
|
flight_number: flight_number, |
|
origin: departure_airport, |
|
destination: arrival_airport, |
|
}, |
|
times: { |
|
origin: departure_time, |
|
destination: arrival_time, |
|
time_since_origin: time_in_air, |
|
time_to_destination: time_to_destination |
|
}, |
|
temperature: { |
|
destination: arrival_temperature |
|
}, |
|
misc: { |
|
altitude: altitude, |
|
ground_speed: ground_speed |
|
}, |
|
mileageplus: { |
|
miles: mileageplus_miles |
|
} |
|
} |
|
end |
|
|
|
def parse_from_in_flight_wifi! |
|
page = HTTParty.get("https://www.unitedwifi.com/portal/flifo/index").body |
|
html = Nokogiri::HTML.parse(page) |
|
|
|
# Parse flight number / route |
|
flight_number, route = html.css('#fltInfoContent > h3').text.strip.split('|') |
|
|
|
United.flight_number = flight_number.gsub("\t\n\t", "") |
|
|
|
United.departure_airport = { |
|
name: html.css('#fltInfoContent > div:nth-child(2) > p:nth-child(6)').text.to_readable.match(/Departs: (.*) Scheduled:/)[1], |
|
code: route.split(' to ').first |
|
} |
|
|
|
United.arrival_airport = { |
|
name: html.css('#fltInfoContent > div:nth-child(2) > p:nth-child(7)').text.to_readable.match(/Arives: (.*) Scheduled:/)[1], |
|
code: route.split(' to ').last |
|
} |
|
|
|
# Parse temperature information |
|
destination_info = html.css('#fltInfoContent > div:nth-child(2) > div:nth-child(2)').text.strip.split("\t").map{ |i| i.strip.split("\n") }.flatten |
|
|
|
United.arrival_temperature = { |
|
fahrenheit: destination_info[1][0..-4].to_i, |
|
celsius: destination_info[2].split('|')[0][0..-2].to_i, |
|
text: destination_info[3].strip |
|
} |
|
|
|
# Parse departure/arrival times |
|
United.departure_time = { |
|
scheduled: Time.parse(html.css('#fltInfoContent > div:nth-child(2) > p:nth-child(6)').text.to_readable.match(/Scheduled: (.*)\|(.*)Actual:/)[1]), |
|
actual: Time.parse(html.css('#fltInfoContent > div:nth-child(2) > p:nth-child(6)').text.to_readable.match(/Actual:(.*)\|/)[1][1..-1]) |
|
} |
|
|
|
United.departure_time[:net] = United.departure_time[:actual] - United.departure_time[:scheduled] |
|
|
|
United.arrival_time = { |
|
scheduled: Time.parse(html.css('#fltInfoContent > div:nth-child(2) > p:nth-child(7)').text.to_readable.match(/Scheduled: (.*)\|(.*)Estimated:/)[1]), |
|
actual: Time.parse(html.css('#fltInfoContent > div:nth-child(2) > p:nth-child(7)').text.to_readable.match(/Estimated:(.*)\|/)[1][1..-1]) |
|
} |
|
|
|
United.arrival_time[:net] = United.arrival_time[:actual] - United.arrival_time[:scheduled] |
|
|
|
# Parse awesome flight statistics |
|
United.altitude = { |
|
feet: html.css('#fltInfoContent > div.thirdThird > div:nth-child(6) > span:nth-child(1)').text.gsub(",", "").to_i, |
|
metres: html.css('#fltInfoContent > div.thirdThird > div:nth-child(6) > span:nth-child(3)').text.gsub(",", "").to_i |
|
} |
|
|
|
United.ground_speed = { |
|
miles_per_hour: html.css('#fltInfoContent > div.thirdThird > div:nth-child(5) > span:nth-child(1)').text.to_i, |
|
kilometres_per_hour: html.css('#fltInfoContent > div.thirdThird > div:nth-child(5) > span:nth-child(3)').text.to_i |
|
} |
|
|
|
# Parse MileageMiles |
|
United.mileageplus_miles = html.css('#fltInfoContent > div.thirdThird > div:nth-child(4) > span').text.gsub(",", "")[0..-7].to_i |
|
rescue |
|
raise StandardError, "You are not connected to United's In-Flight Wi-Fi system." |
|
end |
|
end |
|
end |
|
|
|
United.parse_from_in_flight_wifi! |
|
|
|
# HOW TO USE |
|
|
|
# Basic |
|
# United.flight_number => "UA941" |
|
# United.departure_airport => {:name=>"London, GB (LHR - Heathrow)", :code=>"LHR"} |
|
# United.arrival_airport => {:name=>"New York/Newark, NJ, US (EWR - Liberty)", :code=>"EWR"} |
|
|
|
# Times |
|
# United.departure_time => {:scheduled=>2016-01-04 16:05:00 -0500, :actual=>2016-01-04 15:57:00 -0500, :net=>-480.0} |
|
# United.arrival_time => {:scheduled=>2016-01-04 19:30:00 -0500, :actual=>2016-01-04 18:59:00 -0500, :net=>-1860.0} |
|
# United.time_to_destination => 4239.645322 |
|
# United.time_in_air => 6697.717391 |
|
|
|
# Temperature |
|
# United.arrival_temperature => {:fahrenheit=>27, :celsius=>-3, :text=>"Partly Cloudy"} |
|
|
|
# Miscellaneous |
|
# United.altitude => {:feet=>36005, :metres=>10974} |
|
# United.ground_speed => {:miles_per_hour=>516, :kilometres_per_hour=>830} |
|
|
|
# MileagePlus |
|
# United.mileageplus_miles => 3466 |
|
|
|
# OR GET JSON |
|
# puts United.to_json |
|
# { |
|
# :basic => { |
|
# :flight_number => "UA941", |
|
# :origin => { |
|
# :name => "London, GB (LHR - Heathrow)", |
|
# :code => "LHR" |
|
# }, |
|
# :destination => { |
|
# :name => "New York/Newark, NJ, US (EWR - Liberty)", |
|
# :code => "EWR" |
|
# } |
|
# }, |
|
# :times => { |
|
# :origin => { |
|
# :scheduled => 2016-01-04 16:05:00 -0500, |
|
# :actual => 2016-01-04 15:57:00 -0500, |
|
# :net => -480.0 |
|
# }, |
|
# :destination => { |
|
# :scheduled => 2016-01-04 19:30:00 -0500, |
|
# :actual => 2016-01-04 18:59:00 -0500, |
|
# :net => -1860.0 |
|
# }, |
|
# :time_since_origin => 7341.838119, |
|
# :time_to_destination => 3578.161876 |
|
# }, |
|
# :temperature => { |
|
# :destination => { |
|
# :fahrenheit => 27, |
|
# :celsius => -3, |
|
# :text => "Partly Cloudy" |
|
# } |
|
# }, |
|
# :misc => { |
|
# :altitude => { |
|
# :feet => 27947, |
|
# :metres => 8518 |
|
# }, |
|
# :ground_speed => { |
|
# :miles_per_hour => 473, |
|
# :kilometres_per_hour => 761 |
|
# } |
|
# }, |
|
# :mileageplus => { |
|
# :miles => 3466 |
|
# } |
|
# } |
|
|
|
# Built by Bilawal Hameed. |