osint-dashboard/app/run_camera_service.py

69 lines
2.2 KiB
Python
Raw Permalink Normal View History

"""Long-running camera discovery service.
Discovers cameras on an interval and upserts them into the `cameras` table
(dedupe by url_hash) for the /api/cameras bbox query. Discovery is not an
event: this process does not publish `events.camera` messages. Those were
inserted as events rows and flooded the HUD feed. The cameras table is the
index.
Env:
CAMERA_SOURCE_URLS comma-separated public directory/list URLs
CAMERA_SCRAPE_INTERVAL seconds between cycles (default 3600)
CAMERA_ENABLED "0" to disable (default "1")
"""
from __future__ import annotations
import asyncio
import logging
import sys
from pathlib import Path
sys_path = str(Path(__file__).parent)
sys.path.insert(0, sys_path)
from camera_config import ( # noqa: E402
CAMERA_SOURCE_URLS, CAMERA_SCRAPE_INTERVAL,
SNAPSHOT_CACHE_DIR,
)
from camera_scraper import run_cycle # noqa: E402
from database import init_extensions # noqa: E402
logging.basicConfig(level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s")
logger = logging.getLogger("osint.camera_service")
ENABLED = sys.argv[1:] != ["--once"]
async def publish_new_cameras(rows: list | None = None) -> int:
"""Do not publish camera discoveries as events.
``run_cycle`` already upserts the cameras table, which backs
``GET /api/cameras``. ``rows`` is newly seen cameras from a cycle or a
test; none are JetStream-published, so the ingester inserts 0 events
with source_type=camera.
"""
del rows
return 0
async def main() -> None:
logger.info("camera discovery starting (%d sources, interval=%ss)",
len(CAMERA_SOURCE_URLS), CAMERA_SCRAPE_INTERVAL)
Path(SNAPSHOT_CACHE_DIR).mkdir(parents=True, exist_ok=True)
await init_extensions()
while True:
try:
n = await run_cycle()
p = await publish_new_cameras()
logger.info("cycle: %d stored, %d camera events published", n, p)
except Exception: # noqa: BLE001
logger.exception("camera cycle error")
if not ENABLED: # --once mode
return
await asyncio.sleep(CAMERA_SCRAPE_INTERVAL)
if __name__ == "__main__":
asyncio.run(main())