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

@ -19,6 +19,7 @@ from datetime import datetime, timezone
from typing import Any, Awaitable, Callable
import httpx
from urllib.parse import quote
from config import OSINT_USER_AGENT
@ -27,6 +28,9 @@ logger = logging.getLogger("osint.live_layers")
MARKER_FIELDS = ("id", "lat", "lon", "heading", "speed", "label", "extra")
ADSB_LOL_BASE = "https://api.adsb.lol"
PLANESPOTTERS_PHOTO = "https://api.planespotters.net/pub/photos"
# Planespotters ToS cap server-side JSON caching at 24 hours.
PLANESPOTTERS_CACHE_TTL = 24 * 3600
AMTRAKER_TRAINS = "https://api.amtraker.com/v3/trains"
RAINVIEWER_MAPS = "https://api.rainviewer.com/public/weather-maps.json"
NWS_ALERTS = "https://api.weather.gov/alerts/active"
@ -803,6 +807,57 @@ async def _get_json(url: str, params: dict | None = None) -> Any:
return resp.json()
def _normalize_planespotter_photo(photo: dict) -> dict | None:
"""Slim a planespotters.net photo object for the aircraft popup.
Image binaries are never proxied/re-hosted: we return the CDN ``src``
and the browser loads it directly (per ToS).
"""
if not isinstance(photo, dict):
return None
large = photo.get("thumbnail_large") or {}
small = photo.get("thumbnail") or {}
src = large.get("src") or small.get("src")
if not src:
return None
size = large.get("size") or small.get("size") or {}
return {
"id": str(photo.get("id") or ""),
"src": src,
"width": size.get("width"),
"height": size.get("height"),
"link": photo.get("link") or "",
"photographer": photo.get("photographer") or "",
}
async def fetch_planespotters_photo(
hex_code: str | None = None, reg: str | None = None,
) -> dict | None:
"""Latest photo for an aircraft from planespotters.net (hex preferred).
Server-side only: planespotters 403s any request carrying an ``Origin``
header (browser fetch() always sends one), so the browser can never reach
it directly. We fetch here with the identifying UA, cache the JSON ≤24h,
and hand the CDN image URL back for the browser to load.
"""
if hex_code:
key = f"psp-hex:{hex_code.strip().lower()}"
url = f"{PLANESPOTTERS_PHOTO}/hex/{quote(hex_code.strip())}"
elif reg:
key = f"psp-reg:{reg.strip().lower()}"
url = f"{PLANESPOTTERS_PHOTO}/reg/{quote(reg.strip())}"
else:
return None
async def _load() -> dict | None:
data = await _get_json(url)
photos = data.get("photos") or []
return _normalize_planespotter_photo(photos[0]) if photos else None
return await _ttl_get(key, PLANESPOTTERS_CACHE_TTL, _load)
async def fetch_aircraft(
bbox: str, limit: int = DEFAULT_LIMIT, persist: bool = False,
) -> list[dict]: