This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
257 lines (236 loc) · 11.6 KB
/
models.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
from fake_useragent import UserAgent
import requests, time, random, datetime, os, pytz
import util
from multiprocessing.pool import Pool
from as_config import AirportSchedule_Config
from as_helpers import AirportSchedule_Helpers
from db_models import Airport, Airline, Country
class AirportSchedule(AirportSchedule_Config, AirportSchedule_Helpers):
def __init__(self, code, mode, proxy=None):
self.code = code
self.proxy = proxy
self.header = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
self.url = "https://api.flightradar24.com/common/v1/airport.json"
self.start_time, self.end_time = self.getStartEndTime()
self.params = {
"code": self.code,
"plugin-setting[schedule][timestamp]": self.start_time if self.start_time else None,
"page": 1,
"limit": 100,
"plugin[]": "schedule",
"plugin-setting[schedule][mode]": "departures"
}
@util.timing
def getStartEndTime(self):
proxy = {"http": random.choice(self.proxy)} if self.proxy else None
time_params = {
"code": self.code,
"plugin[]": "details"
}
try:
r = requests.get(self.url, params=time_params,
timeout=15, proxies=proxy, headers=self.header)
except:
return None
if r.status_code == 200:
offset = r.json()[
"result"]["response"]["airport"]["pluginData"]["details"]["timezone"]["offset"]
current_time_with_time_zone = int(
time.mktime(time.gmtime())) + offset
date = datetime.datetime.fromtimestamp(current_time_with_time_zone)
start_of_day = datetime.datetime(
date.year, date.month, date.day, 0, 0, 0, tzinfo=pytz.UTC)
end_of_day = datetime.datetime(
date.year, date.month, date.day + 1, 0, 0, 0, tzinfo=pytz.UTC)
return util.convertDateTimeToUnix(start_of_day) - offset, util.convertDateTimeToUnix(end_of_day) - offset
else:
print(r.status_code)
def __getListOfParams(self):
return [dict(self.params, page=i) for i in range(1, 16)]
def getDetails(self):
proxy = {"http": random.choice(self.proxy)} if self.proxy else None
url = "https://api.flightradar24.com/common/v1/airport.json?code={}&plugin[]=details&plugin[]=runways".format(self.code)
r = requests.get(url, timeout=15, proxies=proxy, headers=self.header)
if r.status_code == 200:
return r.json()
else:
return None
@util.timing
def getData(self):
list_params = self.__getListOfParams()
pool = Pool(os.cpu_count())
data = [flight for flights in pool.map(self.parseJson, list_params) for flight in flights
if self.checkBeforeEndTime(flight) and self.checkCargo(flight)]
offset = data[0]["flight"]["airport"]["origin"]["timezone"]["offset"]
Flight_data = pool.map(Flight, data)
return {"flights": data}
#return {"origin": self.code.upper(), "flights": [i.dictionary() for i in Flight_data], "offset": offset}
@util.timing
def getData2(self):
list_params = self.__getListOfParams()
pool = Pool(os.cpu_count())
flightList = [flight for flights in pool.map(self.parseJson, list_params) for flight in flights
if self.checkBeforeEndTime(flight) and self.checkCargo(flight)]
offset = flightList[0]["flight"]["airport"]["origin"]["timezone"]["offset"]
tz = Airport(self.code).tzByNum
dst = flightList[0]["flight"]["airport"]["origin"]["timezone"]["isDst"]
flightList = [{
"flightNum": i["flight"]["identification"]["number"]["default"],
"time": util.convertUnixToTimeStamp(i["flight"]["time"]["scheduled"]["departure"], tz, dst),
"flightTime": util.convertSecondToHour(
i["flight"]["time"]["scheduled"]["arrival"] - i["flight"]["time"]["scheduled"]["departure"]),
"actualTime": util.convertUnixToTimeStamp(
i["flight"]["time"]["real"]["departure"], tz, dst),
"airline": self.setValueforAirline(i),
"aircraft": self.setValueforAircraft(i),
"airlineIATA": self.setValueforAirlineIATA(i),
"destinationCity": self.setValueforCity(i),
"destinationAirport": self.setValueforDestinationAirport(i),
"destinationCountry": self.setValueforCountry(i),
"destinationCountryCode": self.setValueforCountryCode(i),
"destinationIATA": self.setValueforDestinationIATA(i),
"gate": i["flight"]["airport"]["origin"]["info"]["gate"],
"status": i["flight"]["status"]["text"]
} for i in flightList
if i is not None]
flightList = [dict(t) for t in {tuple(d.items()) for d in flightList}]
return {"origin": self.code.upper(), "flights": flightList, "offset": offset, "dst": dst}
@util.timing
def getCurrentData(self):
local_param = self.params
local_param["plugin-setting[schedule][timestamp]"] = None
local_param["limits"] = 40
data = [Flight(i).dictionary() for i in self.parseJson(local_param) if self.checkCargo(i)]
tz = Airport(self.code).tzByNum
dst = data[0]["origin_offset_dst"]
#data_withtime = [flight.__setitem__("sched_dep", util.convertUnixToTimeStamp(flight["sched_dep"], tz, dst)) for flight in data]
[data[i].update({"sched_dep": util.convertUnixToTimeStamp(data[i]["sched_dep"], tz, dst)}) for i in range(len(data))]
return {"flights": data}
@util.timing
def getAirportWeather(self):
local_param = self.params
local_param["plugin-setting[schedule][timestamp]"] = None
local_param["limits"] = 1
local_param["plugin-setting[schedule][mode]"] = None
local_param["plugin[]"] = None
attempt = 1
while attempt < 5:
try:
proxy = {"http": random.choice(self.proxy)} if self.proxy else None
#return requests.get(self.url, params=local_param, timeout=15, proxies=proxy, headers=self.header).json()
return requests.get(self.url, params=local_param, timeout=15, proxies=proxy, headers=self.header).json()["result"]["response"]["airport"]["pluginData"]["weather"]
except:
attempt += 1
return
def checkBeforeEndTime(self, flight):
return flight["flight"]["time"]["scheduled"]["departure"] <= self.end_time
def parseJson(self, params):
status_mode = self.params["plugin-setting[schedule][mode]"]
attempt = 1
while attempt < 5:
try:
proxy = {"http": random.choice(self.proxy)} if self.proxy else None
return requests.get(self.url, params=params, timeout=15, proxies=proxy, headers=self.header).json()["result"]["response"]["airport"]["pluginData"]["schedule"][status_mode]["data"]
except:
attempt += 1
return
def checkCargo(self, flight):
def AirlineName(flight):
try:
return flight["flight"]["airline"]["name"]
except:
return "-"
def AirlineIATA(flight):
try:
return flight["flight"]["airline"]["code"]["iata"]
except:
return "-"
def AircraftIATA(flight):
try:
return flight["flight"]["aircraft"]["model"]["code"]
except:
return "-"
name, iata, aircraftiata = AirlineName(
flight), AirlineIATA(flight), AircraftIATA(flight)
try:
if any(i in name for i in self.cargoNameList) or any(i in iata for i in self.cargoIATAList) or any(i in aircraftiata for i in self.cargoAircraftList):
return False
else:
return True
except:
return True
class Flight:
def __init__(self, data):
data = data["flight"]
self.id = util.get(data, ["identification", "row"])
self.flight_num = util.get(
data, ["identification", "number", "default"])
self.status_detail = util.get(data, ["status", "text"])
self.aircraft_code = util.get(data, ["aircraft", "model", "code"])
self.aircraft_text = util.get(data, ["aircraft", "model", "text"])
self.aircraft_reg = util.get(data, ["aircraft", "registration"])
self.aircraft_co2 = util.get(data, ["aircraft", "country", "alpha2"])
self.aircraft_restr = util.get(data, ["aircraft", "restricted"])
self.owner = util.get(data, ["owner", "name"])
self.owner_iata = util.get(data, ["owner", "code", "iata"])
self.owner_icao = util.get(data, ["owner", "code", "icao"])
self.airline = util.get(data, ["airline", "name"])
self.airline_iata = util.get(data, ["airline", "code", "iata"])
self.airline_icao = util.get(data, ["airline", "code", "icao"])
self.origin_offset = util.get(
data, ["airport", "origin", "timezone", "offset"])
self.origin_offset_abbr = util.get(
data, ["airport", "origin", "timezone", "abbr"])
self.origin_offset_dst = util.get(
data, ["airport", "origin", "timezone", "isDst"])
self.origin_terminal = util.get(
data, ["airport", "origin", "info", "terminal"])
self.origin_gate = util.get(
data, ["airport", "origin", "info", "gate"])
self.dest_iata = util.get(
data, ["airport", "destination", "code", "iata"])
self.dest_icao = util.get(
data, ["airport", "destination", "code", "icao"])
self.dest_name = util.get(data, ["airport", "destination", "name"])
self.dest_city = util.get(
data, ["airport", "destination", "position", "region", "city"])
self.dest_country = util.get(
data, ["airport", "destination", "position", "country", "name"])
self.dest_country_code = util.get(
data, ["airport", "destination", "position", "country", "code"])
self.dest_offset = util.get(
data, ["airport", "destination", "timezone", "offset"])
self.dest_offset_abbr = util.get(
data, ["airport", "destination", "timezone", "abbr"])
self.dest_offset_dst = util.get(
data, ["airport", "destination", "timezone", "isDst"])
self.dest_terminal = util.get(
data, ["airport", "destination", "info", "terminal"])
self.dest_gate = util.get(
data, ["airport", "destination", "info", "gate"])
self.sched_dep = util.get(data, ["time", "scheduled", "departure"])
self.sched_arr = util.get(data, ["time", "scheduled", "arrival"])
self.real_dep = util.get(data, ["time", "real", "departure"])
self.real_arr = util.get(data, ["time", "real", "arrival"])
self.dest_lat = util.get(
data, ["airport", "destination", "position", "latitude"])
self.dest_lng = util.get(
data, ["airport", "destination", "position", "longitude"])
def key_list(self):
return list(self.__dict__.keys())
def value_list(self):
return list(self.__dict__.values())
def dictionary(self):
return self.__dict__
if __name__ == "__main__":
from proxy import Proxy
proxy = Proxy().ip_proxies
AS = AirportSchedule("PHL", 0, proxy=proxy)
data = AS.getAirportWeather()
print(data)
#util.pprint(data)
# f = Flight(data)
print("---------")
# util.pprint(f.dictionary())