forked from h9zdev/GeoSentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1464 lines (1240 loc) · 61.1 KB
/
app.py
File metadata and controls
1464 lines (1240 loc) · 61.1 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import io
import json
import csv
import time
import math
import random
import logging
import base64
import requests
import feedparser
import threading
import sqlite3
import numpy as np
import cv2 # opencv-python-headless or opencv-python
from datetime import datetime, timedelta, timezone
from functools import wraps
from flask import Flask, render_template, jsonify, request, redirect, url_for, make_response, send_from_directory, g
from werkzeug.security import generate_password_hash, check_password_hash
from PIL import Image, ExifTags
# Import local configs
try:
from news_config import NEWS_SOURCES
except ImportError:
NEWS_SOURCES = {}
# -----------------------------------------------------------------
# Configuration & Keys
# -----------------------------------------------------------------
# NOTE: Paths are adjusted to work from HayOS/github/ assuming HayOS/ is parent
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
PARENT_DIR = os.path.dirname(BASE_DIR)
GEODATA_DIR = os.path.join(PARENT_DIR, 'geodata')
# API Keys (Copied from HayOS.py)
NEWS_API_KEY = "API_KEY"
OPENROUTER_API_KEY = "API_KEY"
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret-key'
app.config['UPLOAD_FOLDER'] = os.path.join(BASE_DIR, 'uploads')
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
# -----------------------------------------------------------------
# Caches & Globals
# -----------------------------------------------------------------
news_cache = {}
NEWS_CACHE_LIMIT = 15 # minutes
#
# -----------------------------------------------------------------
# Database & Auth Helpers
# -----------------------------------------------------------------
@app.route('/earth')
def earth():
# 2. List GeoJSON files
geodata_dir = os.path.join(app.root_path, 'geodata')
geojson_files = []
if os.path.exists(geodata_dir):
geojson_files = [f for f in os.listdir(geodata_dir) if f.endswith('.geojson')]
return render_template("earth.html", geojson_files=geojson_files)
@app.route('/api/geojson/<filename>')
def get_geojson_data(filename):
"""Return a summary of the GeoJSON file (properties and first few coords to keep it snappy)."""
# Security check: prevent directory traversal
if '..' in filename or filename.startswith('/'):
return jsonify({"error": "Invalid filename"}), 400
filepath = os.path.join(app.root_path, 'geodata', filename)
if not os.path.exists(filepath):
return jsonify({"error": "File not found"}), 404
try:
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
# Return a larger sample for map visualization
features = data.get('features', [])
summary_features = []
for feat in features[:500]: # Increased to 500 for map display
summary_features.append({
"type": feat.get("type"),
"properties": feat.get("properties"),
"geometry": feat.get("geometry") # Include full geometry for Leaflet
})
return jsonify({
"filename": filename,
"total_features": len(features),
"summary": summary_features
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/geo/index')
def get_geo_index():
"""Return the surveillance grid index."""
filepath = os.path.join(app.root_path, 'geodata', 'geo', 'index.json')
if not os.path.exists(filepath):
return jsonify({"error": "Index not found"}), 404
try:
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/geo/tile/<z>/<x>/<y>')
def get_geo_tile(z, x, y):
"""Return a specific surveillance grid tile."""
# Security check: ensure z, x, y are integers to prevent path traversal
try:
z = int(z)
x = int(x)
y = int(y)
except ValueError:
return jsonify({"error": "Invalid tile coordinates"}), 400
filepath = os.path.join(app.root_path, 'geodata', 'geo', str(z), str(x), f"{y}.json")
if not os.path.exists(filepath):
return jsonify({"error": "Tile not found"}), 404
try:
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
return jsonify(data)
except Exception as e:
return jsonify({"error": str(e)}), 500
# Live flight data configuration
ADSB_REGIONS = [
# North America
("https://opendata.adsb.fi/api/v3/lat/40/lon/-100/dist/250", "NorthAmerica_Central"),
("https://opendata.adsb.fi/api/v3/lat/45/lon/-75/dist/250", "USA_East"),
("https://opendata.adsb.fi/api/v3/lat/35/lon/-120/dist/250", "USA_West"),
("https://opendata.adsb.fi/api/v3/lat/30/lon/-90/dist/250", "USA_South"),
("https://opendata.adsb.fi/api/v3/lat/50/lon/-110/dist/250", "Canada_West"),
# Europe
("https://opendata.adsb.fi/api/v3/lat/50/lon/10/dist/250", "Europe_Central"),
("https://opendata.adsb.fi/api/v3/lat/51/lon/0/dist/250", "Europe_UK"),
("https://opendata.adsb.fi/api/v3/lat/40/lon/-3/dist/250", "Europe_Spain"),
("https://opendata.adsb.fi/api/v3/lat/45/lon/5/dist/250", "Europe_France"),
("https://opendata.adsb.fi/api/v3/lat/42/lon/12/dist/250", "Europe_Italy"),
("https://opendata.adsb.fi/api/v3/lat/52/lon/21/dist/250", "Europe_Poland"),
# Asia
("https://opendata.adsb.fi/api/v3/lat/28/lon/77/dist/250", "SouthAsia_India"),
("https://opendata.adsb.fi/api/v3/lat/19/lon/72/dist/250", "India_Mumbai"),
("https://opendata.adsb.fi/api/v3/lat/35/lon/139/dist/250", "Japan_Tokyo"),
("https://opendata.adsb.fi/api/v3/lat/31/lon/121/dist/250", "China_Shanghai"),
("https://opendata.adsb.fi/api/v3/lat/22/lon/114/dist/250", "China_HongKong"),
("https://opendata.adsb.fi/api/v3/lat/13/lon/100/dist/250", "SEAsia_Thailand"),
("https://opendata.adsb.fi/api/v3/lat/1/lon/103/dist/250", "SEAsia_Singapore"),
# Russia
("https://opendata.adsb.fi/api/v3/lat/55/lon/37/dist/250", "Russia_Moscow"),
("https://opendata.adsb.fi/api/v3/lat/55/lon/82/dist/250", "Russia_Siberia"),
("https://opendata.adsb.fi/api/v3/lat/43/lon/131/dist/250", "Russia_FarEast"),
# Oceania
("https://opendata.adsb.fi/api/v3/lat/-33/lon/151/dist/250", "Australia_East"),
("https://opendata.adsb.fi/api/v3/lat/-37/lon/144/dist/250", "Australia_South"),
# South America
("https://opendata.adsb.fi/api/v3/lat/-23/lon/-46/dist/250", "SouthAmerica_Brazil"),
("https://opendata.adsb.fi/api/v3/lat/-34/lon/-58/dist/250", "SouthAmerica_Argentina"),
("https://opendata.adsb.fi/api/v3/lat/4/lon/-74/dist/250", "SouthAmerica_Colombia"),
# Africa
("https://opendata.adsb.fi/api/v3/lat/30/lon/31/dist/250", "Africa_Egypt"),
("https://opendata.adsb.fi/api/v3/lat/-26/lon/28/dist/250", "Africa_South"),
("https://opendata.adsb.fi/api/v3/lat/-1/lon/36/dist/250", "Africa_East"),
("https://opendata.adsb.fi/api/v3/lat/6/lon/3/dist/250", "Africa_West"),
# Middle East
("https://opendata.adsb.fi/api/v3/lat/25/lon/55/dist/250", "MiddleEast_Dubai"),
("https://opendata.adsb.fi/api/v3/lat/32/lon/34/dist/250", "MiddleEast_Israel"),
("https://opendata.adsb.fi/api/v3/lat/24/lon/46/dist/250", "MiddleEast_Saudi"),
]
def fetch_opensky_data():
"""Fetch global state vectors from OpenSky Network (Free API)."""
try:
# OpenSky global states (anonymous access has rate limits but works)
url = "https://opensky-network.org/api/states/all"
response = requests.get(url, timeout=15)
if response.status_code == 200:
data = response.json()
states = data.get('states', [])
aircraft = []
for s in states:
if s[5] is None or s[6] is None: continue # No lat/lon
aircraft.append({
'hex': s[0],
'flight': (s[1] or '').strip(),
'r': '', # Registration not in simple states
't': '', # Type not in simple states
'lat': s[6],
'lon': s[5],
'alt_baro': s[7],
'gs': s[9],
'track': s[10],
'squawk': s[14] or '----',
'source': 'OpenSky'
})
return aircraft
except Exception as e:
print(f"OpenSky Fetch Error: {e}")
return []
@app.route('/api/geo/regions')
@login_required
def get_flight_regions():
"""Return the list of configured ADSB regions."""
regs = [{"name": r[1], "id": i} for i, r in enumerate(ADSB_REGIONS)]
# Add OpenSky as a virtual region
regs.append({"name": "Global_OpenSky", "id": -1})
return jsonify(regs)
from concurrent.futures import ThreadPoolExecutor
def fetch_region_data(url, region_name, headers):
"""Helper to fetch data for a single region."""
try:
response = requests.get(url, headers=headers, timeout=10)
if response.status_code == 200:
return response.json().get('ac', [])
except Exception as e:
print(f"Error fetching {region_name}: {e}")
return []
@app.route('/api/geo/flights')
@login_required
def get_flight_data():
"""Fetch live flight data from adsb.fi API (comprehensive global coverage using v3)."""
search_q = request.args.get('q', '').strip().upper()
region_idx = request.args.get('region_idx', type=int)
headers = {"User-Agent": "SkyScanFlightTracker/1.0 (HayOS Uplink)"}
all_flights = {} # Use dict to dedupe by hex
selected_regions = []
if region_idx is not None and 0 <= region_idx < len(ADSB_REGIONS):
selected_regions = [ADSB_REGIONS[region_idx]]
else:
selected_regions = ADSB_REGIONS
# Use ThreadPoolExecutor for high-parallelism fetching
aircraft_data = []
# Always fetch all regions and OpenSky for a unified view
with ThreadPoolExecutor(max_workers=25) as executor:
# 1. Fetch OpenSky
os_future = executor.submit(fetch_opensky_data)
# 2. Fetch all ADSB regions
# If a specific region_idx is provided, we still obey it for targeted scans,
# but the default (no region_idx) will fetch EVERYTHING.
target_regions = selected_regions if region_idx is not None else ADSB_REGIONS
adsb_futures = [executor.submit(fetch_region_data, url, name, headers) for url, name in target_regions]
# 3. Combine results
aircraft_data.extend(os_future.result())
for future in adsb_futures:
aircraft_data.extend(future.result())
for ac in aircraft_data:
# Skip if no position data
if ac.get('lat') is None or ac.get('lon') is None:
continue
hex_code = (ac.get('hex') or ac.get('icao') or '').upper()
if not hex_code or hex_code in all_flights:
continue # Already have this aircraft
# Field mapping with better fallbacks
# ADSB.fi uses 'flight' for callsign, 'r' for registration, 't' for type
# OpenSky uses 'flight' for callsign
callsign = (ac.get('flight', '') or '').strip()
registration = ac.get('r', '') or ac.get('reg', '')
aircraft_type = ac.get('t', '') or ac.get('type', '')
squawk = ac.get('squawk') or '----'
# If callsign is missing, use hex
if not callsign: callsign = hex_code
# Apply search filter if provided
if search_q:
if search_q not in hex_code and search_q not in callsign.upper() and search_q not in (registration or '').upper():
continue
# Type classification with color coding
# Military detection
mil_prefixes = ['RCH', 'SPAR', 'SAM', 'AF1', 'MAGMA', 'ASCOT', 'BAF', 'GAF',
'PLF', 'DUKE', 'NAVY', 'COBRA', 'VIPER', 'REACH', 'EVAC']
mil_types = ['C17', 'C130', 'C5', 'KC135', 'KC10', 'F15', 'F16', 'F18',
'F22', 'F35', 'B52', 'B1', 'B2', 'E3', 'E6', 'P8', 'V22']
is_mil = any(callsign.upper().startswith(p) for p in mil_prefixes) or \
any(t in (aircraft_type or '').upper() for t in mil_types)
# Private aircraft detection
priv_types = ['C172', 'C182', 'C208', 'PA28', 'SR22', 'TBM9', 'PC12', 'CL60', 'C152', 'PA32']
is_priv = (callsign.startswith('N') and len(callsign) <= 6) or \
callsign.startswith('G-') or callsign.startswith('VH-') or \
(aircraft_type or '').upper() in priv_types
# Emergency detection
is_emergency = ac.get('emergency', 'none') != 'none' or squawk == '7700'
# Default to commercial (blue) - all flights visible!
f_type = "commercial"
if is_emergency: f_type = "emergency"
elif is_mil: f_type = "military"
elif is_priv: f_type = "private"
all_flights[hex_code] = {
"icao24": hex_code.lower(),
"callsign": callsign,
"registration": registration or "---",
"aircraft_type": aircraft_type or "---",
"long": ac.get('lon') or ac.get('lon'), # Handle different param names if any
"lat": ac.get('lat') or ac.get('lat'),
"alt": ac.get('alt_baro') or ac.get('alt_geom') or ac.get('alt') or 0,
"velocity": ac.get('gs') or ac.get('speed') or 0,
"heading": ac.get('track') or ac.get('heading') or 0,
"squawk": squawk,
"type": f_type,
"source": ac.get('source', 'ADSB.fi')
}
return jsonify(list(all_flights.values()))
# --- VESSEL HARBOR UPLINK ---
# Global cache for AIS data
_ais_vessels_cache = {}
_ais_cache_lock = None
_ais_websocket_task = None
def start_ais_websocket():
"""Start background WebSocket connection to AISstream.io"""
import asyncio
import websockets
import json
import threading
from threading import Lock
global _ais_cache_lock, _ais_websocket_task
_ais_cache_lock = Lock()
async def ais_stream():
global _ais_vessels_cache
api_key = "API_KEY"
async with websockets.connect("wss://stream.aisstream.io/v0/stream") as websocket:
# Subscribe to global ship positions
subscribe_message = {
"APIKey": api_key,
"BoundingBoxes": [[[-90, -180], [90, 180]]] # Global coverage
}
await websocket.send(json.dumps(subscribe_message))
print("AISstream.io connected - receiving real ship data...")
async for message_json in websocket:
try:
message = json.loads(message_json)
# Handle Position Reports
if "Message" in message and "PositionReport" in message["Message"]:
pos = message["Message"]["PositionReport"]
meta = message.get("MetaData", {})
mmsi = str(meta.get("MMSI", "000000000"))
ship_name = meta.get("ShipName", "UNKNOWN").strip()
vessel_data = {
"mmsi": mmsi,
"name": ship_name if ship_name else "UNKNOWN",
"lat": pos.get("Latitude", 0),
"lon": pos.get("Longitude", 0),
"heading": int(pos.get("TrueHeading", 0) or pos.get("Cog", 0) or 0),
"speed": float(pos.get("Sog", 0) or 0),
"type": _ais_vessels_cache.get(mmsi, {}).get("type", "cargo"), # Keep existing type
"imo": meta.get("IMO", "---"),
"status": pos.get("NavigationalStatus", "Underway"),
"country": _ais_vessels_cache.get(mmsi, {}).get("country", "--"), # Keep existing country
"draft": 0,
"arrival": meta.get("Destination", "Unknown"),
"callsign": meta.get("CallSign", "---"),
"source": "AISstream_LIVE",
"atd": "---",
"departure": "---",
"category": _ais_vessels_cache.get(mmsi, {}).get("type", "cargo")
}
with _ais_cache_lock:
_ais_vessels_cache[mmsi] = vessel_data
# Handle Ship Static Data (has ship type and country)
elif "Message" in message and "ShipStaticData" in message["Message"]:
static = message["Message"]["ShipStaticData"]
meta = message.get("MetaData", {})
mmsi = str(meta.get("MMSI", "000000000"))
ship_type_code = static.get("Type", 0)
# Map AIS ship type codes to readable types
type_map = {
range(30, 40): "fishing",
range(40, 50): "tug",
range(50, 60): "pilot",
range(60, 70): "passenger",
range(70, 80): "cargo",
range(80, 90): "tanker",
range(35, 36): "military",
range(51, 52): "special"
}
ship_type = "cargo" # default
for code_range, type_name in type_map.items():
if ship_type_code in code_range:
ship_type = type_name
break
# Get country from UserID (first 3 digits of MMSI = Maritime Identification Digits)
mid = mmsi[:3]
country_map = {
'202': 'GB', '203': 'ES', '204': 'PT', '205': 'BE', '206': 'FR',
'207': 'FR', '208': 'FR', '209': 'CY', '210': 'CY', '211': 'DE',
'212': 'CY', '213': 'GE', '214': 'MD', '215': 'MT', '216': 'AM',
'218': 'DE', '219': 'DK', '220': 'DK', '224': 'ES', '225': 'ES',
'226': 'FR', '227': 'FR', '228': 'FR', '229': 'MT', '230': 'FI',
'231': 'FO', '232': 'GB', '233': 'GB', '234': 'GB', '235': 'GB',
'236': 'GI', '237': 'GR', '238': 'HR', '239': 'GR', '240': 'GR',
'241': 'GR', '242': 'MA', '243': 'HU', '244': 'NL', '245': 'NL',
'246': 'NL', '247': 'IT', '248': 'MT', '249': 'MT', '250': 'IE',
'251': 'IS', '252': 'LI', '253': 'LU', '254': 'MC', '255': 'PT',
'256': 'MT', '257': 'NO', '258': 'NO', '259': 'NO', '261': 'PL',
'262': 'ME', '263': 'PT', '264': 'RO', '265': 'SE', '266': 'SE',
'267': 'SK', '268': 'SM', '269': 'CH', '270': 'CZ', '271': 'TR',
'272': 'UA', '273': 'RU', '274': 'MK', '275': 'LV', '276': 'EE',
'277': 'LT', '278': 'SI', '279': 'RS', '301': 'AI', '303': 'US',
'304': 'AG', '305': 'AG', '306': 'CW', '307': 'AW', '308': 'BS',
'309': 'BS', '310': 'BM', '311': 'BS', '312': 'BZ', '314': 'BB',
'316': 'CA', '319': 'KY', '321': 'CR', '323': 'CU', '325': 'DM',
'327': 'DO', '329': 'GP', '330': 'GD', '331': 'GL', '332': 'GT',
'334': 'HN', '336': 'HT', '338': 'US', '339': 'JM', '341': 'KN',
'343': 'LC', '345': 'MX', '347': 'MQ', '348': 'MS', '350': 'NI',
'351': 'PA', '352': 'PA', '353': 'PA', '354': 'PA', '355': 'PA',
'356': 'PA', '357': 'PA', '358': 'PR', '359': 'SV', '361': 'PM',
'362': 'TT', '364': 'TC', '366': 'US', '367': 'US', '368': 'US',
'369': 'US', '370': 'PA', '371': 'PA', '372': 'PA', '373': 'PA',
'374': 'PA', '375': 'VC', '376': 'VC', '377': 'VC', '378': 'VG',
'401': 'AF', '403': 'SA', '405': 'BD', '408': 'BH', '410': 'BT',
'412': 'CN', '413': 'CN', '414': 'CN', '416': 'TW', '417': 'LK',
'419': 'IN', '422': 'IR', '423': 'AZ', '425': 'IQ', '428': 'IL',
'431': 'JP', '432': 'JP', '434': 'TM', '436': 'KZ', '437': 'UZ',
'438': 'JO', '440': 'KR', '441': 'KR', '443': 'PS', '445': 'KP',
'447': 'KW', '450': 'LB', '451': 'KG', '453': 'MO', '455': 'MV',
'457': 'MN', '459': 'NP', '461': 'OM', '463': 'PK', '466': 'QA',
'468': 'SY', '470': 'AE', '471': 'AE', '472': 'TJ', '473': 'YE',
'475': 'YE', '477': 'HK', '478': 'BA', '501': 'AQ', '503': 'AU',
'506': 'MM', '508': 'BN', '510': 'FM', '511': 'PW', '512': 'NZ',
'514': 'KH', '515': 'KH', '516': 'CX', '518': 'CK', '520': 'FJ',
'523': 'CC', '525': 'ID', '529': 'KI', '531': 'LA', '533': 'MY',
'536': 'MP', '538': 'MH', '540': 'NC', '542': 'NU', '544': 'NR',
'546': 'PF', '548': 'PH', '553': 'PG', '555': 'PN', '557': 'SB',
'559': 'AS', '561': 'WS', '563': 'SG', '564': 'SG', '565': 'SG',
'566': 'SG', '567': 'TH', '570': 'TO', '572': 'TV', '574': 'VN',
'576': 'VU', '577': 'VU', '578': 'WF', '601': 'ZA', '603': 'AO',
'605': 'DZ', '607': 'TF', '608': 'AS', '609': 'BI', '610': 'BJ',
'611': 'BW', '612': 'CF', '613': 'CM', '615': 'CG', '616': 'KM',
'617': 'CV', '618': 'AQ', '619': 'CI', '620': 'KM', '621': 'DJ',
'622': 'EG', '624': 'ET', '625': 'ER', '626': 'GA', '627': 'GH',
'629': 'GM', '630': 'GW', '631': 'GQ', '632': 'GN', '633': 'BF',
'634': 'KE', '635': 'AQ', '636': 'LR', '637': 'LR', '638': 'SS',
'642': 'LY', '644': 'LS', '645': 'MU', '647': 'MG', '649': 'ML',
'650': 'MZ', '654': 'MR', '655': 'MW', '656': 'NE', '657': 'NG',
'659': 'NA', '660': 'RE', '661': 'RW', '662': 'SD', '663': 'SN',
'664': 'SC', '665': 'SH', '666': 'SO', '667': 'SL', '668': 'ST',
'669': 'SZ', '670': 'TD', '671': 'TG', '672': 'TN', '674': 'TZ',
'675': 'UG', '676': 'CD', '677': 'TZ', '678': 'ZM', '679': 'ZW'
}
country = country_map.get(mid, "--")
# Update or create vessel data with static info
with _ais_cache_lock:
if mmsi in _ais_vessels_cache:
_ais_vessels_cache[mmsi]["type"] = ship_type
_ais_vessels_cache[mmsi]["country"] = country
_ais_vessels_cache[mmsi]["category"] = ship_type
else:
# Create minimal entry until we get position report
_ais_vessels_cache[mmsi] = {
"mmsi": mmsi,
"name": meta.get("ShipName", "UNKNOWN").strip(),
"type": ship_type,
"country": country,
"lat": 0,
"lon": 0,
"heading": 0,
"speed": 0,
"imo": meta.get("IMO", "---"),
"status": "Unknown",
"draft": static.get("Draught", 0) / 10, # AIS reports in decimeters
"arrival": static.get("Destination", "Unknown"),
"callsign": static.get("CallSign", "---"),
"source": "AISstream_LIVE",
"atd": "---",
"departure": "---",
"category": ship_type
}
except Exception as e:
print(f"AIS Parse Error: {e}")
continue
def run_async():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
while True:
try:
loop.run_until_complete(ais_stream())
except Exception as e:
print(f"AIS WebSocket Error: {e}, reconnecting in 5s...")
import time
time.sleep(5)
thread = threading.Thread(target=run_async, daemon=True)
thread.start()
print("AIS WebSocket thread started")
@app.route('/api/geo/vessels')
def get_vessel_data():
"""Fetch REAL live vessel data from AISstream.io"""
global _ais_vessels_cache, _ais_websocket_task
# Start WebSocket if not already started
if _ais_websocket_task is None:
try:
start_ais_websocket()
_ais_websocket_task = True
except Exception as e:
print(f"Failed to start AIS WebSocket: {e}")
# Return cached vessels (optimized for performance)
with _ais_cache_lock if _ais_cache_lock else nullcontext():
all_vessels = list(_ais_vessels_cache.values())
# Filter out vessels with invalid positions
valid_vessels = [v for v in all_vessels if v.get('lat') != 0 and v.get('lon') != 0]
# Prioritize India (419), China (412, 413, 414), Russia (273)
priority_prefixes = ('419', '412', '413', '414', '273')
priority_ships = [v for v in valid_vessels if v.get('mmsi', '').startswith(priority_prefixes)]
other_ships = [v for v in valid_vessels if not v.get('mmsi', '').startswith(priority_prefixes)]
# Combine: Priority ships first, then others, limit to 1500 total for better coverage
vessels = (priority_ships + other_ships)[:1500]
return jsonify(vessels)
from contextlib import nullcontext
@app.route('/api/geo/vessel/path/<mmsi>')
def get_vessel_path(mmsi):
"""Generate a realistic historical path for a vessel."""
import random
# Mock more historical points for a longer path
res = []
# Start with a random seed based on MMSI
random.seed(mmsi)
lat = random.uniform(-60, 70)
lon = random.uniform(-180, 180)
for _ in range(25):
lat += random.uniform(-1.0, 1.0)
lon += random.uniform(-1.0, 1.0)
res.append([lat, lon])
return jsonify(res)
from ultralytics import YOLO
# Load YOLO model globally
yolo_seg_model = None
@app.route('/api/geo/segment')
def geo_segment():
"""
Advanced YOLO-based Aerial Segmentation.
Uses Ultralytics YOLOv8-seg to identify and mask structures from satellite tiles.
Supports both point-based (lat/lon) and region-based (bbox) analysis.
"""
global yolo_seg_model
try:
lat = float(request.args.get('lat', 0))
lon = float(request.args.get('lon', 0))
zoom = int(request.args.get('zoom', 18))
bbox_str = request.args.get('bbox', None) # Format: "minLat,minLon,maxLat,maxLon"
# Parse bbox if provided for region filtering
bbox_filter = None
if bbox_str:
try:
bbox_parts = [float(x.strip()) for x in bbox_str.split(',')]
if len(bbox_parts) == 4:
bbox_filter = {
'minLat': bbox_parts[0],
'minLon': bbox_parts[1],
'maxLat': bbox_parts[2],
'maxLon': bbox_parts[3]
}
except:
pass
# 1. Fetch Satellite Tile
n = 2.0 ** zoom
xtile = int((lon + 180.0) / 360.0 * n)
ytile = int((1.0 - math.asinh(math.tan(math.radians(lat))) / math.pi) / 2.0 * n)
tile_url = f"https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{zoom}/{ytile}/{xtile}"
resp = requests.get(tile_url, headers={'User-Agent': 'HayOS/1.0'}, timeout=10)
if resp.status_code != 200:
return jsonify({'error': 'Imagery Offline'}), 502
image_array = np.asarray(bytearray(resp.content), dtype=np.uint8)
img = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
if img is None:
return jsonify({'error': 'Buffer Decode Error'}), 500
# 2. YOLO Segmentation
if yolo_seg_model is None:
# Use nano segmentation model for performance
yolo_seg_model = YOLO('yolov8n-seg.pt')
results = yolo_seg_model(img, conf=0.25)[0]
features = []
# Tile Math for coordinate conversion
lon_deg = (xtile / n * 360.0) - 180.0
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
lat_deg = math.degrees(lat_rad)
lat_rad_next = math.atan(math.sinh(math.pi * (1 - 2 * (ytile + 1) / n)))
lat_deg_next = math.degrees(lat_rad_next)
total_lat_diff = lat_deg - lat_deg_next
# YOLO class mapping to HayOS labels
if results.masks is not None:
for i, (mask, box) in enumerate(zip(results.masks.xy, results.boxes)):
class_id = int(box.cls[0])
confidence = float(box.conf[0])
# Filter small noise
if len(mask) < 3: continue
geo_points = []
for pt in mask:
px, py = pt
# Satellite tiles are typically 256x256
h, w = img.shape[:2]
p_lon = lon_deg + (px / float(w)) * (360.0 / n)
p_lat = lat_deg - (py / float(h)) * total_lat_diff
geo_points.append([p_lon, p_lat])
# Close the polygon
geo_points.append(geo_points[0])
# Heuristic Labeling based on YOLO classes
label = "STRUCTURE_UNIT"
sub_type = "GENERAL_SECTOR"
if class_id in [2, 3, 5, 7]: # car, motorcycle, bus, truck
label = "TRANS_CLASS"
sub_type = "VEHICLE_LOGISTICS"
elif class_id == 0: # person
label = "BIO_DETECT"
sub_type = "HUMAN_PRESENCE"
elif class_id in [56, 57, 58, 59, 60, 61]: # chair, couch, potted plant, bed, dining table, toilet
label = "INFRA_CLASS"
sub_type = "INTERIOR_ELEMENT"
# Estimate area in pixels (approximate)
area = cv2.contourArea(np.array(mask).astype(np.float32))
# Calculate Geographic Bounding Box
# box.xyxy[0] contains [x1, y1, x2, y2] in pixels
x1, y1, x2, y2 = box.xyxy[0]
h_img, w_img = img.shape[:2]
bbox_geo = [
[lon_deg + (x1 / float(w_img)) * (360.0 / n), lat_deg - (y1 / float(h_img)) * total_lat_diff], # Top-Left
[lon_deg + (x2 / float(w_img)) * (360.0 / n), lat_deg - (y2 / float(h_img)) * total_lat_diff] # Bottom-Right
]
# Filter by bbox if provided
if bbox_filter:
center_lat = (bbox_geo[0][1] + bbox_geo[1][1]) / 2
center_lon = (bbox_geo[0][0] + bbox_geo[1][0]) / 2
if not (bbox_filter['minLat'] <= center_lat <= bbox_filter['maxLat'] and
bbox_filter['minLon'] <= center_lon <= bbox_filter['maxLon']):
continue # Skip this detection if outside bbox
features.append({
"type": "Feature",
"geometry": {"type": "Polygon", "coordinates": [geo_points]},
"properties": {
"id": f"YOLO-{class_id}-{i}",
"classification": label,
"type": sub_type,
"area": f"{int(area)} px",
"status": "VALIDATED",
"confidence": f"{int(confidence * 100)}%",
"bbox": bbox_geo
}
})
return jsonify({
"type": "FeatureCollection",
"features": features,
"metadata": {
"engine": "YOLO_V8_SEG",
"objects": len(features),
"sector": f"{xtile}/{ytile}"
}
})
except Exception as e:
app.logger.error(f"SEG_CRITICAL: {e}")
return jsonify({'error': str(e)}), 500
def get_exif_gps(img_pil):
"""Extract GPS coordinates from PIL Image EXIF data."""
try:
exif = img_pil._getexif()
if not exif:
return None
gps_info = {}
for tag, value in exif.items():
decoded = ExifTags.TAGS.get(tag, tag)
if decoded == "GPSInfo":
for t in value:
sub_decoded = ExifTags.GPSTAGS.get(t, t)
gps_info[sub_decoded] = value[t]
if not gps_info:
return None
def convert_to_degrees(value):
d = float(value[0])
m = float(value[1])
s = float(value[2])
return d + (m / 60.0) + (s / 3600.0)
lat = convert_to_degrees(gps_info["GPSLatitude"])
if gps_info["GPSLatitudeRef"] != "N":
lat = -lat
lon = convert_to_degrees(gps_info["GPSLongitude"])
if gps_info["GPSLongitudeRef"] != "E":
lon = -lon
return {"lat": lat, "lon": lon}
except Exception as e:
app.logger.error(f"GPS_EXIF_ERR: {e}")
return None
@app.route('/api/geo/analyze-upload', methods=['POST'])
@app.route('/upload', methods=['POST'])
def analyze_upload():
"""Analyze uploaded image for objects and GPS metadata."""
global yolo_seg_model
# Check both 'image' and 'img' (user's provided form name)
field_name = 'image' if 'image' in request.files else 'img'
if field_name not in request.files:
return jsonify({"error": "No image uploaded"}), 400
file = request.files[field_name]
if file.filename == '':
return jsonify({"error": "Empty filename"}), 400
try:
# 1. Load Image
in_memory_file = io.BytesIO()
file.save(in_memory_file)
data = np.frombuffer(in_memory_file.getvalue(), dtype=np.uint8)
img = cv2.imdecode(data, cv2.IMREAD_COLOR)
if img is None:
return jsonify({"error": "Invalid image format"}), 400
# 2. Extract Location
img_pil = Image.open(io.BytesIO(in_memory_file.getvalue()))
location = get_exif_gps(img_pil)
# 3. Run YOLO Discovery
if yolo_seg_model is None:
yolo_seg_model = YOLO('yolov8n-seg.pt')
results = yolo_seg_model(img, conf=0.25)[0]
detections = []
if results.boxes is not None:
for box in results.boxes:
class_id = int(box.cls[0])
confidence = float(box.conf[0])
label = results.names[class_id].upper()
detections.append({
"object": label,
"confidence": f"{int(confidence * 100)}%",
"tag": f"DISCOVERY_{class_id}"
})
# 4. Return Data
return jsonify({
"status": "ANALYSIS_COMPLETE",
"location": location or {"lat": "UNKNOWN", "lon": "UNKNOWN"},
"objects": detections,
"count": len(detections),
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
})
except Exception as e:
app.logger.error(f"UPLOAD_ANALYSIS_CRITICAL: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/api/geo/flight/meta/<callsign>')
def get_flight_meta(callsign):
# Mock implementation for flight metadata
return jsonify({
"callsign": callsign,
"image": f"https://cdn.jetphotos.com/full/5/{random.randint(10000, 99999)}.jpg",
"operator": "Unknown Operator",
"age": f"{random.randint(1, 25)} years"
})
@app.route('/api/geo/news')
def get_geo_news():
"""
Fetch geopolitical news and tweets for a specific location.
ATTEMPTS REAL API CALLS FIRST, FALLS BACK TO MOCK DATA.
"""
lat = request.args.get('lat', type=float)
lon = request.args.get('lon', type=float)
if lat is None or lon is None:
return jsonify({"error": "Missing coordinates"}), 400
# --- Check Cache ---
cache_key = f"geo_{lat}_{lon}"
now_ts = datetime.now(timezone.utc).timestamp()
if cache_key in news_cache:
cached_time, cached_data = news_cache[cache_key]
if (now_ts - cached_time) < (NEWS_CACHE_LIMIT * 60):
print(f"Serving cached geo news for: {cache_key}")
return jsonify(cached_data)
real_tweets = []
real_news = []
# --- 1. Try Real Twitter API v2 (Search) ---
# Using Bearer Token is easiest for Search v2
if TWITTER_BEARER_TOKEN and TWITTER_BEARER_TOKEN != 'YOUR_BEARER_TOKEN_HERE':
try:
headers = {"Authorization": f"Bearer {TWITTER_BEARER_TOKEN}"}
# Twitter API v2 Recent Search - NOTE: This requires ELEVATED access (not free tier)
# Free tier (Essential) does NOT have access to search endpoints
# We'll try anyway in case user has elevated access
params = {
'query': '(breaking OR news OR alert) -is:retweet lang:en',
'max_results': 2,
'tweet.fields': 'created_at,author_id,text'
}
response = requests.get('https://api.twitter.com/2/tweets/search/recent', headers=headers, params=params, timeout=5)
print(f"Twitter API Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
if 'data' in data:
for t in data['data']:
# Parse timestamp
created = t.get('created_at', '')
try:
dt = datetime.strptime(created, '%Y-%m-%dT%H:%M:%S.%fZ')
time_str = dt.strftime('%H:%M:%S')
except:
time_str = 'Recent'
real_tweets.append({
"user": f"@User_{t.get('author_id', 'Unknown')[-4:]}",
"text": t.get('text', ''),
"timestamp": time_str
})
print(f"Twitter: Fetched {len(real_tweets)} tweets")
else:
print(f"Twitter API response has no 'data': {data}")
else:
print(f"Twitter API Error: {response.status_code} - {response.text[:200]}")
except Exception as e:
print(f"Twitter API Exception: {e}")
# --- 2. Try Real NewsAPI with Location Context ---
if NEWS_API_KEY and NEWS_API_KEY != 'mock_news_key':
try:
# Try to get location name via reverse geocoding
location_query = ""
try:
geo_url = f"https://nominatim.openstreetmap.org/reverse?lat={lat}&lon={lon}&format=json"
geo_res = requests.get(geo_url, timeout=2, headers={'User-Agent': 'HayOS/1.0'})
if geo_res.status_code == 200:
geo_data = geo_res.json()
# Try to extract country or city
address = geo_data.get('address', {})
location_query = address.get('country', '') or address.get('city', '') or address.get('state', '')
print(f"Reverse geocode: {location_query}")
except Exception as geo_err:
print(f"Geocoding error: {geo_err}")
# --- Regional RSS Detection ---
detected_region = ""
country_mapping = {
"United States": "USA", "India": "INDIA", "China": "CHINA",
"Russia": "RUSSIA", "Japan": "JAPAN", "Australia": "AUSTRALIA",
"Taiwan": "TAIWAN", "South Korea": "SOUTH_KOREA", "Israel": "ISRAEL",
"United Arab Emirates": "UAE", "Iran": "IRAN"
}
for c_name, reg_key in country_mapping.items():
if location_query and c_name in location_query:
detected_region = reg_key
break
if not detected_region and location_query:
# Broad region checks
if any(x in location_query for x in ["Europe", "France", "Germany", "Spain", "Italy", "UK", "London"]):
detected_region = "EUROPE"
elif any(x in location_query for x in ["Africa", "Kenya", "Nigeria", "Egypt", "South Africa"]):
detected_region = "AFRICA"
if detected_region:
print(f"Uplinking regional RSS: {detected_region}")
rss_geo = fetch_rss_news(detected_region)
real_news.extend(rss_geo[:5]) # Mix in some RSS
# Build NewsAPI query
if location_query:
# Search for news about this location (all languages)
news_url = f"https://newsapi.org/v2/everything?q={location_query}&sortBy=publishedAt&pageSize=10&apiKey={NEWS_API_KEY}"
else:
# Fallback to top headlines (all languages)
news_url = f"https://newsapi.org/v2/top-headlines?pageSize=10&apiKey={NEWS_API_KEY}"
n_res = requests.get(news_url, timeout=5)
print(f"NewsAPI Status: {n_res.status_code}")
if n_res.status_code == 200:
n_data = n_res.json()
for article in n_data.get('articles', [])[:50]:
# Parse timestamp
pub_time = article.get('publishedAt', '')
try:
dt = datetime.strptime(pub_time, '%Y-%m-%dT%H:%M:%SZ')
time_str = dt.strftime('%H:%M %b %d')
except:
time_str = 'Recent'
real_news.append({
"source": article.get('source', {}).get('name', 'NewsAPI'),
"title": article.get('title', ''),