Created
July 9, 2024 12:38
-
-
Save mbreslow/18a4f36b3433bfa8c4069c4f466e8637 to your computer and use it in GitHub Desktop.
Python Code to Query Position in United Airlines Upgrade List
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
import requests | |
import os | |
from datetime import datetime | |
import difflib | |
# Ensure the 'runs' directory exists | |
runs_dir = 'runs' | |
os.makedirs(runs_dir, exist_ok=True) | |
FLIGHT_NUMBER = '78' | |
FLIGHT_DATE = '20240714' #YYYYMMDD | |
LAST_NAME_PREFIX = 'BRE' | |
url = "https://smartphone.united.com/UnitedMobileDataServices/api/UpgradeList/GetUpgradeList" | |
params = { | |
"accessCode": "ACCESSCODE", | |
"appversion": "4.1.37", | |
"applicationId": "1", | |
"languageCode": "en-US", | |
"departureAirportCode": "NRT", | |
"transactionId": "", | |
"flightNumber": FLIGHT_NUMBER, | |
"flightDate": FLIGHT_DATE | |
} | |
headers = { | |
"Host": "smartphone.united.com", | |
"Content-Type": "application/json", | |
"Accept": "*/*", | |
"User-Agent": "UnitedCustomerFacingIPhone/4.1.37.1 CFNetwork/1240.0.4 Darwin/20.6.0", | |
"Accept-Language": "en-ca" | |
} | |
response = requests.get(url, headers=headers, params=params) | |
if response.status_code == 200: | |
respjson = response.json() | |
if respjson is None: | |
print(f"{datetime.now().strftime('%Y%m%d%H%M%S')}: List is not posted yet") | |
else: | |
outputs = [] | |
for cabin in ['first', 'business']: | |
cabinBookingStatus = respjson['upgradeList'][cabin]['cabinBookingStatus'] | |
capacity = 0 | |
booked = 0 | |
for item in cabinBookingStatus: | |
if item['key'] == 'Capacity': | |
capacity = int(item['value']) | |
elif item['key'] == 'Booked': | |
booked = int(item['value']) | |
available = capacity - booked | |
outputs.append(f"[{cabin}] Available: {available}, Booked: {booked}") | |
standbyList = respjson['upgradeList'][cabin]['standby'] | |
listPlaces = [] | |
for idx, item in enumerate(standbyList): | |
if item['name']['last'] == LAST_NAME_PREFIX: | |
listPlaces.append(idx+1) | |
outputs.append(f"[{cabin}] List places: {listPlaces}") | |
print('\n'.join(outputs)) | |
# Write the outputs to a file with a timestamp in the filename | |
timestamp = datetime.now().strftime('%Y%m%d%H%M%S') | |
filename = os.path.join(runs_dir, f'run.{timestamp}.txt') | |
with open(filename, 'w') as file: | |
file.write('\n'.join(outputs)) | |
# Find the 2 most recent runs | |
run_files = sorted(os.listdir(runs_dir), reverse=True)[:2] | |
if len(run_files) < 2: | |
print("Not enough run files to compare.") | |
else: | |
latest_file = os.path.join(runs_dir, run_files[0]) | |
previous_file = os.path.join(runs_dir, run_files[1]) | |
with open(latest_file, 'r') as file: | |
latest_strings = file.readlines() | |
with open(previous_file, 'r') as file: | |
previous_strings = file.readlines() | |
# Compare the strings | |
if latest_strings == previous_strings: | |
print(f"No change since {run_files[1]}") | |
else: | |
diff = difflib.unified_diff(previous_strings, latest_strings, fromfile=previous_file, tofile=latest_file) | |
print(''.join(diff)) | |
else: | |
print(f"Request failed with status code {response.status_code}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment