Skip to content

Commit

Permalink
Create flight_search.py
Browse files Browse the repository at this point in the history
  • Loading branch information
xnkit69 committed Sep 21, 2023
1 parent 65d26cb commit 4bc7463
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions day40 /flight_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import requests
from flight_data import FlightData

TEQUILA_ENDPOINT = "https://tequila-api.kiwi.com"
TEQUILA_API_KEY = "********************************"


class FlightSearch:

def get_destination_code(self, city_name):
location_endpoint = f"{TEQUILA_ENDPOINT}/locations/query"
headers = {"apikey": TEQUILA_API_KEY}
query = {"term": city_name, "location_types": "city"}
response = requests.get(url=location_endpoint, headers=headers, params=query)
results = response.json()["locations"]
code = results[0]["code"]
return code

def check_flights(self, origin_city_code, destination_city_code, from_time, to_time):
headers = {"apikey": TEQUILA_API_KEY}
query = {
"fly_from": origin_city_code,
"fly_to": destination_city_code,
"date_from": from_time.strftime("%d/%m/%Y"),
"date_to": to_time.strftime("%d/%m/%Y"),
"nights_in_dst_from": 7,
"nights_in_dst_to": 28,
"flight_type": "round",
"one_for_city": 1,
"max_stopovers": 0,
"curr": "GBP"
}

response = requests.get(
url=f"{TEQUILA_ENDPOINT}/v2/search",
headers=headers,
params=query,
)
try:
data = response.json()["data"][0]
except IndexError:
query["max_stopovers"] = 1
response = requests.get(
url=f"{TEQUILA_ENDPOINT}/v2/search",
headers=headers,
params=query,
)
data = response.json()["data"][0]
pprint(data)
flight_data = FlightData(
price=data["price"],
origin_city=data["route"][0]["cityFrom"],
origin_airport=data["route"][0]["flyFrom"],
destination_city=data["route"][1]["cityTo"],
destination_airport=data["route"][1]["flyTo"],
out_date=data["route"][0]["local_departure"].split("T")[0],
return_date=data["route"][2]["local_departure"].split("T")[0],
stop_overs=1,
via_city=data["route"][0]["cityTo"]
)
return flight_data
else:
flight_data = FlightData(
price=data["price"],
origin_city=data["route"][0]["cityFrom"],
origin_airport=data["route"][0]["flyFrom"],
destination_city=data["route"][0]["cityTo"],
destination_airport=data["route"][0]["flyTo"],
out_date=data["route"][0]["local_departure"].split("T")[0],
return_date=data["route"][1]["local_departure"].split("T")[0]
)

return flight_data

0 comments on commit 4bc7463

Please sign in to comment.