-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflight_ticket_price_api.py
More file actions
97 lines (82 loc) · 3.34 KB
/
flight_ticket_price_api.py
File metadata and controls
97 lines (82 loc) · 3.34 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
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
import requests
from datetime import datetime, timedelta
import configuration as config
def get_min_price(start_iata, dest_iata):
"""
Gets the minimum price for a given airport and destination airport.
Args:
start_iata (str): The starting airport iata code.
dest_iata (str): The destination airport iata code.
Returns:
The cheapest price from the API
"""
url = "https://booking-com15.p.rapidapi.com/api/v1/flights/getMinPrice"
querystring = {
"fromId": start_iata + ".AIRPORT",
"toId": dest_iata + ".AIRPORT",
"departDate": (datetime.today() + timedelta(days=7)).strftime("%Y-%m-%d"),
"currency": config.CURRENCY
}
headers = {
"x-rapidapi-key": config.RAPIDAPI_KEY,
"x-rapidapi-host":"booking-com15.p.rapidapi.com"
}
try:
response = requests.get(url, headers=headers, params=querystring)
response.raise_for_status()
data = response.json()
# Check if the response contains flight data and extract the first item
if data and data.get("data") and isinstance(data["data"], list) and len(data["data"]) > 0:
cheapest_price = data["data"][0]["price"]["units"] # Access the first element of the data for price
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
except ValueError as e:
print(f"Invalid JSON response: {e}")
return cheapest_price
def get_price_range(path, date, cabin_class):
"""
Gets the price range for a given airport path, date and cabin class (first class, business, economy).
Args:
path (str): A list of IATA codes along the path.
date (datetime): The date to get the price range in.
cabin_class: The cabin class to get the price range in.
Returns:
The price range (str)
"""
if config.API_ON == False:
return " API disabled "
number_of_legs = len(path)
legs = []
for i, iata in enumerate(path):
if i < number_of_legs - 1:
start_iata = iata
dest_iata = path[i + 1]
legs.append({
"fromId": start_iata + ".AIRPORT",
"toId": dest_iata + ".AIRPORT",
"date": str(date)
})
url = "https://booking-com15.p.rapidapi.com/api/v1/flights/getMinPriceMultiStops"
querystring = {"legs": str(legs), "currency_code": config.SELECTED_CURRENCY , "cabininClass": cabin_class}
headers = {
"x-rapidapi-key": config.RAPIDAPI_KEY,
"x-rapidapi-host": "booking-com15.p.rapidapi.com"
}
try:
response = requests.get(url, headers=headers, params=querystring)
response.raise_for_status()
data = response.json()
prices = []
if data and data.get("data") and isinstance(data["data"], list):
for flight in data["data"]:
if "price" in flight and "units" in flight["price"]:
prices.append(int(flight["price"]["units"]))
if prices:
return str(min(prices)) + "-" + str(max(prices)) # Returning the range of prices
else:
return "N/A" # No prices found
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
except ValueError as e:
print(f"Invalid JSON response: {e}")
return "N/A"