-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
95 lines (72 loc) · 2.18 KB
/
main.py
File metadata and controls
95 lines (72 loc) · 2.18 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
#!/usr/bin/env python3
# main.py
import requests
from datetime import datetime, timezone
from rich.console import Console
from rich.table import Table
from rich.prompt import Prompt
from rich.panel import Panel
from rich.text import Text
console = Console()
def header():
txt = Text(
"✈ Flight Data — Terminal Edition ✈",
justify="center",
style="bold white on blue"
)
console.print(Panel(txt, border_style="bright_magenta", title="Welcome"))
def find_flights(q):
url = "https://opensky-network.org/api/states/all"
try:
r = requests.get(url, timeout=10)
r.raise_for_status()
data = r.json()
except Exception as err:
console.print(f"[red]Failed to fetch data:[/red] {err}")
return
results = []
for s in data.get("states", []):
callsign = (s[1] or "").strip()
if not callsign or q.upper() not in callsign.upper():
continue
try:
last_seen = datetime.fromtimestamp(s[4], timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
except:
last_seen = "N/A"
results.append({
"callsign": callsign,
"icao": s[0],
"country": s[2],
"alt": s[7] if s[7] else "N/A",
"seen": last_seen,
"lat": s[6],
"lon": s[5],
})
if not results:
console.print(f"[yellow]No flights found for '{q}'[/yellow]")
return
table = Table(title=f"Flights matching '{q}'", show_lines=True)
cols = ["Callsign", "ICAO24", "Country", "Altitude (m)", "Last Seen", "Latitude", "Longitude"]
for c in cols:
table.add_column(c, justify="center")
for f in results:
table.add_row(
f["callsign"],
f["icao"],
f["country"],
str(f["alt"]),
f["seen"],
str(f["lat"]),
str(f["lon"]),
)
console.print(table)
def main():
header()
while True:
q = Prompt.ask("\nCallsign (or 'exit')")
if q.lower() == "exit":
console.print("[green]bye[/green]")
break
find_flights(q)
if __name__ == "__main__":
main()