-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflights.py
357 lines (328 loc) · 12.6 KB
/
flights.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time
import datetime
import openpyxl as xl
import csv
import threading
chrome_path = 'C:/bin/chromedriver.exe'
i = 0
dep_times_list = None
arr_times_list = None
airlines_list = None
price_list = None
durations_list = None
stops_list = None
layovers_list = None
idpath = None
#Setting ticket types paths
return_ticket = "//label[@id='flight-type-roundtrip-label-hp-flight']"
one_way_ticket = "//label[@id='flight-type-one-way-label-hp-flight']"
multi_ticket = "//label[@id='flight-type-multi-dest-label-hp-flight']"
def ticket_chooser(ticket,browser):
global idpath
try:
if(ticket == one_way_ticket):
idpath = "//input[@id='flight-departing-single-hp-flight']"
else:
idpath = "//input[@id='flight-departing-hp-flight']"
ticket_type = browser.find_element_by_xpath(ticket)
ticket_type.click()
except Exception as e:
pass
def dep_country_chooser(dep_country,browser):
fly_from = browser.find_element_by_xpath("//input[@id='flight-origin-hp-flight']")
time.sleep(1)
fly_from.clear()
time.sleep(1.5)
fly_from.send_keys(' ' + dep_country)
time.sleep(1.5)
first_item = browser.find_element_by_xpath("//a[@id='aria-option-0']")
time.sleep(1.5)
first_item.click()
def arrival_country_chooser(arrival_country,browser):
fly_to = browser.find_element_by_xpath("//input[@id='flight-destination-hp-flight']")
time.sleep(1)
fly_to.clear()
time.sleep(1.5)
fly_to.send_keys(' ' + arrival_country)
time.sleep(1.5)
first_item = browser.find_element_by_xpath("//a[@id='aria-option-0']")
time.sleep(1.5)
first_item.click()
def dep_date_chooser(month, day, year,browser):
dep_date_button = browser.find_element_by_xpath(idpath)
dep_date_button.clear()
dep_date_button.send_keys(month + '/' + day + '/' + year)
def return_date_chooser(month, day, year,browser):
return_date_button = browser.find_element_by_xpath("//input[@id='flight-returning-hp-flight']")
for i in range(11):
return_date_button.send_keys(Keys.BACKSPACE)
return_date_button.send_keys(month + '/' + day + '/' + year)
def one_way_help(browser):
search = browser.find_element_by_xpath("//button[@class='trigger-utility menu-trigger btn-utility btn-secondary dropdown-toggle theme-standard pin-left menu-arrow gcw-component gcw-traveler-amount-select gcw-component-initialized']")
search.click()
def search(browser):
search = browser.find_element_by_xpath("//button[@class='btn-primary btn-action gcw-submit']")
search.click()
time.sleep(10)
print('Results ready!')
df = pd.DataFrame()
def compile_data(browser):
global df
global dep_times_list
global arr_times_list
global airlines_list
global price_list
global durations_list
global stops_list
global layovers_list
#departure times
dep_times = browser.find_elements_by_xpath("//span[@data-test-id='departure-time']")
dep_times_list = [value.text for value in dep_times]
#arrival times
arr_times = browser.find_elements_by_xpath("//span[@data-test-id='arrival-time']")
arr_times_list = [value.text for value in arr_times]
#airline name
airlines = browser.find_elements_by_xpath("//span[@data-test-id='airline-name']")
airlines_list = [value.text for value in airlines]
#prices
prices = browser.find_elements_by_xpath("//span[@data-test-id='listing-price-dollars']")
price_list = [value.text for value in prices]
#durations
durations = browser.find_elements_by_xpath("//span[@data-test-id='duration']")
durations_list = [value.text for value in durations]
#stops
stops = browser.find_elements_by_xpath("//span[@class='number-stops']")
stops_list = [value.text for value in stops]
#layovers
layovers = browser.find_elements_by_xpath("//span[@data-test-id='layover-airport-stops']")
layovers_list = [value.text for value in layovers]
now = datetime.datetime.now()
current_date = (str(now.year) + '-' + str(now.month) + '-' + str(now.day))
current_time = (str(now.hour) + ':' + str(now.minute))
current_price = 'price' + '(' + current_date + '---' + current_time + ')'
for i in range(len(dep_times_list)):
try:
df.loc[i, 'departure_time'] = dep_times_list[i]
except Exception as e:
pass
try:
df.loc[i, 'arrival_time'] = arr_times_list[i]
except Exception as e:
pass
try:
df.loc[i, 'airline'] = airlines_list[i]
except Exception as e:
pass
try:
df.loc[i, 'duration'] = durations_list[i]
except Exception as e:
pass
try:
df.loc[i, 'stops'] = stops_list[i]
except Exception as e:
pass
try:
df.loc[i, 'layovers'] = layovers_list[i]
except Exception as e:
pass
try:
df.loc[i, 'price'] = price_list[i]
except Exception as e:
pass
print('Excel Sheet Created!')
now = datetime.datetime.now()
current_date = (str(now.day) + str(now.month) + str(now.year))
current_time = (str(now.hour) + ':' + str(now.minute))
current_price = 'price' + '(' + current_date + '---' + current_time + ')'
def main(day,month,year,browser):
try:
global i
date = day+month+year
l=[]
d={}
d2={}
with open("airport_codes.csv", 'r') as csvfile2:
csvreader2 = csv.reader(csvfile2)
for row in csvreader2:
if row[3]=='India':
l.append(row[1])
with open("routes.csv", 'r') as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
if row[2] not in l and row[4] not in l:
continue
if row[2] in d:
d[row[2]].add(row[4])
else:
d[row[2]]={row[4]}
with open("airport_codes.csv", 'r') as csvfile3:
csvreader3 = csv.reader(csvfile3)
for row in csvreader3:
if row[1] in d.keys():
d2[row[1]]=row[0]
except Exception as e:
print(e)
print('_main')
for a in d:
d1=list(d[a])
d1.sort()
for b in d1:
try:
i+=1
link = 'https://www.expedia.co.in/'
browser.get(link)
time.sleep(1)
#choose flights only
flights_only = browser.find_element_by_xpath("//button[@id='tab-flight-tab-hp']")
flights_only.click()
ticket_chooser(one_way_ticket,browser)
if a in d2:
dep_country_chooser(d2[a],browser)
else:
dep_country_chooser(a,browser)
if b in d2:
arrival_country_chooser(d2[b],browser)
else:
arrival_country_chooser(b,browser)
dep_date_chooser(day,month,year,browser)
#return_date_chooser('05', '01', '2020')
one_way_help(browser)
search(browser)
compile_data(browser)
if(len(dep_times_list) == 0):
continue
current_values = df.iloc[0]
cheapest_dep_time = current_values[0]
cheapest_arrival_time = current_values[1]
cheapest_airline = current_values[2]
cheapest_duration = current_values[3]
cheapest_stops = current_values[4]
cheapest_price = current_values[-1]
print('run {} completed!'.format(i))
df.to_excel('flights'+a+'_'+b+current_date+'_'+date+'.xlsx')
time.sleep(1)
except Exception as e:
print(e)
file1 = open("missed"+date+"1.txt","a+")
file1.write(a+','+b+','+day+','+month+','+year+"\n")
file1.close()
continue
colnames = ['DEP','ARR','Day','Month','Year']
data = pd.read_csv('missed'+day+month+year+'.txt', names=colnames)
DEP = data.DEP.tolist()
ARR = data.ARR.tolist()
for j in range(len(ARR)):
try:
i+=1
a = DEP[j]
b = ARR[j]
link = 'https://www.expedia.co.in/'
browser.get(link)
time.sleep(1)
#choose flights only
flights_only = browser.find_element_by_xpath("//button[@id='tab-flight-tab-hp']")
flights_only.click()
ticket_chooser(one_way_ticket,browser)
if a in d2:
dep_country_chooser(d2[a],browser)
else:
dep_country_chooser(a,browser)
if b in d2:
arrival_country_chooser(d2[b],browser)
else:
arrival_country_chooser(b,browser)
dep_date_chooser(day,month,year,browser)
#return_date_chooser('05', '01', '2020')
one_way_help(browser)
search(browser)
compile_data(browser)
if(len(dep_times_list) == 0):
continue
current_values = df.iloc[0]
cheapest_dep_time = current_values[0]
cheapest_arrival_time = current_values[1]
cheapest_airline = current_values[2]
cheapest_duration = current_values[3]
cheapest_stops = current_values[4]
cheapest_price = current_values[-1]
print('run {} completed!'.format(i))
df.to_excel('flights'+a+'_'+b+current_date+'_'+date+'.xlsx')
time.sleep(1)
except Exception as e:
print(e)
file1 = open("missed"+date+"_twice.txt","a+")
file1.write(a+','+b+','+day+','+month+','+year+"\n")
file1.close()
continue
colnames = ['DEP','ARR','Day','Month','Year']
data = pd.read_csv('missed'+day+month+year+'_twice.txt', names=colnames)
DEP = data.DEP.tolist()
ARR = data.ARR.tolist()
for j in range(len(ARR)):
try:
i+=1
a = DEP[j]
b = ARR[j]
link = 'https://www.expedia.co.in/'
browser.get(link)
time.sleep(1)
#choose flights only
flights_only = browser.find_element_by_xpath("//button[@id='tab-flight-tab-hp']")
flights_only.click()
ticket_chooser(one_way_ticket,browser)
if a in d2:
dep_country_chooser(d2[a],browser)
else:
dep_country_chooser(a,browser)
if b in d2:
arrival_country_chooser(d2[b],browser)
else:
arrival_country_chooser(b,browser)
dep_date_chooser(day,month,year,browser)
#return_date_chooser('05', '01', '2020')
one_way_help(browser)
search(browser)
compile_data(browser)
if(len(dep_times_list) == 0):
continue
current_values = df.iloc[0]
cheapest_dep_time = current_values[0]
cheapest_arrival_time = current_values[1]
cheapest_airline = current_values[2]
cheapest_duration = current_values[3]
cheapest_stops = current_values[4]
cheapest_price = current_values[-1]
print('run {} completed!'.format(i))
df.to_excel('flights'+a+'_'+b+current_date+'_'+date+'.xlsx')
time.sleep(1)
except Exception as e:
print(e)
file1 = open("missed"+date+"_thrice.txt","a+")
file1.write(a+','+b+','+day+','+month+','+year+"\n")
file1.close()
continue
if __name__ == "__main__":
print('Enter Day')
x = int(input())
print('Enter Month')
y = int(input())
print('Enter Year')
z = int(input())
print('Number of Days')
n = int(input())
flydate = datetime.date(z,y,x)
t = [None]*n
browserarr = [None]*n
for br in range(n):
browserarr[br] = webdriver.Chrome(executable_path=chrome_path)
for k in range(n):
iterdate = flydate + datetime.timedelta(days = k)
t[k] = threading.Thread(target=main, args=(str(iterdate.day),str(iterdate.month),str(iterdate.year),browserarr[k],))
for k in range(n):
t[k].start()
for k in range(n):
t[k].join()