-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflyover
More file actions
executable file
·564 lines (520 loc) · 15.8 KB
/
flyover
File metadata and controls
executable file
·564 lines (520 loc) · 15.8 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#!/usr/bin/env bash
# Author: Pooya Moradi
# Date: 2022-03-10
# License: MIT
# Description: A simple client for flightradar24.com. Shows info of aircrafts
# flying overhead in a [latitude ± δ, longitude ± δ] vicinity
# Dependencies: jq, bc, curl, notify-send
# Note: depends on 'nominatim.openstreetmap.org' for geolocation service
version=0.25
log_level=3
use_notify=0
brief_mode=0
latitude=""
longitude=""
delta_deg=""
lat1=""
lon1=""
lat2=""
lon2=""
bounds=""
custom_bounds=""
flight_ids=""
mode=""
excl_grnd=0
details_file_tmp=""
curl_opts=()
script_tmp_dir=$(mktemp -d "flyover-tmp-XXX" -p "")
# clean up temporary variables and directories
trap 'cleanup' EXIT
print_help() {
cat << 'END_HEREDOC'
DESCRIPTION
A simple client for flightradar24.com. Shows info of aircrafts flying
overhead in a [latitude ± δ, longitude ± δ] vicinity
USAGE: flyover [OPTIONS]
OPTIONS
[-s search_str]
Search for city/region/place
Use quotes for multi-word queries
[-y latitude]
Negative for south of equator and positive for north of equator
Range: -90 <= latitude[float] <= +90
[-x longitude]
Negative for west of Prime Meridian and positive for east of Prime Meridian
Range: -180 <= longitude[float] <= +180
[-b bounds]
Geographic bounds of the region of interest
Format (comma-separated): "lat1,lon1,lat2,lon2"
[-i flight_ids]
flightradar24 flight_ids as a comma-separated list
In this mode, only the corresponding flight details are queried
Note: id is flightradar24's internal id (not ICAO or other standards)
Format example: "id1,id2,id3"
[-r delta_deg]
δ in degrees in scan square : [latitude ± δ, longitude ± δ]
Note: 1° of latitude ≈ 111 km
Range: r[float] > 0
[-o /path/to/detailed_flights]
Set output path for detailed info of flights (json array)
(implies 'detailed' mode)
[-n]
Use notification (implies 'detailed' mode)
[-f]
Set mode to 'brief', only print an augmented json
[-g]
Exclude GRND-type vehicles from presentation
(only applies to STDOUT and notifications of 'detailed' mode)
[-v log_level]
v = 0 : No logs. Equivalent to -q
v = 1 : Set level to error
v = 2 : Set level to warn
v = 3 : Set level to info (default)
v = 4 : Set level to debug
[-q]
Set log level to v = 0
[-V]
Print version
[-h]
Print help message
NOTES
- latitude/longitude should be input in decimal degrees (not DMS)
- Either use -s (geolocation service) OR (-y,-x) but NOT both
- Default mode of operation is 'detailed'
EXAMPLES
flyover -s "deylaman" -r 2
flyover -s "قلعه گبری" -r 0.5 -n -v4
flyover -y "-27.115" -x "-109.395" -r 24.15 -o /tmp/detailed.json
flyover -s "深圳" -qf
flyover -i "2b1abd2f,2b1cae23"
flyover -b "43.58,58.72,46.58,61.72"
flyover -s "oslo" -g
END_HEREDOC
}
cleanup() {
rm -rf "$script_tmp_dir"
}
log() {
local mode
case "$1" in
"debug")
mode="debug"
((log_level < 4)) && return
;;
"info")
mode="info"
((log_level < 3)) && return
;;
"warn")
mode="warn"
((log_level < 2)) && return
;;
"error")
mode="error"
((log_level < 1)) && return
;;
esac
printf >&2 "[%-5s] %s\n" "$mode" "$2"
}
fetch() {
local url="$1"
if ! curl --silent --fail --retry-connrefused --retry 3 \
"${curl_opts[@]}" \
--get "$url"; then
return 1
fi
}
sanitize_latlon() {
local latitude="$1"
local longitude="$2"
if jq -e -n "$latitude < -90 or $latitude > 90 or \
$longitude < -180 or $longitude > 180" > /dev/null; then
log error "Bad usage! latitude/longitude out of range"
return 2
fi
#remove +- signs from numbers
if ! printf "%f %f\n" "$latitude" "$longitude"; then
log error "Bad usage! latitude/longitude are invalid numbers"
return 2
fi
}
sanitize() {
local result
if ((use_notify)) || [[ -n $details_file ]]; then
brief_mode=0
fi
if ((brief_mode)); then
log debug "Mode is set to 'brief'"
else
log debug "Mode is set to 'detailed'"
fi
if [[ -n "$flight_ids" ]]; then
if [[ -n "$latitude" || -n "$longitude" || -n "$query_str" ||
-n "$delta_deg" ]]; then
log warn "Ignoring search_city/latitude/longitude/delta_deg/bounds"
fi
mode="direct-ids"
return 0
fi
if [[ -n "$bounds" ]]; then
if [[ -n "$latitude" || -n "$longitude" || -n "$query_str" ||
-n "$delta_deg" ]]; then
log warn "Ignoring search_city/latitude/longitude/delta_deg."
fi
mode="scan_bounds"
# remove all whilespaces from the list
bounds="${bounds//[[:space:]]/}"
IFS="," read -r lat1 lon1 lat2 lon2 <<< "$bounds"
if [[ -z "$lat1" || -z "$lon1" || -z "$lat2" || -z "$lon2" ]]; then
log error "Bad usage! lat1/lon1/lat2/lon2 are not given"
return 2
fi
if ! result="$(sanitize_latlon "$lat1" "$lon1")"; then
return 2
fi
read -r lat1 lon1 <<< "$result"
if ! result="$(sanitize_latlon "$lat2" "$lon2")"; then
return 2
fi
read -r lat2 lon2 <<< "$result"
return 0
fi
if [[ -n "$query_str" ]]; then
if [[ -n "$latitude" || -n "$longitude" ]]; then
log warn "Ignoring latitude/longitude"
fi
if ! result="$(geolocate "$query_str")"; then
return 2
fi
read -r latitude longitude <<< "$result"
else
if [[ -z "$latitude" || -z "$longitude" ]]; then
log error "latitude/longitude is not provided"
return 2
fi
if ! result="$(sanitize_latlon "$latitude" "$longitude")"; then
return 2
fi
read -r latitude longitude <<< "$result"
fi
if [[ -z "$delta_deg" ]]; then
log info "Using default r: δ = 1°"
delta_deg=1
else
if ! jq -e -n "$delta_deg > 0" > /dev/null; then
log error "Bad usage! delta_deg invalid or out of range"
return 2
fi
fi
mode="scan_bounds"
return 0
}
geolocate() {
local query="$1"
local latitude longitude
local response
local endpoint="https://nominatim.openstreetmap.org"
curl_opts=()
curl_opts+=(--data-urlencode "q=$query")
curl_opts+=(--data-urlencode "format=json")
if ! response=$(fetch \
"$endpoint/search"); then
log error "Problem in connecting to $endpoint"
return 1
fi
log info "Match found: $(jq '.[0].display_name' <<< "$response")"
read -r latitude longitude < \
<(jq -r '.[0] | .lat + " " + .lon' <<< "$response")
if [[ -z "$latitude" || -z "$longitude" ]]; then
log error "No match was found for : $query"
return 1
fi
log info "'$query' WGS84 coordinates at latitude=$latitude, longitude=$longitude"
printf "%s %s\n" "$latitude" "$longitude"
}
get_flights() {
local bounds="$1"
local endpoint="https://data-cloud.flightradar24.com"
local config="faa=1&satellite=1&mlat=1&flarm=1&adsb=1&gnd=1&air=1&\
vehicles=1&estimated=1&maxage=14400&gliders=1&stats=1"
curl_opts=()
local response
if ! response=$(fetch \
"$endpoint/zones/fcgi/feed.js?$config&bounds=$bounds"); then
log error "Problem in connecting to $endpoint"
return 1
fi
printf "%s\n" "$response"
}
generate_bounds() {
local delta="$1"
local latitude="$2"
local longitude="$3"
local y1 y2 x1 x2
if ! { read -r y1 && read -r x1 && read -r y2 && read -r x2; } < \
<(
bc --quiet --standard --warn <<< \
"scale=10;
$latitude-$delta;
$longitude-$delta;
$latitude+$delta;
$longitude+$delta;
"
); then
return 1
fi
# note the bound order required for flightradar24
printf "%f,%f,%f,%f\n" "$y2" "$y1" "$x1" "$x2"
}
get_flight_details() {
local id="$1"
local endpoint="https://data-live.flightradar24.com"
local response
curl_opts=()
if ! response=$(fetch \
"$endpoint/clickhandler/?version=1.5&flight=$id"); then
log error "Problem in connecting to $endpoint"
return 1
fi
printf "%s\n" "$response"
}
filter_flights() {
local flights="$1"
local latitude="$2"
local longitude="$3"
local delta_deg="$4"
local filter="
del(.[] | arrays | select(
pow(.[1] - \$lat ; 2) > pow(\$delta; 2) or
pow(.[2] - \$lon ; 2) > pow(\$delta; 2)))
"
jq --argjson lat "$latitude" --argjson lon "$longitude" \
--argjson delta "$delta_deg" \
"$filter" \
<<< "$flights"
}
# Augments the output of get_flights (brief json) with explanatory keys
# The correspondence of array elements assignment is based on:
# https://github.com/JeanExtreme002/FlightRadarAPI/blob/main/python/FlightRadar24/entities/flight.py
gen_brief() {
local brief="$1"
jq '
. as $x
| (del (.stats, .version, .full_count)) as $y
| [($y | keys[] as $id
| {
"id" : $id,
"icao_hex" : $y.[$id][0],
"lat" : $y.[$id][1],
"lng" : $y.[$id][2],
"hd" : $y.[$id][3],
"alt" : $y.[$id][4],
"spd" : $y.[$id][5],
"squawk" : $y.[$id][6],
"feeder_id" : $y.[$id][7],
"aircraft_model" : $y.[$id][8],
"aircraft_reg" : $y.[$id][9],
"timestamp" : $y.[$id][10],
"airport_orig" : $y.[$id][11],
"airport_dest" : $y.[$id][12],
"flight_num" : $y.[$id][13],
"on_ground" : $y.[$id][14],
"vertical_speed" : $y.[$id][15],
"callsign" : $y.[$id][16],
"airline_icao" : $y.[$id][18]
})] as $flights
|
{
"full_count": $x.full_count,
"version": $x.version,
$flights,
"stats" : $x.stats,
}
' <<< "$brief"
}
show() {
local detail="$1"
local msg
local flightradar_link
local link_handle
local filter='
.aircraft.images.thumbnails[0].src + "\n" +
.airline.name + ", " +
.identification.number.default + "/" + .identification.callsign + "\n" +
"✈️ " + .aircraft.model.text + "\n" +
"↗️ " + .airport.origin.position.country.name + ", " +
.airport.origin.position.region.city + "\n" +
"↘️ " + .airport.destination.position.country.name + ", " +
.airport.destination.position.region.city + "\n" +
"📅 " + .status.text + "\n" +
"📏 " +
try ((.trail[0].alt * 0.0003048) | floor | tostring) catch ("?") + "km" +
", " +
try ((.trail[0].spd * 1.8520000) | floor | tostring) catch ("?") + "km/h"
'
# apply presentation filter
if ((excl_grnd)); then
if jq -e '.aircraft.model.code | test("grnd"; "i")' \
> /dev/null <<< "$detail"; then
log debug "Skipping GRND-type vehicle"
return
fi
fi
msg=$(jq -r "$filter" <<< "$detail")
# img_link of airplane body
local airplane_img_link
# title = airline_name + flight_number/flight_callsign
local title
{ read -r airplane_img_link && read -r title; } <<< "$msg"
msg=$(tail -n+3 <<< "$msg")
link_handle=$(jq -r '.identification as $id
| ($id.callsign + "/" + $id.id)' <<< "$detail")
flightradar_link="https://www.flightradar24.com/$link_handle"
printf "———————\n%s\n%s\n%s\n" "$title" "$msg" "$flightradar_link"
if [[ "$use_notify" == 1 ]]; then
local img_path
local ua="User-Agent: Mozilla/5.0 \
(X11; Ubuntu; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0"
if [[ -n "$airplane_img_link" ]]; then
img_path=$(mktemp "$script_tmp_dir/img-XXX")
curl_opts=()
curl_opts+=(--header "$ua" --output "$img_path")
fetch "$airplane_img_link"
fi
[[ -z "$title" ]] && title=" "
notify-send "$title" "$msg\n$flightradar_link" \
--icon "$img_path" --expire-time 10000
fi
}
while getopts s:r:y:x:b:i:v:o:gfnqVh name; do
case $name in
s)
query_str="$OPTARG"
;;
r)
delta_deg="$OPTARG"
;;
y)
latitude="$OPTARG"
;;
x)
longitude="$OPTARG"
;;
b)
bounds="$OPTARG"
custom_bounds=1
;;
i)
flight_ids="$OPTARG"
;;
v)
log_level="$OPTARG"
;;
o)
details_file="$OPTARG"
brief_mode=0
;;
f)
brief_mode=1
;;
g)
excl_grnd=1
;;
n)
use_notify=1
brief_mode=0
;;
q)
log_level=0
;;
V)
printf "%s\n" "$version"
exit 0
;;
h)
print_help
exit 0
;;
?)
log error "Bad usage! Check -h"
exit 2
;;
esac
done
# sanitization
if ! sanitize; then
exit 2
fi
case $mode in
"scan_bounds")
if [[ -z "$custom_bounds" ]]; then
# creating bounds based on inputted city/(lat/lon) and delta_deg
if ! bounds=$(generate_bounds \
"$delta_deg" "$latitude" "$longitude"); then
log error "Problem in generating bounds. Check -h"
exit 2
fi
log info "Scan region for δ=$delta_deg°: $bounds"
else
# reforming the directly inputed bounds
bounds=$(printf "%s,%s,%s,%s\n" "$lat2" "$lat1" "$lon1" "$lon2")
log info "Scan region: $bounds"
fi
# get all flights in region defined by bounds
if ! flights=$(get_flights "$bounds"); then
exit 1
fi
# get only flights that are within the desired geographical region
# if [[ -z "$custom_bounds" ]]; then
# filtered_flights=$(filter_flights "$flights" \
# "$latitude" "$longitude" "$delta_deg")
# else
# filtered_flights="$flights"
# fi
filtered_flights="$flights"
log debug "Flights brief info: $(jq -c . <<< "$filtered_flights")"
;;
"direct-ids")
flight_ids="${flight_ids//[[:space:]]/}"
filtered_flights="{}"
old_ifs="$IFS"
IFS=","
for id in $flight_ids; do
flights_tmp=$(jq \
". + {\"$id\" : []}" <<< "$filtered_flights")
filtered_flights="$flights_tmp"
done
IFS="$old_ifs"
;;
esac
if [[ -n "$details_file" ]]; then
details_file_tmp=$(mktemp "$script_tmp_dir/details-XXX.json")
printf "" > "$details_file"
fi
# handle brief mode
if ((brief_mode)); then
gen_brief "$filtered_flights"
exit
fi
# handle detailed mode
# loop over `flight_id`s and print their info
while read -r id; do
if ! detail=$(get_flight_details "$id"); then
log error "Problem getting details of flight_id: $id"
exit 1
fi
if [[ -n "$details_file" ]]; then
# append details json (one line) to details_file
jq --compact-output '.' <<< "$detail" >> "$details_file_tmp"
fi
# present the details of matched flight
show "$detail"
done < <(jq -r 'del (.stats, .version, .full_count) | keys[]' \
<<< "$filtered_flights")
# slurp the details_file as a single json array
if [[ -n "$details_file" ]]; then
swap_tmp=$(mktemp "$script_tmp_dir/details_tmp-XXX")
cp "$details_file_tmp" "$swap_tmp"
jq --slurp '.' "$swap_tmp" > "$details_file_tmp"
cp "$details_file_tmp" "$details_file"
fi