-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_aa_award_flights.py
450 lines (383 loc) · 14.5 KB
/
search_aa_award_flights.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"""
Script to search for AA award availablity for multiple origin/destination pairs
and dates. It lets you filter on departure time, duration, number of stops,
and number of miles.
At the time of writing, there were plenty of award search tools out there, but
AA just recently moved to dynamic pricing, so there weren't any that could
filter on dynamic pricing.
This script uses Selenium to scrape the AA award search page. It's not the
fastest, but it works.
Usage:
python search_aa_award_flights.py --help
"""
import argparse
import datetime
import itertools
import pandas as pd
import re
import time
from bs4 import BeautifulSoup
from pprint import pprint
from selenium import webdriver
from typing import Any, Dict, List, Tuple
from tabulate import tabulate
def process_flights(flights: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
processed_flights = []
for flight in flights:
processed_flight = {}
# Process origin, destination, depart_time, and arrive_time
processed_flight["origin"] = flight["origin"]
processed_flight["destination"] = flight["destination"]
# Convert depart_time and arrive_time to datetime.time objects
depart_time = datetime.datetime.strptime(
flight["depart_time"], "%I:%M %p"
).time()
arrive_time = datetime.datetime.strptime(
flight["arrive_time"], "%I:%M %p"
).time()
processed_flight["depart_time"] = depart_time
processed_flight["arrive_time"] = arrive_time
# Process duration
duration_parts = re.findall(r"\d+", flight["duration"])
duration_minutes = int(duration_parts[0]) * 60 + int(duration_parts[1])
processed_flight["duration"] = duration_minutes
# Process num_stops
processed_flight["num_stops"] = int(flight["num_stops"].split(" ")[0])
# Process miles
num_miles = {}
for cabin, miles in flight["num_miles"].items():
cabin = cabin.lower()
if "main" in cabin:
cabin = "main"
elif "first" in cabin:
cabin = "first"
elif "business" in cabin:
cabin = "business"
miles_value = float(miles.replace("K", ""))
num_miles[cabin] = miles_value
# Unpack to num_miles_X, where X is each key
for cabin, miles in num_miles.items():
processed_flight[f"num_miles_{cabin}"] = miles
processed_flights.append(processed_flight)
# sort by ascending depart_time
processed_flights.sort(key=lambda x: x["depart_time"])
return processed_flights
def scrape_flight_page(url: str, driver: webdriver.Chrome, sleep_sec: int = 10) -> list:
"""
Scrapes the AA award flight page for flight details and number of miles for each cabin type.
Args:
url: URL of the AA award flight page to scrape.
driver: Selenium webdriver to use to fetch the page.
Returns:
flights: List of dictionaries containing flight details and number of miles for each cabin type.
"""
# fetch the page
driver.get(url)
# wait N seconds
if sleep_sec:
time.sleep(sleep_sec)
# get the page source
html = driver.page_source
# in the html, scrape flights
soup = BeautifulSoup(html, "html.parser")
# Find all elements that correspond to a flight
flight_elements = soup.find_all(
"div", class_="grid-x grid-padding-x ng-star-inserted"
)
# Initialize an empty list to store flight details
flights = []
# Iterate through each flight element and extract necessary information
print(f"Found {len(flight_elements)} flights")
for flight in flight_elements:
# Extract origin, destination, depart_time, arrive_time, duration and num_stops
origin = (
flight.find("div", class_="cell large-3 origin")
.find("div", class_="city-code")
.text.strip()
)
depart_time = (
flight.find("div", class_="cell large-3 origin")
.find("div", class_="flt-times")
.text.strip()
)
destination = (
flight.find("div", class_="cell large-3 destination")
.find("div", class_="city-code")
.text.strip()
)
arrive_time = (
flight.find("div", class_="cell large-3 destination")
.find("div", class_="flt-times")
.text.strip()
)
duration = flight.find("div", class_="duration").text.strip()
num_stops = flight.find("div", class_="stops").text.strip()
# Find all cabin types and corresponding number of miles within this flight
cabin_elements = flight.find_all(
"div", class_="cell auto pad-left-xxs pad-right-xxs ng-star-inserted"
)
# Initialize an empty dictionary to store cabin type and corresponding number of miles
cabin_miles = {}
# Iterate through each cabin element and extract necessary information
for cabin in cabin_elements:
cabin_type = cabin.find(
"span", class_="hidden-accessible hidden-product-type"
)
num_miles = cabin.find("span", class_="per-pax-amount ng-star-inserted")
# If both a cabin type and number of miles are found, add them to the dictionary
if cabin_type is not None and num_miles is not None:
cabin_miles[cabin_type.text.strip()] = num_miles.text.strip()
# Append this flight's details and number of miles for each cabin type to the list of flights
flights.append(
{
"origin": origin,
"destination": destination,
"depart_time": depart_time,
"arrive_time": arrive_time,
"duration": duration,
"num_stops": num_stops,
"num_miles": cabin_miles,
}
)
# Cleanup
flights = process_flights(flights)
return flights
def filter_flights(
flights: List[Dict[str, Any]],
max_miles_main: int,
max_duration: int,
depart_time_range: Tuple[datetime.time, datetime.time],
arrive_time_range: Tuple[datetime.time, datetime.time],
max_stops: int,
) -> List[Dict[str, Any]]:
"""
Filters a list of flights based on various criteria.
Args:
flights: List of dictionaries containing flight details and number of miles for each cabin type.
max_miles_main: Maximum number of miles allowed for a flight in the main cabin.
max_duration: Maximum duration of a flight in minutes.
depart_time_range: Tuple of minimum and maximum departure times allowed for a flight.
arrive_time_range: Tuple of minimum and maximum arrival times allowed for a flight.
max_stops: Maximum number of stops allowed for a flight.
Returns:
filtered_flights: List of dictionaries containing flight details and number of miles for each cabin type that meet the filtering criteria.
"""
filtered_flights = []
# fix times
min_depart_time, max_depart_time = depart_time_range
min_arrive_time, max_arrive_time = arrive_time_range
for flight in flights:
num_miles_main = flight.get("num_miles_main", 10000000)
if (
num_miles_main <= max_miles_main
and flight["duration"] <= max_duration
and flight["depart_time"] >= min_depart_time
and flight["depart_time"] <= max_depart_time
and flight["arrive_time"] >= min_arrive_time
and flight["arrive_time"] <= max_arrive_time
and flight["num_stops"] <= max_stops
):
filtered_flights.append(flight)
return filtered_flights
def generate_url(
depart_date: str,
origin: str,
destination: str,
n_adults: int,
n_children: int,
) -> str:
"""
Generates the URL for the AA award flight page.
Args:
depart_date: Departure date in YYYY-MM-DD format.
origin: Origin airport code.
destination: Destination airport code.
n_adults: Number of adults.
n_children: Number of children.
Returns:
url: URL of the AA award flight page to scrape.
"""
n_passengers = n_adults + n_children
url = f"https://www.aa.com/booking/search?locale=en_US&pax={n_passengers}&adult={n_adults}&child={n_children}&type=OneWay&searchType=Award&cabin=&carriers=ALL&slices=%5B%7B%22orig%22:%22{origin}%22,%22origNearby%22:true,%22dest%22:%22{destination}%22,%22destNearby%22:true,%22date%22:%22{depart_date}%22%7D%5D&maxAwardSegmentAllowed=2"
return url
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Search for AA award availability for multiple origin/destination pairs and dates."
)
parser.add_argument(
"-d",
"--depart_date",
nargs="+",
help="Departure date(s) in YYYY-MM-DD format.",
required=True,
)
parser.add_argument(
"-o", "--origin", nargs="+", help="Origin airport codes.", required=True
)
parser.add_argument(
"-des",
"--destination",
nargs="+",
help="Destination airport codes.",
required=True,
)
parser.add_argument("--n_adults", type=int, help="Number of adults.", default=1)
parser.add_argument("--n_children", type=int, help="Number of children.", default=0)
parser.add_argument(
"--max_miles_main",
type=int,
default=20,
help="Maximum number of miles in thousands.",
)
parser.add_argument(
"--max_duration",
type=int,
default=11 * 60,
help="Maximum duration of flight in minutes.",
)
parser.add_argument(
"--depart_time_range",
nargs=2,
default=["07:00", "16:00"],
help="Departure time range in HH:MM format.",
)
parser.add_argument(
"--arrive_time_range",
nargs=2,
default=["12:00", "22:00"],
help="Arrival time range in HH:MM format.",
)
parser.add_argument(
"--max_stops", type=int, default=1, help="Maximum number of stops."
)
parser.add_argument(
"--output_file_raw",
default="flights_all.csv",
help="Output file for raw flight data.",
)
parser.add_argument(
"--output_file_filtered",
default="flights_filtered.csv",
help="Output file for filtered flight data.",
)
parser.add_argument(
"--sleep_init_sec",
type=int,
default=10,
help="Initial sleep time in seconds when loading browser.",
)
parser.add_argument(
"--sleep_sec",
type=int,
default=5,
help="Sleep time in seconds between each page load.",
)
args = parser.parse_args()
print("Arguments:")
pprint(vars(args))
# Flight info
depart_date = args.depart_date
origin = args.origin
destination = args.destination
n_adults = args.n_adults
n_children = args.n_children
# Filter criteria
max_miles_main = args.max_miles_main # in thousands
max_duration = args.max_duration # in minutes
depart_time_range = tuple(
datetime.datetime.strptime(d, "%H:%M").time() for d in args.depart_time_range
)
arrive_time_range = tuple(
datetime.datetime.strptime(d, "%H:%M").time() for d in args.arrive_time_range
)
max_stops = args.max_stops
# Other config
output_file_raw = args.output_file_raw
output_file_filtered = args.output_file_filtered
sleep_init_sec = args.sleep_init_sec
sleep_sec = args.sleep_sec
# use chrome driver
driver = webdriver.Chrome()
time.sleep(sleep_init_sec)
# loop through all of depart_date, origin, and destination combos.. including a counter
all_flights = []
all_filtered_flights = []
error_combos = []
missing_combos = []
for i, (dt, o, d) in enumerate(itertools.product(depart_date, origin, destination)):
# print the current iteration out of total iterations to keep track of progress
print(
f"\nScraping {i+1} of {len(depart_date) * len(origin) * len(destination)}: {dt}, {o}, {d}"
)
# generate the url
url = generate_url(
dt,
o,
d,
n_adults,
n_children,
)
# scrape the flights
flights = []
filtered_flights = []
try:
flights = scrape_flight_page(url, driver, sleep_sec=sleep_sec)
except:
print("Error scraping. Continuing to next iteration.")
error_combos.append((dt, o, d))
continue
print(f"Found {len(flights)} total flights.")
if len(flights) == 0:
print("No flights found. Continuing to next iteration.")
missing_combos.append((dt, o, d))
continue
# add the date to each object
for flight in flights:
flight["date"] = dt
# filter the flights
if len(flights) > 0:
filtered_flights = filter_flights(
flights,
max_miles_main,
max_duration,
depart_time_range,
arrive_time_range,
max_stops,
)
print(f"Found {len(filtered_flights)} flights that meet the criteria.")
all_flights.extend(flights)
if len(filtered_flights) > 0:
all_filtered_flights.extend(filtered_flights)
print("Done scraping.")
print(f"Found {len(all_flights)} total flights.")
print(f"Found {len(all_filtered_flights)} flights that meet the criteria.")
# Sort all by ascending origin, then ascending date, then ascending time
all_filtered_flights = sorted(
all_filtered_flights, key=lambda x: (x["origin"], x["date"], x["depart_time"])
)
# print in a pretty table
print(
tabulate(
all_filtered_flights,
headers="keys",
tablefmt="pretty",
showindex="never",
floatfmt=".2f",
)
)
# Save to CSV
if len(all_filtered_flights) > 0:
df = pd.DataFrame(all_filtered_flights)
df.to_csv(output_file_filtered, index=False)
print(f"Saved {len(all_filtered_flights)} records to {output_file_filtered}.")
if len(all_flights) > 0:
df = pd.DataFrame(all_flights)
df.to_csv(output_file_raw, index=False)
print(f"Saved {len(all_flights)} records to {output_file_raw}.")
# print errors
if len(error_combos) > 0:
print("\nErrors:")
pprint(error_combos)
if len(missing_combos) > 0:
print("\nMissing:")
pprint(missing_combos)