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:
parent
e568d013ca
commit
665aaec22f
5 changed files with 228 additions and 4 deletions
|
|
@ -572,3 +572,73 @@ def test_persist_aircraft_snapshot_writes_tracks(monkeypatch):
|
|||
}]
|
||||
asyncio.run(persist_aircraft_snapshot(rows))
|
||||
assert recorded == [("aircraft", "abc")]
|
||||
|
||||
|
||||
# ── Planespotters.net photo lookup ────────────────────────────────────────
|
||||
|
||||
def test_normalize_planespotter_photo_prefers_large_thumbnail():
|
||||
from live_layers import _normalize_planespotter_photo
|
||||
|
||||
out = _normalize_planespotter_photo({
|
||||
"id": "1053982",
|
||||
"thumbnail": {"src": "https://t.plnspttrs.net/x_t.jpg", "size": {"width": 200, "height": 141}},
|
||||
"thumbnail_large": {"src": "https://t.plnspttrs.net/x_280.jpg", "size": {"width": 395, "height": 280}},
|
||||
"link": "https://www.planespotters.net/photo/1053982/foo",
|
||||
"photographer": "Günther Feniuk",
|
||||
})
|
||||
assert out["id"] == "1053982"
|
||||
assert out["src"] == "https://t.plnspttrs.net/x_280.jpg"
|
||||
assert out["width"] == 395
|
||||
assert out["height"] == 280
|
||||
assert out["photographer"] == "Günther Feniuk"
|
||||
assert "planespotters.net" in out["link"]
|
||||
|
||||
|
||||
def test_normalize_planespotter_photo_empty_or_malformed_returns_none():
|
||||
from live_layers import _normalize_planespotter_photo
|
||||
|
||||
assert _normalize_planespotter_photo({}) is None
|
||||
assert _normalize_planespotter_photo({"thumbnail": {}}) is None
|
||||
assert _normalize_planespotter_photo(None) is None
|
||||
assert _normalize_planespotter_photo("not-a-dict") is None
|
||||
|
||||
|
||||
def test_fetch_planespotters_photo_hex_builds_url_and_normalizes(monkeypatch):
|
||||
import asyncio
|
||||
|
||||
from live_layers import fetch_planespotters_photo, _cache
|
||||
|
||||
seen = []
|
||||
|
||||
async def fake_get(url, params=None):
|
||||
seen.append(url)
|
||||
return {"photos": [{
|
||||
"id": "1", "thumbnail": {"src": "https://t.plnspttrs.net/a_t.jpg"},
|
||||
"thumbnail_large": {"src": "https://t.plnspttrs.net/a_280.jpg"},
|
||||
"link": "https://www.planespotters.net/photo/1/x", "photographer": "A",
|
||||
}]}
|
||||
|
||||
monkeypatch.setattr("live_layers._get_json", fake_get)
|
||||
_cache.clear()
|
||||
out = asyncio.run(fetch_planespotters_photo(hex_code="e8027e"))
|
||||
assert out["src"] == "https://t.plnspttrs.net/a_280.jpg"
|
||||
assert seen == ["https://api.planespotters.net/pub/photos/hex/e8027e"]
|
||||
|
||||
|
||||
def test_fetch_planespotters_photo_reg_fallback_and_no_result(monkeypatch):
|
||||
import asyncio
|
||||
|
||||
from live_layers import fetch_planespotters_photo, _cache
|
||||
|
||||
seen = []
|
||||
|
||||
async def fake_get(url, params=None):
|
||||
seen.append(url)
|
||||
return {"photos": []}
|
||||
|
||||
monkeypatch.setattr("live_layers._get_json", fake_get)
|
||||
_cache.clear()
|
||||
assert asyncio.run(fetch_planespotters_photo(reg="D-ABCD")) is None
|
||||
assert seen == ["https://api.planespotters.net/pub/photos/reg/D-ABCD"]
|
||||
# no hex and no reg → no upstream call at all
|
||||
assert asyncio.run(fetch_planespotters_photo()) is None
|
||||
|
|
|
|||
Loading…
Reference in a new issue