feat(aircraft): planespotters.net photo in aircraft popup

Add planespotters.net latest-photo lookup for ADS-B aircraft.

- app/live_layers.py: fetch_planespotters_photo() (hex preferred, reg
  fallback) + _normalize_planespotter_photo(); 24h TTL cache (their ToS cap).
- app/main.py: GET /api/aircraft/photo?hex=...|reg=... (422/404/502).
- app/static/index.html: .ps-photo block in ADS-B popup; lazy load on
  popupopen; thumbnail links to photo page + photographer credit.

Server-side proxy, not browser fetch(): planespotters 403s any request
carrying an Origin header (every browser fetch() sends one). The thumbnail
binary is loaded by the browser straight from their CDN, never re-hosted.

Tests: 4 unit + 4 API contract (38 pass in the two files).
This commit is contained in:
Sirius DevOps 2026-08-28 22:58:04 -04:00
commit 665aaec22f
5 changed files with 228 additions and 4 deletions

View file

@ -18,6 +18,7 @@ from contextlib import asynccontextmanager
from datetime import datetime, timedelta, timezone
from decimal import Decimal
from pathlib import Path
from typing import NoReturn
from uuid import UUID
import structlog
@ -52,8 +53,8 @@ from keystore import KeyFormatError, delete_key, list_keys, set_key
from settings_store import SettingsError, get_app_settings, list_models, set_summary_model
from live_layers import (
fetch_aircraft, fetch_fire_incidents, fetch_fire_perimeters,
fetch_radar_meta, fetch_storms, fetch_trains, fetch_vessels,
fetch_weather_alerts, overlay_catalog, parse_bbox,
fetch_planespotters_photo, fetch_radar_meta, fetch_storms, fetch_trains,
fetch_vessels, fetch_weather_alerts, overlay_catalog, parse_bbox,
)
logging.basicConfig(level=logging.INFO)
@ -1390,7 +1391,7 @@ async def map_layers():
return {"layers": MAP_LAYERS, "overlays": overlay_catalog()}
def _upstream_or_502(exc: Exception, name: str):
def _upstream_or_502(exc: Exception, name: str) -> NoReturn:
logger.warning("live_layer_upstream_failed", layer=name, error=str(exc))
raise HTTPException(502, f"{name} upstream unavailable: {exc}") from exc
@ -1424,6 +1425,23 @@ async def list_aircraft(
_upstream_or_502(exc, "aircraft")
@app.get("/api/aircraft/photo")
async def aircraft_photo(
hex_code: str | None = Query(None, alias="hex", pattern="^[0-9a-fA-F]{6}$"),
reg: str | None = Query(None, min_length=1, max_length=12),
):
"""Latest planespotters.net photo for an aircraft (hex preferred, reg fallback)."""
if not hex_code and not reg:
raise HTTPException(422, "hex or reg required")
try:
photo = await fetch_planespotters_photo(hex_code=hex_code, reg=reg)
except Exception as exc:
_upstream_or_502(exc, "planespotters")
if photo is None:
raise HTTPException(404, "no photo")
return overlay_json(photo, 86400)
@app.get("/api/trains")
async def list_trains(
bbox: str | None = Query(None, description="minlon,minlat,maxlon,maxlat"),