-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkiwi_controller.py
More file actions
184 lines (153 loc) · 6.41 KB
/
kiwi_controller.py
File metadata and controls
184 lines (153 loc) · 6.41 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from helpers.date_converter import *
from helpers.countries_link_normalization import *
from model.model import *
from config import *
import urllib.request
import requests
import json
# script variables
data_from_kiwi_url = {}
all_flights = []
sorted_data = []
# variable needed for search in api
fly_from = 'krakow'
fly_to = ''
number_of_passengers = '2'
date_from = '08/08/2018'
date_to = '08/12/2018'
days_in_destination_from = '2'
days_in_destination_to = '4'
price_from = '0'
price_to = '80'
booking_tokens = []
def create_link(city_from, city_to, date):
link = NODEEP + f"{city_from}/{city_to}/{date}/2-4"
return link
# in this function we do get request and
# get all data from kiwi api
def get_data_from_kiwi_url():
with urllib.request.urlopen(
SEARCH_ENGINE + f"?flyFrom={fly_from}"
f"&to={fly_to}"
f"&typeFlight=return"
f"&daysInDestinationFrom={days_in_destination_from}"
f"&daysInDestinationTo={days_in_destination_to}"
f"&price_from={price_from}"
f"&price_to={price_to}&"
f"dateFrom={date_from}"
f"&dateTo={date_to}"
) as url:
print(
SEARCH_ENGINE + f"?flyFrom={fly_from}"
f"&to={fly_to}"
f"&typeFlight=return"
f"&daysInDestinationFrom={days_in_destination_from}"
f"&daysInDestinationTo={days_in_destination_to}"
f"&price_from={price_from}"
f"&price_to={price_to}"
f"&dateFrom={date_from}"
f"&dateTo={date_to}"
)
global data_from_kiwi_url
global all_flights
data_from_kiwi_url = json.loads(url.read().decode())
all_flights = data_from_kiwi_url['data']
def sort_useful_data():
global convert_date
for counter, key in enumerate(range(0, len(all_flights))):
single_trip = {
'cityFrom': all_flights[counter]['mapIdfrom'],
'countryFrom': all_flights[counter]['countryFrom'],
'cityTo': all_flights[counter]['mapIdto'],
'countryTo': all_flights[counter]['countryTo'],
'date': convert_date_to_UTC(all_flights[counter]['route'][0]['dTime']),
'return_cityFrom': all_flights[counter]['route'][1]['cityFrom'],
'return_cityTo': all_flights[counter]['route'][1]['cityTo'],
'return_date': convert_date_to_UTC(all_flights[counter]['route'][1]['dTime']),
"nightsInDestination": all_flights[counter]['nightsInDest'],
'price': all_flights[counter]['conversion']['EUR'],
'booking_token': all_flights[counter]['booking_token'],
"link": create_link(
all_flights[counter]['mapIdfrom'] + "-" + normalise(all_flights[counter]['countryFrom']['name']),
all_flights[counter]['mapIdto'] + "-" + normalise(all_flights[counter]['countryTo']['name']),
convert_date_to_UTC(all_flights[counter]['route'][0]['dTime'])),
"cityFromFullName": all_flights[counter]['cityFrom'],
"cityToFullName": all_flights[counter]['cityTo']
}
booking_tokens.append(all_flights[counter]['booking_token'])
sorted_data.append(single_trip)
def check_flights(booking_token):
"""
Confirms the price of the flights, has to return true, otherwise the save_booking wont pass
"""
parameters = {'v': 2, # default
'pnum': 1, # passenger number
'bnum': 0, # number of bags
'booking_token': booking_token
}
response = requests.get(CHECK_FLIGHTS_ENGINE, params=parameters).json()
print(response)
checked = response['flights_checked']
invalid = response['flights_invalid']
return checked, invalid
def get_data_by_default_parameters():
get_data_from_kiwi_url()
sort_useful_data()
print(sorted_data)
# print(check_flights(booking_tokens[0]))
db.child("tripsletter").push(sorted_data)
return unpack_data()
def init_search_parameters(city_from="krakow", city_to="", date_f="08/08/2018", date_t="20/12/2018", passengers="1",
days_in_destination_f='2',
days_in_destination_t='4', price_t='70'):
''' Function which init global parameters for user search '''
global fly_from, fly_to, number_of_passengers
global date_from, date_to
global days_in_destination_from, days_in_destination_to, price_to
fly_from = city_from
fly_to = city_to
number_of_passengers = passengers
date_from = date_f
date_to = date_t
days_in_destination_from = days_in_destination_f
days_in_destination_to = days_in_destination_t
price_to = price_t
get_data_from_kiwi_url()
sort_useful_data()
# print(sorted_data)
# call check flights for more information about single flight ex. number of seats, bags fee
# print(check_flights(booking_tokens[0]))
# put into firebase data
db.child("users_search").push(sorted_data)
return unpack_data()
def get_data_from_db():
all_searches = db.child("users_search").get()
result = []
for user in all_searches.each():
result.append(user.val())
return result
def unpack_data():
global sorted_data
list_of_flights = []
if len(data_from_kiwi_url['hashtags']) == 0:
return None
for count, trip in enumerate(sorted_data):
data_for_telegram = f''' <a href="{sorted_data[count]['link']}">{sorted_data[count]['cityFromFullName']} - {sorted_data[count]['cityToFullName']}</a>''' \
f''' Price: <b>{sorted_data[count]['price']} €</b> \n''' \
f'''{sorted_data[count]['date']} - {sorted_data[count]['return_date']} \n'''
list_of_flights.append(data_for_telegram)
# print(list_of_flights)
sorted_data.clear()
return list_of_flights
def reset():
global fly_from, fly_to, number_of_passengers
global date_from, date_to
global days_in_destination_from, days_in_destination_to, price_to
fly_from = 'krakow'
fly_to = ''
number_of_passengers = '2'
date_from = '08/08/2018'
date_to = '08/12/2018'
days_in_destination_from = '2'
days_in_destination_to = '4'
price_to = '80'