-
Notifications
You must be signed in to change notification settings - Fork 0
/
live.py
164 lines (132 loc) · 4.4 KB
/
live.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
import json
import os
import requests
import sys
import webhook
import time
from datetime import datetime, timedelta
config = json.load(open("config.json", "r"))
headers = {
"accept-encoding": "gzip, br",
"accept-language": "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "max-age=0",
"origin": "https://www.flightradar24.com",
"referer": "https://www.flightradar24.com/",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
}
bounds = config.get("bounds")
regs = set(config["planes"])
activeFlights = set([])
if os.path.exists("./active.json"):
activeFlights = set(json.load(open("./active.json", "r")))
else:
with open("./active.json", "w") as f:
f.write("[]")
def getData():
try:
thisFlights = []
flights = requests.get(
f"http://data-cloud.flightradar24.com/zones/fcgi/feed.js?faa=1&satellite=1&mlat=1&flarm=1&adsb=1&gnd=1&air=1&vehicles=1&estimated=1&maxage=14400&gliders=1&stats=1&limit=1000&bounds={bounds.replace(',', '%2C')}",
headers=headers,
).json()
print("\b\b\b\b\b\b\b\b\b\b\b (a/t/e):", len(flights), end="/")
sys.stdout.flush()
for flightId in flights:
flight = flights[flightId]
if not isinstance(flight, list):
continue
if flight[9] in regs:
thisFlights.append(flightId)
except Exception as error:
print("Error getting flights:", error)
return []
finally:
return thisFlights
def sendWebhook():
webhook.sendMessage()
def launchEvent(hex):
res = requests.get(
f"https://data-live.flightradar24.com/clickhandler/?version=1.5&flight={hex}",
headers=headers,
).json()
print(res.get("aircraft").get("registration"), end="..")
sys.stdout.flush()
webhook.launchPlane(res)
def landEvent(hex):
res = requests.get(
f"https://data-live.flightradar24.com/clickhandler/?version=1.5&flight={hex}",
headers=headers,
).json()
print(res.get("aircraft", {}).get("registration"), end="..")
sys.stdout.flush()
webhook.landPlane(res)
activeFlights.remove(hex)
def run():
start = time.time()
print(datetime.now().strftime("%H:%M:%S"), "checking...", end="")
sys.stdout.flush()
flights = getData()
print(len(flights), end="/")
sys.stdout.flush()
newActive = {}
queue = []
for flight in flights:
newActive[flight] = flight
if flight not in activeFlights:
activeFlights.add(flight)
queue.append((launchEvent, flight))
for id in list(activeFlights):
if id not in newActive:
queue.append((landEvent, id))
print(len(queue), end=": ")
for item in queue:
try:
print(item[0], item[1])
item[0](item[1])
except Exception as error:
print("\n", error, "\n")
if len(queue) == 0:
print(" ", end="")
else:
json.dump(list(activeFlights), open("./active.json", "w"))
print(end="\b\b\b\b sending")
sys.stdout.flush()
activeFlights.update(newActive)
try:
sendWebhook()
except Exception as error:
print("\n", error, "\n")
diff = round(time.time() - start, 1)
print("\b\b\b\b\b\b\bdone in", str(diff) + "s!")
# waiting
if not config.get("checkHours"):
return
now = datetime.now()
hour = now.hour
if hour < config["checkHours"][0] or hour > config["checkHours"][1]:
target = now.replace(
hour=config["checkHours"][0], minute=0, second=0, microsecond=0
)
if target < now:
target += timedelta(days=1)
delay = target - now
print("sleeping for", delay.seconds, "seconds")
time.sleep(delay.seconds)
if not config.get("checkDays"):
return
now = datetime.now()
day = now.weekday()
if day not in config["checkDays"]:
delay = (now.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1)) - now
print("sleeping for", delay.seconds, "seconds")
time.sleep(delay.seconds)
def start():
while True:
try:
run()
except Exception as error:
print("\n", error, "\n")
time.sleep(config.get("interval", 60))