-
Notifications
You must be signed in to change notification settings - Fork 0
/
flight_search.py
86 lines (74 loc) · 3.12 KB
/
flight_search.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
import requests
import datetime as dt
from flight_data import FlightData
TEQUILA_ENDPOINT = "https://tequila-api.kiwi.com"
date_format = '%d/%m/%Y'
ORIGIN_CITY_IATA = 'LON'
class FlightSearch:
# This class is responsible for talking to the Flight Search API.
def __init__(self, tequila_api):
self.tequila_api = tequila_api
def get_city_code(self, city_name):
parameters = {
'apikey': self.tequila_api,
'term': city_name,
'locale': "en-US",
'location_types': 'city',
'limit': "1"
}
response = requests.get(url=f'{TEQUILA_ENDPOINT}/locations/query', params=parameters)
response.raise_for_status()
data = response.json()['locations'][0]['code']
return data
def search_flights(self, destination_city_code):
tmr_date = dt.datetime.today() + dt.timedelta(days=1)
max_date = dt.datetime.today() + dt.timedelta(days=180)
headers = {
'apikey': self.tequila_api
}
parameters = {
'fly_from': ORIGIN_CITY_IATA,
'fly_to': destination_city_code,
'date_from': tmr_date.strftime(date_format),
'date_to': max_date.strftime(date_format),
'max_stopovers': 0,
'curr': 'GBP',
'nights_in_dst_from': 7,
'nights_in_dst_to': 28,
'flight_type': 'round',
"one-for-city": 1
}
response = requests.get(
url=f'{TEQUILA_ENDPOINT}/v2/search',
headers=headers,
params=parameters
)
response.raise_for_status()
try:
data = response.json()['data'][0]
except IndexError:
parameters['max_stopovers'] = 1
response = requests.get(
url=f'{TEQUILA_ENDPOINT}/v2/search',
headers=headers,
params=parameters
)
response.raise_for_status()
print(response.json())
if len(response.json()['data']) > 0:
data = response.json()['data'][0]
flight_data = FlightData(price=data['price'], city_from=data['cityFrom'], fly_from=data['flyFrom'],
city_to=data['cityTo'], fly_to=data['flyTo'],
leave_date=data["route"][0]["local_departure"].split("T")[0],
return_date=data["route"][1]["local_departure"].split("T")[0],
stop_overs=1, via_city=data["route"][0]["cityTo"])
return flight_data
else:
return None
else:
flight_data = FlightData(price=data['price'], city_from=data['cityFrom'], fly_from=data['flyFrom'],
city_to=data['cityTo'], fly_to=data['flyTo'],
leave_date=data["route"][0]["local_departure"].split("T")[0],
return_date=data["route"][1]["local_departure"].split("T")[0],
)
return flight_data