-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflight_data.py
More file actions
59 lines (50 loc) · 1.88 KB
/
flight_data.py
File metadata and controls
59 lines (50 loc) · 1.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
from data_manager import *
from datetime import *
from dateutil.relativedelta import relativedelta
import os
from requests import *
class FlightData:
'''This class is responsible for structuring the flight data.'''
def __init__(self, fly_to, fly_from='YYT', max_length_of_stay=25, flight_type='round', num_of_adult=1,
num_of_children=0, min_length_of_stay=7, ):
self.api = os.getenv('SEARCHAPI')
self.URL = 'https://tequila-api.kiwi.com/v2/search'
self.fly_from = fly_from
self.fly_to = fly_to
self.today = (datetime.now() + timedelta(days=1)).strftime('%d/%m/%Y')
self.end_date = (datetime.now() + relativedelta(months=6)).strftime('%d/%m/%Y')
self.return_from = fly_to
self.return_to = fly_from
self.flight_type = flight_type
self.adult = num_of_adult
self.child = num_of_children
self.nights_in_dst_from = min_length_of_stay
self.nights_in_dst_to = max_length_of_stay
self.curr = 'CAD'
self.header = {
'apikey': self.api,
}
def getFlightDetails(self):
parameter = {
'fly_from': self.fly_from,
'fly_to': self.fly_to,
'date_from': self.today,
'date_to': self.end_date,
'children': self.child,
'curr': self.curr,
'nights_in_dst_from': self.nights_in_dst_from,
'nights_in_dst_to': self.nights_in_dst_to,
}
try:
detail = get(url=self.URL, headers=self.header, params=parameter).json()['data'][0]
except IndexError:
return f'There are no Flights Available for {self.fly_to}'
else:
return detail
def getPrice(self):
try:
prices = self.getFlightDetails()['price']
except TypeError:
return 0
else:
return prices