Skip to content

Commit 07cf285

Browse files
authored
Create flight_search.py
1 parent 83a3bc3 commit 07cf285

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

day39/flight_search.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import requests
2+
from flight_data import FlightData
3+
4+
TEQUILA_ENDPOINT = "https://tequila-api.kiwi.com"
5+
TEQUILA_API_KEY = "********************************"
6+
7+
8+
class FlightSearch:
9+
10+
def get_destination_code(self, city_name):
11+
location_endpoint = f"{TEQUILA_ENDPOINT}/locations/query"
12+
headers = {"apikey": TEQUILA_API_KEY}
13+
query = {"term": city_name, "location_types": "city"}
14+
response = requests.get(url=location_endpoint, headers=headers, params=query)
15+
results = response.json()["locations"]
16+
code = results[0]["code"]
17+
return code
18+
19+
def check_flights(self, origin_city_code, destination_city_code, from_time, to_time):
20+
headers = {"apikey": TEQUILA_API_KEY}
21+
query = {
22+
"fly_from": origin_city_code,
23+
"fly_to": destination_city_code,
24+
"date_from": from_time.strftime("%d/%m/%Y"),
25+
"date_to": to_time.strftime("%d/%m/%Y"),
26+
"nights_in_dst_from": 7,
27+
"nights_in_dst_to": 28,
28+
"flight_type": "round",
29+
"one_for_city": 1,
30+
"max_stopovers": 0,
31+
"curr": "GBP"
32+
}
33+
34+
response = requests.get(
35+
url=f"{TEQUILA_ENDPOINT}/v2/search",
36+
headers=headers,
37+
params=query,
38+
)
39+
40+
try:
41+
data = response.json()["data"][0]
42+
except IndexError:
43+
print(f"No flights found for {destination_city_code}.")
44+
return None
45+
46+
flight_data = FlightData(
47+
price=data["price"],
48+
origin_city=data["route"][0]["cityFrom"],
49+
origin_airport=data["route"][0]["flyFrom"],
50+
destination_city=data["route"][0]["cityTo"],
51+
destination_airport=data["route"][0]["flyTo"],
52+
out_date=data["route"][0]["local_departure"].split("T")[0],
53+
return_date=data["route"][1]["local_departure"].split("T")[0]
54+
)
55+
print(f"{flight_data.destination_city}: £{flight_data.price}")
56+
return flight_data

0 commit comments

Comments
 (0)