-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflight_checker.py
More file actions
69 lines (60 loc) · 2.28 KB
/
flight_checker.py
File metadata and controls
69 lines (60 loc) · 2.28 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
import requests
import os
class FlightChecker:
def __init__(self):
self.TEQUILA_API_KEY = os.environ.get('TEQUILA_API_KEY')
self.SEARCH_URL = 'https://api.tequila.kiwi.com/v2/search'
self.IATA_URL = 'https://api.tequila.kiwi.com/locations/query'
self.header = {
'apikey': self.TEQUILA_API_KEY,
}
def get_codes(self, places):
codes = []
for name_of_place in places:
parameters = {
'term': name_of_place,
}
iata_code = requests.get(self.IATA_URL, headers=self.header, params=parameters).json()['locations'][0][
'code']
codes.append(iata_code)
return codes
def get_names(self, codes):
names = []
for code in codes:
parameters = {
'term': code,
}
try:
name = \
requests.get(self.IATA_URL, headers=self.header, params=parameters).json()['locations'][0]['city'][
'name']
except:
name = requests.get(self.IATA_URL, headers=self.header, params=parameters).json()['locations'][0][
'name']
names.append(name)
return names
def check_flight(self, user):
flights = []
for i in range(len(user['fly_to'])):
parameters = {
"fly_from": user['fly_from'],
"fly_to": user['fly_to'][i],
"date_from": user['date_from'],
"date_to": user["date_to"],
"return_from": user['return_from '],
"return_to": user["return_to"],
"price_to": user['price_to'][i],
'curr': 'USD',
}
response = requests.get(self.SEARCH_URL, headers=self.header, params=parameters).json()
try:
if len(response['data']) > 0:
flight_dict = {
'cityFrom': response['data'][0]['cityFrom'],
'cityTo': response['data'][0]['cityTo'],
'price': response['data'][0]['price'],
}
flights.append(flight_dict)
except:
flights = flights
return flights