-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_code.py
314 lines (201 loc) · 8.5 KB
/
main_code.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
import os
from dotenv import load_dotenv
import requests
from datetime import date, datetime
from twilio.rest import Client
from with_txt_file import send_chat_log
import time
current_time = time.strftime("%H:%M:%S")
#Loads key.env file for API keys
load_dotenv('key.env')
#Following function returns basic flight statistics with real-time location and aircraft image.
def flight_data(querystring = {"withAircraftImage":"true","withLocation":"true"}):
flight = input("what is the flight number: ")
year = int(input('Enter a year: '))
month = int(input('Enter a month: '))
day = int(input('Enter a day: '))
try:
date1 = str(date(year, month, day))
except:
print("Date Error")
return flight_data()
#Standard API code for it to work
api = "https://aerodatabox.p.rapidapi.com/flights/number/"
url = api + flight +"/"+date1
headers = {
"X-RapidAPI-Key": os.getenv('flightAPI'),
"X-RapidAPI-Host": "aerodatabox.p.rapidapi.com"
}
#Standard API code for it to work
response = requests.request("GET", url, headers=headers, params=querystring)
airline = response.json()[0]["airline"]["name"]
departure = response.json()[0]["departure"]["airport"]["name"]
arrival = response.json()[0]["arrival"]["airport"]["name"]
time = response.json()[0]["departure"]["scheduledTimeUtc"]
time_list= list(time)
time_checker = ''.join(time_list[11:13])
departure_time= ''.join(time_list[11:16])
image = response.json()[0]["aircraft"]["image"]["url"]
try:
real_time_geo = response.json()[0]["location"]
list_location = list(real_time_geo.values())
lat = list_location[4]
lon = list_location[5]
lat_lon = [lat,lon]
except:
print("Real time location for this flight is not available")
#download image
r = requests.get(image)
with open('flight.jpg','wb') as f:
f.write(r.content)
print("This is a "+ airline +" flight.")
print("Flying from " + departure )
print("To " + arrival)
try:
print(lat_lon)
except:
print("")
return response.json()
x = flight_data(querystring = {"withAircraftImage":"true","withLocation":"true"})
time = x[0]["departure"]["scheduledTimeUtc"]
def departure_time():
time_list= list(time)
departure_time= ''.join(time_list[11:16])
if departure_time<current_time:
d_time = str('Your flight was departed on '+departure_time)
return d_time
else:
d_time = str('Your flight will depart on '+ departure_time)
return d_time
departure_time()
# Following fuction returns an image of map with APIs taking latitude and longitude of the aircraft.
def map(lat,lon="0.00,0.00"):
try:
list_location = list(x[0]["location"].values())
lat = str(list_location[4])
lon = str(list_location[5])
endpoint = 'https://maps.googleapis.com/maps/api/staticmap?center='
map_size = '&zoom=7&size=400x400&markers=color:red%7Clabel:O%7C'
marker = '&markers=size:mid%7Ccolor:0xFF0000%7C&key='
#All API keys are stored in a seperate file called key.env and the following line is extracting that respective map key for this function to work
API = os.getenv('maps')
image_url = endpoint + lat+','+lon + map_size + lat+','+lon + marker + API
r = requests.get(image_url)
with open('flight_map.jpg','wb') as f:
f.write(r.content)
except:
print('')
try:
map(x[0]["location"])
except:
print('')
#Link example for MAP >>>>>>>> https://maps.googleapis.com/maps/api/staticmap?center=52.47159, -1.76778&zoom=6&size=400x400&markers=color:blue%7Clabel:S%7C52.47159, -1.76778&markers=size:mid%7Ccolor:0xFFFF00%7Clabel:C%7CTok,AK%22&key=AIzaSyBJM6palbsErzflk8nXqV4wWQdH_Zdg5_E'
#Following function returns departure weather. It takes a city as parameter from the flight function and returns weather
def departure_weather(city="london"):
#Standard API code for it to work
weather_api = "https://weatherapi-com.p.rapidapi.com/current.json"
querystring = {"q":city}
headers = {
"X-RapidAPI-Key": os.getenv('weatherAPI'),
"X-RapidAPI-Host": "weatherapi-com.p.rapidapi.com"
}
#Standard API code for it to work
response = requests.request("GET", weather_api, headers=headers, params=querystring)
weather_C= response.json()["current"]["temp_c"]
sky_state = response.json()["current"]["condition"]["text"]
try:
visibility = response.json()["current"]["condition"]["vis_miles"]
except:
print("")
print("Current temperature in " + city +" is " +str(round(weather_C))+"°C")
print("Sky is "+sky_state)
try:
print(visibility)
except:
print("")
departure_weather(x[0]["departure"]["airport"]["name"])
#Following function returns arrival weather. It takes a city as parameter from the flight function and returns weather
def arrival_weather(city="london"):
#Standard API code for it to work
weather_api = "https://weatherapi-com.p.rapidapi.com/current.json"
querystring = {"q":city}
headers = {
"X-RapidAPI-Key": os.getenv('weatherAPI'),
"X-RapidAPI-Host": "weatherapi-com.p.rapidapi.com"
}
#Standard API code for it to work
response = requests.request("GET", weather_api, headers=headers, params=querystring)
weather_C= response.json()["current"]["temp_c"]
sky_state = response.json()["current"]["condition"]["text"]
try:
visibility = response.json()["current"]["condition"]["vis_miles"]
except:
print("")
print("Current temperature in " + city +" is " +str(round(weather_C))+"°C")
print("Sky is "+sky_state)
try:
print(visibility)
except:
print("")
return response.json()
arrival_weather(x[0]["arrival"]["airport"]["name"])
#Following function validates IATA airport code and returns false if code is wrong. If it is correct it passes these codes to be used in the next function.
def airport_code(airport = 'bhx',airport2 = 'lhr'):
airport = str(input("3 digits airport code. Example: Bhx for Brirmingham > "))
airport2 = str(input('3 digits airport code. Example: LHR for London > '))
if len(airport) == 3 and len(airport2) == 3:
return airport, airport2
else:
return print(False)
#Following function takes IATA airport code afrom the previous function and returns estimated travel time between two given airports.
def airportDistance():
list_arpt = list(airport_code())
airport = list_arpt[0]
airport2 = list_arpt[1]
#Standard API code for it to work
url = 'https://aerodatabox.p.rapidapi.com/airports/iata/'
urlFinal = url + airport + "/distance-time/"+ airport2
headers = {
"X-RapidAPI-Key": os.getenv('flightAPI'),
"X-RapidAPI-Host": "aerodatabox.p.rapidapi.com"
}
#Standard API code for it to work
response = requests.request("GET", urlFinal, headers=headers)
approx = response.json()["approxFlightTime"]
return print('Estimated flight time from ' +airport + ' to ' + airport2 +' is ' +approx)
airportDistance()
#Following function checks a UK number's format and returns the correct format of the number to be used in the next functio to send a message.
def number_check(number='+447196325410'):
number = str(input("number?"))
if number.startswith('+') == True:
return number
elif number.startswith('07') == True:
list_num = list(number)
num = ''.join(list_num[1:11])
number = str("+44"+num)
return number
else:
list_num = list(number)
num = ''.join(list_num[2:14])
number = str("+"+num)
return number
#Following function sends a message to the correct format number passed by previous function.
def message(time="00:00",flight_insight=x):
#Standard API code for it to work
account_sid = os.getenv('account_SID')
auth_token = os.getenv('Auth')
client = Client(account_sid, auth_token)
#Standard API code for it to work
airline = str(x[0]["airline"]["name"])
departure = str(x[0]["departure"]["airport"]["name"])
arrival = str(x[0]["arrival"]["airport"]["name"])
time = str(departure_time())
#Standard API code for it to work
message = client.messages.create(
from_='+16208378159',
body= "This is a "+ airline +" flight✈️." + " Flying from " + departure +"🛫." +" To " + arrival + "🛩️." + '⏳ ' + time,
to= number_check(),
)
print(message.sid)
#Standard API code for it to work
message(x[0]["departure"]["scheduledTimeUtc"],x[0])