-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
249 lines (197 loc) · 7.71 KB
/
main.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
from flask import Flask, request
from icecream import ic
import requests
import http.client
import json
from domain import DOMAIN
app = Flask(__name__)
# This function takes in a vehicle registration and returns the last known location and other information about the flight
@app.route("/reg/<reg>")
def reg(reg):
"""
This function takes in a aircarft registration and returns the last known location and other information about the flight.
Parameters:
reg (str): The aircraft registration number
Returns:
A JSON object containing the following information:
- Image: The aircraft image
- Index: The index of the last known location
- aircraft info: Information about the aircraft, including registration, airline, model, and country
- flight info: Information about the last known location, including whether it is live, from and to airports, and flight number
"""
response = requests.get(
f"https://www.jetphotos.com/api/json/lastseen.php?reg={reg}"
)
response_json = response.json()
result = response_json["result"]["response"]
aircraft_info = result["aircraftInfo"]
flight_info = result["data"]
indexx = 0
for index, live in enumerate(flight_info):
if live["status"]["live"] == True:
indexx = index
try:
image = {
"src": result["aircraftImages"][0]["images"]["large"][0]["src"],
"copyright": result["aircraftImages"][0]["images"]["large"][0]["copyright"],
}
except:
image = "Not available"
try:
From = f"{flight_info[indexx]['airport']['origin']['name']} \
[{flight_info[indexx]['airport']['origin']['code']['iata']}]"
except:
From = "Not available"
try:
To = f"{flight_info[indexx]['airport']['destination']['name']} \
[{flight_info[indexx]['airport']['destination']['code']['iata']}]"
except:
To = "Not available"
try:
Registration = aircraft_info["airline"]["name"]
except:
Registration = "Not available"
try:
Airline = aircraft_info["registration"]
except:
Airline = "Not available"
try:
Model = aircraft_info["model"]["text"]
except:
Model = "Not available"
try:
Country = aircraft_info["country"]["name"]
except:
Country = "Not available"
final_response = {
"Image": image,
"Index": indexx,
"aircraft info": {
"Registration": Registration,
"Airline": Airline,
"Model": Model,
"Country": Country,
},
"flight info": {
"Is live": flight_info[indexx]["status"]["live"],
"From": From,
"To": To,
"Flight number": flight_info[indexx]["identification"]["number"]["default"],
},
}
return final_response
@app.route("/zone/<zone>")
def bound(zone):
"""
This function takes in a zone in the form of two coordinates (y1, y2 ,x1, x2) and returns a dictionary containing information about the flights in that zone.
Parameters:
zone (str): The zone in the form of two coordinates (y1, y2 ,x1, x2)
Returns:
A dictionary containing the following information:
- Zone: The zone in the form of two coordinates (y1, y2 ,x1, x2)
- Quantity: The number of flights in the zone
- Flights: A list of dictionaries, each containing information about a single flight in the zone, including its aircraft model, registration, origin, destination, and live status
"""
# Use this format in url "<zone>" for two corner of a rectangle to determine a zone: y1,y2,x1,x2
# coordinates (y1, y2 ,x1, x2)
bounds = zone.replace(",", "%2C")
additional_params = "satellite=1&mlat=1&flarm=1&adsb=1&gnd=1&air=1&vehicles=1&estimated=1&maxage=14400&gliders=1&stats=1"
response = http.client.HTTPSConnection("data-cloud.flightradar24.com")
response.request(
"GET", f"/zones/fcgi/feed.json?faa=1&bounds={bounds}&{additional_params}"
)
res = response.getresponse()
data = res.read()
data_dict = json.loads(data)
keys_of_dict = data_dict.keys()
data_dict.pop("full_count", None)
data_dict.pop("stats", None)
data_dict.pop("version", None)
list_keys_of_dict = list(keys_of_dict)
ic(list_keys_of_dict) # for logging
def fetch_zone():
url_bound = str(request)
start_index = url_bound.find("/zone/") + len("/zone/")
end_index = url_bound.find("'", start_index)
coordinates_str = url_bound[start_index:end_index]
coordinates = [float(coord) for coord in coordinates_str.split(",")]
json_cordinate = {
"1 x": coordinates[0],
"1 y": coordinates[2],
"2 x": coordinates[1],
"2 y": coordinates[3],
"Cordinates": f"{coordinates[0]}, {coordinates[2]} & {coordinates[1]}, {coordinates[3]}",
}
return json_cordinate
result = []
domain = DOMAIN # your host domain
for item in list_keys_of_dict:
flight_info = {
"1 Aircraft model": data_dict[item][8],
"2 Registration": data_dict[item][9],
"3 Register info": f"{domain}reg/{data_dict[item][9]}",
"4 Origin": data_dict[item][11],
"5 Origin weather": f"{domain}weather/{data_dict[item][11]}",
"6 Destination": data_dict[item][12],
"7 Dest weather": f"{domain}weather/{data_dict[item][12]}",
"8 Live info": f"{domain}id/{item}",
}
result.append(flight_info)
client_response = {
"1 Zone": fetch_zone(),
"2 Quantity": f"{len(result)} Flights are on this zone.",
"3 Flights": result,
}
return client_response
@app.route("/id/<id>")
def live_info(id):
"""
This function takes in an id and returns the latest status of the flight with that id.
Parameters:
id (str): The id of the flight
Returns:
A JSON object containing the latest status of the flight, including its origin, destination, airline, flight number, and status.
"""
response = requests.get(
f"https://data-live.flightradar24.com/clickhandler/?version=1.5&flight={id}"
)
response_json = response.json()
return response_json["trail"][0]
@app.route("/weather/<iata>")
def weather(iata):
"""
This function takes in an IATA code and returns the current weather at the airport.
Parameters:
iata (str): The IATA code of the airport
Returns:
A JSON object containing the current weather at the airport, including temperature, humidity, pressure, sky condition, visibility, and wind speed and direction.
Raises:
KeyError: If the IATA code is not valid
"""
try:
response = http.client.HTTPSConnection("api.flightradar24.com")
response.request("GET", f"/common/v1/airport.json?code={iata}")
res = response.getresponse()
data = res.read()
data_dict = json.loads(data)
weather = data_dict["result"]["response"]["airport"]["pluginData"]["weather"]
clean_weather_data = {
"1 temp": weather["temp"],
"2 elevation": weather["elevation"],
"3 humidity": weather["humidity"],
"4 pressure": weather["pressure"],
"5 sky_status": weather["sky"]["condition"]["text"],
"6 visibility": weather["sky"]["visibility"],
"7 wind": {
"direction": weather["wind"]["direction"],
"speed": weather["wind"]["speed"],
},
}
except KeyError:
return {"message": "Enter valid IATA code"}
return clean_weather_data
@app.route("/")
def hi():
return {"message": "hi there"}
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0")