-
Notifications
You must be signed in to change notification settings - Fork 3
/
flight.py
298 lines (227 loc) · 13.4 KB
/
flight.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
from time import sleep, strftime
import flightradar24 as fl
from bs4 import BeautifulSoup
import requests
import webbrowser
from colorama import Fore, Back, Style
api=fl.Api()
def get_key(val,dict):
for key, value in dict.items():
if val in value:
return key
return None
def get_aiport_iata(country):
all_airports = api.get_airports()
data = []
for airport in all_airports['rows']:
if airport["country"].lower() == country.lower():
temp = {}
temp["name"] = airport["name"]
temp["iata"] = airport['iata']
data.append(temp)
return data
def get_airline_code(airline_name):
all_airlines = api.get_airlines()
results = []
code = ''
for airline in all_airlines['rows']:
if airline["Name"].lower() == airline_name.lower():
code = airline['Code']
for airline in all_airlines['rows']:
if airline["Name"].lower().find(airline_name.lower()) != -1:
results.append(airline)
if code != '':
return code
else:
return results
def list_flights(arr_iata,dep_iata,year,month,date,hour):
flights = []
departure_time = []
arrival_time = []
base_url=f'https://www.flightstats.com/v2/flight-tracker/route/{arr_iata}/{dep_iata}/?year={year}&month={month}&date={date}&hour={hour}'
res=requests.get(base_url)
soup=BeautifulSoup(res.content,'html.parser')
flights_data = soup.find_all('h2',class_='flights-list-bold-text flights-list-margined leftText')
dept_time_data = soup.find_all('h2',class_='flights-list-bold-text flights-list-margined departureTimePadding')
arr_time_data = soup.find_all('h2',class_='flights-list-light-text flights-list-margined')
if len(flights_data) and len(dept_time_data) and len(arr_time_data) != 0:
for flight in flights_data:
flights.append(flight.text)
for dep_time in dept_time_data:
departure_time.append(dep_time.text)
for arr_time in arr_time_data:
arrival_time.append(arr_time.text)
return flights, departure_time, arrival_time, base_url
else:
return None
def flight_status(flight_id,year,month,date):
airline = flight_id.split()[0]
id = flight_id.split()[1]
base_url=f'https://www.flightstats.com/v2/flight-tracker/{airline}/{id}?year=20{year}&month={month}&date={date}'
print(base_url)
res=requests.get(base_url)
soup=BeautifulSoup(res.content,'html.parser')
try:
status=soup.find("div",class_='text-helper__TextHelper-sc-8bko4a-0 iicbYn').text
except:
status=soup.find("div",class_='text-helper__TextHelper-sc-8bko4a-0 iicbYn').text
dep_city=soup.find_all("div",class_='text-helper__TextHelper-sc-8bko4a-0 efwouT')[0].text
dep_airport=soup.find_all('div',class_='text-helper__TextHelper-sc-8bko4a-0 cHdMkI')[0].text
arr_city=soup.find_all("div",class_='text-helper__TextHelper-sc-8bko4a-0 efwouT')[1].text
arr_airport=soup.find_all('div',class_='text-helper__TextHelper-sc-8bko4a-0 cHdMkI')[1].text
scheduled_dep_time=soup.find_all("div",class_='text-helper__TextHelper-sc-8bko4a-0 kbHzdx')[0].text
actual_dep_time=soup.find_all("div",class_='text-helper__TextHelper-sc-8bko4a-0 kbHzdx')[1].text
scheduled_arr_time=soup.find_all("div",class_='text-helper__TextHelper-sc-8bko4a-0 kbHzdx')[2].text
actual_arr_time=soup.find_all("div",class_='text-helper__TextHelper-sc-8bko4a-0 kbHzdx')[3].text
terminal_dep = soup.find_all("div",class_='ticket__TGBValue-sc-1rrbl5o-16 hUgYLc text-helper__TextHelper-sc-8bko4a-0 kbHzdx')[0].text
gate_dep = soup.find_all("div",class_='ticket__TGBValue-sc-1rrbl5o-16 hUgYLc text-helper__TextHelper-sc-8bko4a-0 kbHzdx')[1].text
terminal_arr = soup.find_all("div",class_='ticket__TGBValue-sc-1rrbl5o-16 hUgYLc text-helper__TextHelper-sc-8bko4a-0 kbHzdx')[2].text
gate_arr = soup.find_all("div",class_='ticket__TGBValue-sc-1rrbl5o-16 hUgYLc text-helper__TextHelper-sc-8bko4a-0 kbHzdx')[3].text
return status,arr_city,arr_airport,dep_city,dep_airport,scheduled_arr_time,actual_arr_time,scheduled_dep_time,actual_dep_time,terminal_arr,gate_arr,terminal_dep,gate_dep
def main():
print()
print('''███████╗██╗ ██╗ ██████╗ ██╗ ██╗████████╗ ████████╗██████╗ █████╗ ██████╗██╗ ██╗███████╗██████╗
██╔════╝██║ ██║██╔════╝ ██║ ██║╚══██╔══╝ ╚══██╔══╝██╔══██╗██╔══██╗██╔════╝██║ ██╔╝██╔════╝██╔══██╗
█████╗ ██║ ██║██║ ███╗███████║ ██║ ██║ ██████╔╝███████║██║ █████╔╝ █████╗ ██████╔╝
██╔══╝ ██║ ██║██║ ██║██╔══██║ ██║ ██║ ██╔══██╗██╔══██║██║ ██╔═██╗ ██╔══╝ ██╔══██╗
██║ ███████╗██║╚██████╔╝██║ ██║ ██║ ██║ ██║ ██║██║ ██║╚██████╗██║ ██╗███████╗██║ ██║
╚═╝ ╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
''')
print()
print(Fore.GREEN+"--"*40+Fore.RESET)
print(Fore.CYAN+"""Tool By: Madhav :)"""+Fore.RESET)
# print(Fore.GREEN+"--"*40+Fore.RESET)
while True:
print(Fore.GREEN+"--"*40+Fore.RESET)
print(Fore.GREEN+"""
1. List of flights on a particular day
2. Flight status
3. Track Flight
99. Exit"""+Fore.RESET)
print(Fore.GREEN+"--"*40+Fore.RESET)
print()
choice = int(input(Fore.YELLOW+"Enter your choice: "+Fore.RESET))
if choice==1:
arrival_airport_country = input(Fore.GREEN+"""Enter Country Name Of the Arrival Airport: """+Fore.RESET)
arr_data = get_aiport_iata(arrival_airport_country)
if len(arr_data)==0:
print(Fore.RED+"--"*40+Fore.RESET)
print(Fore.RED+"No Airport Found"+Fore.RESET)
print(Fore.RED+"--"*40+Fore.RESET)
else:
print(Fore.YELLOW+"--"*40+Fore.RESET)
print(Fore.YELLOW+f" IATA Code of Airports In {arrival_airport_country}"+Fore.RESET)
print()
for each in arr_data:
print(Fore.YELLOW+f"{each['name']}:{each['iata']}"+Fore.RESET)
print(Fore.YELLOW+"--"*40+Fore.RESET)
arrival_airport = input(Fore.GREEN+"Enter IATA Code Of Arrival Airport: "+Fore.RESET)
departure_airport_country = input(Fore.GREEN+"""Enter Country Name Of the Departure Airport: """+Fore.RESET)
dep_data = get_aiport_iata(departure_airport_country)
if len(dep_data) == 0:
print(Fore.RED+"--"*40+Fore.RESET)
print(Fore.RED+"No Airport Found"+Fore.RESET)
print(Fore.RED+"--"*40+Fore.RESET)
else:
print(Fore.YELLOW+"--"*40+Fore.RESET)
print(Fore.YELLOW+f" IATA Code of Airports In {departure_airport_country}"+Fore.RESET)
print()
for each in dep_data:
print(Fore.YELLOW+f"{each['name']}:{each['iata']}"+Fore.RESET)
print(Fore.YELLOW+"--"*40+Fore.RESET)
dep_airport = input(Fore.GREEN+"Departure Airport: "+Fore.RESET)
date = input(Fore.GREEN+"Date(YYYY-MM-DD): "+Fore.RESET)
print()
print(Fore.CYAN+"""Select Time Interval-
1. 00:00 - 06:00
2. 06:00 - 12:00
3. 12:00 - 18:00
4. 18:00 - 00:00"""+Fore.RESET)
print()
ask_time = int(input(Fore.YELLOW+"Enter your choice: "+Fore.RESET))
if ask_time == 1:
hour = "0"
elif ask_time == 2:
hour = "6"
elif ask_time == 3:
hour = "12"
else:
hour = "18"
flights,dep_time,arr_time, base_url = list_flights(arrival_airport,dep_airport,year=date.split('-')[0],month=date.split('-')[1],date=date.split('-')[2],hour=hour)
if flights == None:
print(Fore.RED+"--"*40+Fore.RESET)
print(Fore.RED+"No Flights Found! Try using another time slot."+Fore.RESET)
print(Fore.RED+"--"*40+Fore.RESET)
else:
print(Fore.GREEN+"--"*40+Fore.RESET)
print(Fore.GREEN+" List Of Flights"+Fore.RESET)
print(Fore.GREEN+"--"*40+Fore.RESET)
for i in range(len(flights)):
print()
print(Fore.GREEN+f"{flights[i]}"+Fore.RESET)
print(Fore.GREEN+'--'*40+Fore.RESET)
print(Fore.GREEN+f'Departure Time:{dep_time[i]}'+Fore.RESET)
print(Fore.GREEN+f'Arrival Time:{arr_time[i]}'+Fore.RESET)
print(Fore.GREEN+'--'*40+Fore.RESET)
more_choice = input("Do you want to see more flights?(Y/n):")
print(more_choice)
if more_choice.lower() in ("",'\n','y'):
print(more_choice)
print(base_url)
webbrowser.open(base_url)
sleep(2)
elif choice == 2:
print()
airline_name = input(Fore.CYAN+"Enter Airline Name: "+Fore.RESET)
flight_id = input(Fore.CYAN+"Enter Flight Id: "+Fore.RESET)
result = get_airline_code(airline_name)
if type(result) == str:
code = result
else:
print(Fore.GREEN+"--"*40+Fore.RESET)
print(Fore.GREEN+f" List Of Airlines Containing {airline_name}"+Fore.RESET)
print(Fore.GREEN+"--"*40+Fore.RESET)
for i in result:
print(Fore.GREEN+f"Airline: {i['Name']}"+Fore.RESET)
continue
date = strftime('%X %x %Z').split()[1].split('/')[1]
month = strftime('%X %x %Z').split()[1].split('/')[0]
year = strftime('%X %x %Z').split()[1].split('/')[2]
data = flight_status(code + " " + flight_id,year,month,date)
print(Fore.GREEN+"--"*40+Fore.RESET)
print(Fore.GREEN+" Flight Status"+Fore.RESET)
print(Fore.GREEN+f"Status: {data[0]}"+Fore.RESET)
print(Fore.GREEN+f"Arrival City: {data[1]}"+Fore.RESET)
print(Fore.GREEN+f"Arrival Airport: {data[2]}"+Fore.RESET)
print(Fore.GREEN+f"Departure City: {data[3]}"+Fore.RESET)
print(Fore.GREEN+f"Departure Airport: {data[4]}"+Fore.RESET)
print(Fore.GREEN+f"Scheduled Arrival Time: {data[5]}"+Fore.RESET)
print(Fore.GREEN+f"Actual Arrival Time: {data[6]}"+Fore.RESET)
print(Fore.GREEN+f"Scheduled Departure Time: {data[7]}"+Fore.RESET)
print(Fore.GREEN+f"Actual Departure Time: {data[8]}"+Fore.RESET)
print(Fore.GREEN+f"Arrival Terminal: {data[9]}"+Fore.RESET)
print(Fore.GREEN+f"Arrival Gate: {data[10]}"+Fore.RESET)
print(Fore.GREEN+f"Departure Terminal: {data[11]}"+Fore.RESET)
print(Fore.GREEN+f"Departure Gate: {data[12]}"+Fore.RESET)
print(Fore.GREEN+"--"*40+Fore.RESET)
sleep(1)
elif choice == 3:
airline_name = input(Fore.CYAN+"Enter Airline Name: "+Fore.RESET)
flight_id = input(Fore.CYAN+"Enter Flight ID: "+Fore.RESET)
result = get_airline_code(airline_name)
if type(result) == str:
code = result
else:
print(Fore.GREEN+"--"*40+Fore.RESET)
print(Fore.GREEN+f" List Of Airlines Containing {airline_name}"+Fore.RESET)
print(Fore.GREEN+"--"*40+Fore.RESET)
for i in result:
print(Fore.GREEN+f"Airline: {i['Name']}"+Fore.RESET)
continue
webbrowser.open(f"https://www.flightradar24.com/{code}{flight_id}")
sleep(3)
elif choice == 99:
print(Fore.CYAN+"Good Bye."+Fore.RESET)
break
if __name__=="__main__":
main()