-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflight_data.py
More file actions
62 lines (53 loc) · 1.99 KB
/
Copy pathflight_data.py
File metadata and controls
62 lines (53 loc) · 1.99 KB
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
class FlightData:
"""Holds the details of a single flight option returned by the search."""
def __init__(self, price, origin_airport, destination_airport, out_date, return_date, stops):
self.price = price
self.origin_airport = origin_airport
self.destination_airport = destination_airport
self.out_date = out_date
self.return_date = return_date
self.stops = stops
def find_cheapest_flight(data, return_date) -> FlightData:
"""Parse a SerpAPI Google Flights response and return the cheapest option.
Merges best_flights and other_flights, skips entries with no price field,
and returns a FlightData with 'N/A' fields if no valid flights are found.
return_date is passed in explicitly because it is not present in the response.
"""
flights = data.get("best_flights", []) + data.get("other_flights", [])
if not flights:
return FlightData(
price="N/A",
origin_airport="N/A",
destination_airport="N/A",
out_date="N/A",
return_date="N/A",
stops="N/A",
)
cheapest = None
for flight in flights:
try:
price = flight["price"]
except KeyError:
continue
if cheapest is None or price < cheapest["price"]:
cheapest = flight
if cheapest is None:
return FlightData(
price="N/A",
origin_airport="N/A",
destination_airport="N/A",
out_date="N/A",
return_date="N/A",
stops="N/A",
)
first_leg = cheapest["flights"][0]
last_leg = cheapest["flights"][-1]
stops = len(cheapest.get("layovers", []))
return FlightData(
price=cheapest["price"],
origin_airport=first_leg["departure_airport"]["id"],
destination_airport=last_leg["arrival_airport"]["id"],
out_date=first_leg["departure_airport"]["time"].split(" ")[0],
return_date=return_date,
stops=stops,
)