Skip to content

Instantly share code, notes, and snippets.

@bih
Last active January 18, 2024 01:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bih/333a32bbfe3ec07e150e to your computer and use it in GitHub Desktop.
Save bih/333a32bbfe3ec07e150e to your computer and use it in GitHub Desktop.
United In-Flight Wi-Fi JSON

United In-Flight Wi-Fi JSON

This is a small script I wrote that allows you to parse information of your flight when connected to United's In-Flight Wi-Fi. It provides information such as expected/actual departure, arrivals, airports, temperatures, altitude, ground speed, miles accrued for the flight and the flight number.

Install

Copy united.rb to your computer and install the following gems:

$ gem install httparty
$ gem install nokogiri

That's it! Run it by executing ruby united.rb or include it in your own Ruby script. See the file to find out what functions are available or just get a JSON output by executing United.to_json.

Note: You do need to be connected to a United's In-Flight Wi-Fi, but you do not need to pay for the internet for it to work. It'll work regardless if you've paid for internet or not.

Example 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
  }
}
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment