-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflight_search.py
More file actions
76 lines (68 loc) · 2.88 KB
/
flight_search.py
File metadata and controls
76 lines (68 loc) · 2.88 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
import requests
from flight_data import FlightData
class FlightSearch:
def __init__(self, endpoint, api_key):
self.endpoint = endpoint
self.api_key = api_key
self.header = {
"apiKey": self.api_key
}
def get_iata_code(self, city):
response = requests.get(url=f"{self.endpoint}/locations/query", headers=self.header, params=city)
result = response.json()
iata = result['locations'][0]['code']
return iata
def search_flights(self, destination, date_from, date_to, max_stops):
params = {
"fly_from": "AMS",
"fly_to": destination,
"date_from": f"{date_from.strftime('%d/%m/%Y')}",
"date_to": f"{date_to.strftime('%d/%m/%Y')}",
"nights_in_dst_from": 7,
"nights_in_dst_to": 28,
"max_stopovers": max_stops,
"curr": "EUR",
"one_for_city": 1
}
response = requests.get(url=f"{self.endpoint}/v2/search", headers=self.header, params=params)
result = response.json()
try:
data = result['data'][0]
except IndexError:
try:
params["max_stopovers"] = 2
response = requests.get(url=f"{self.endpoint}/v2/search", headers=self.header, params=params)
result = response.json()
data = result['data'][0]
flight = FlightData(
departure_city=data['cityFrom'],
dep_code=data['flyFrom'],
destination=data['cityTo'],
dest_code=data['flyTo'],
price=data['price'],
outbound_date=data['route'][0]['local_departure'].split("T")[0],
return_date=data['route'][-1]['local_departure'].split("T")[0],
stop_overs=0,
via_cities=[]
)
for route in data['route']:
if route['flyTo'] != 'AMS' and route['cityCodeTo'] != destination:
flight.via_cities.append(route['cityTo'])
flight.stop_overs += 1
print(f"{flight.destination}: €{flight.price}, via {flight.via_cities}")
return flight
except IndexError:
print(f"No flights found for {destination}.")
return None
else:
flight = FlightData(
departure_city=data['cityFrom'],
dep_code=data['flyFrom'],
destination=data['cityTo'],
dest_code=data['flyTo'],
price=data['price'],
outbound_date=data['route'][0]['local_departure'].split("T")[0],
return_date=data['route'][1]['local_departure'].split("T")[0],
)
print(f"{flight.destination}: €{flight.price}")
return flight