Last active
November 18, 2022 16:44
-
-
Save lordjabez/d99375e17f5b5691e0f65060452ec676 to your computer and use it in GitHub Desktop.
American Airlines seat viewer
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 python3 | |
# This script takes one parameter, an American Airlines flight number. | |
# It also requires a FlightLabs API key to be stored in FLIGHTLABS_ACCESS_KEY, | |
# you can get one at https://app.goflightlabs.com/. | |
# It will lookup additional flight details from the flight number, and then | |
# open a web page to the AA seat map tool, passing the proper URL parameters | |
# to view the seat map for the provided flight. | |
# It requires dateutil and requests to be installed before use. | |
import os | |
import sys | |
import webbrowser | |
import dateutil.parser | |
import requests | |
flight_number = sys.argv[1] | |
flightlabs_access_key = os.environ['FLIGHTLABS_ACCESS_KEY'] | |
flightlabs_base_url = 'https://app.goflightlabs.com' | |
flightlabs_schedules_url = f'{flightlabs_base_url}/advanced-flights-schedules' | |
aa_base_url = 'https://www.aa.com/seats/view' | |
def get_flight_info(flight_number): | |
params = {'access_key': flightlabs_access_key, 'flight_iata': f'AA{flight_number}'} | |
response = requests.get(flightlabs_schedules_url, params=params) | |
return next(f for f in response.json()['data'] if f['type'] == 'departure') | |
def get_seats_url(flight_info): | |
departure_time = dateutil.parser.parse(flight_info['departure']['scheduledTime']) | |
params = { | |
'flightNumber': flight_info['flight']['number'], | |
'departureMonth': departure_time.month, | |
'departureDay': departure_time.day, | |
'originAirport': flight_info['departure']['iataCode'], | |
'destinationAirport': flight_info['arrival']['iataCode'], | |
} | |
request = requests.Request('GET', aa_base_url, params=params) | |
prepared_request = request.prepare() | |
return prepared_request.url | |
flight_info = get_flight_info(flight_number) | |
aa_seats_url = get_seats_url(flight_info) | |
webbrowser.open(aa_seats_url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment