Add open IP camera discovery scraper (OSINT map)
Some checks failed
build-and-deploy / build (push) Failing after 5s
Some checks failed
build-and-deploy / build (push) Failing after 5s
- camera_scraper: public-directory-only discovery (Insecam-style HTML + plain-text lists), hard private-range guard (fail closed), per-host rate limiting, Nominatim geocoding at <=1 req/s, TTL'd local snapshot cache, sha256 url_hash dedupe with Postgres upsert - cameras table (migration 002) + /api/cameras?bbox= + snapshot endpoint with cache passthrough in main.py - run_camera_service: long-running cycle worker following NATS->ingester pattern; publishes events.camera for shared ingester - docker-compose camera-service profile, .env.example knobs Verified E2E against TimescaleDB+PostGIS: private-range entries dropped, dedupe across cycles holds, bbox query returns expected rows.
This commit is contained in:
parent
bce2d5c3bd
commit
69a30115cc
8 changed files with 730 additions and 0 deletions
71
app/main.py
71
app/main.py
|
|
@ -591,6 +591,77 @@ async def sentiment_by_source(hours: int = 24):
|
|||
} for r in rows]
|
||||
|
||||
|
||||
# ── Cameras (open-camera discovery map) ───────────────────────────────────
|
||||
|
||||
@app.get("/api/cameras")
|
||||
async def list_cameras(
|
||||
bbox: str | None = Query(
|
||||
None,
|
||||
description="Bounding box 'min_lon,min_lat,max_lon,max_lat'",
|
||||
),
|
||||
source: str | None = Query(None, description="Filter by discovery_source"),
|
||||
limit: int = Query(500, ge=1, le=5000),
|
||||
):
|
||||
"""Cameras for map display, optionally filtered by geographic bbox."""
|
||||
from camera_models import cameras as cam_table
|
||||
|
||||
async with async_session() as session:
|
||||
stmt = select(cam_table).order_by(cam_table.c.last_seen.desc())
|
||||
if bbox:
|
||||
try:
|
||||
min_lon, min_lat, max_lon, max_lat = (
|
||||
float(v) for v in bbox.split(",")
|
||||
)
|
||||
except ValueError:
|
||||
raise HTTPException(
|
||||
422, "bbox must be 'min_lon,min_lat,max_lon,max_lat'"
|
||||
)
|
||||
if not (-180 <= min_lon <= 180 and -180 <= max_lon <= 180
|
||||
and -90 <= min_lat <= 90 and -90 <= max_lat <= 90):
|
||||
raise HTTPException(422, "bbox coordinates out of range")
|
||||
stmt = stmt.where(
|
||||
and_(cam_table.c.location_lat >= min_lat,
|
||||
cam_table.c.location_lat <= max_lat,
|
||||
cam_table.c.location_lon >= min_lon,
|
||||
cam_table.c.location_lon <= max_lon))
|
||||
if source:
|
||||
stmt = stmt.where(cam_table.c.discovery_source == source)
|
||||
rows = (await session.execute(stmt.limit(limit))).mappings().all()
|
||||
|
||||
return [{
|
||||
"id": str(r["id"]),
|
||||
"source_url": r["source_url"],
|
||||
"snapshot_url": r["snapshot_url"],
|
||||
"discovery_source": r["discovery_source"],
|
||||
"lat": r["location_lat"],
|
||||
"lon": r["location_lon"],
|
||||
"location_name": r["location_name"],
|
||||
"vendor": r["vendor"],
|
||||
"device_type": r["device_type"],
|
||||
"first_seen": r["first_seen"].isoformat(),
|
||||
"last_seen": r["last_seen"].isoformat(),
|
||||
} for r in rows]
|
||||
|
||||
|
||||
@app.get("/api/cameras/{camera_id}/snapshot")
|
||||
async def camera_snapshot(camera_id: UUID):
|
||||
"""Snapshot image for one camera, served through the local TTL cache."""
|
||||
from camera_models import cameras as cam_table
|
||||
from camera_scraper import fetch_snapshot
|
||||
|
||||
async with async_session() as session:
|
||||
row = (await session.execute(
|
||||
select(cam_table).where(cam_table.c.id == camera_id)
|
||||
)).mappings().one_or_none()
|
||||
if not row or not row["snapshot_url"]:
|
||||
raise HTTPException(404, "Camera or snapshot not found")
|
||||
data = await fetch_snapshot(row["snapshot_url"])
|
||||
if not data:
|
||||
raise HTTPException(502, "Snapshot unavailable")
|
||||
from fastapi.responses import Response
|
||||
return Response(content=data, media_type="image/jpeg")
|
||||
|
||||
|
||||
# ── Frontend ──────────────────────────────────────────────────────────────
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
|
|
|
|||
Loading…
Reference in a new issue