maubot-media/media_bot/clients/arr.py
Maddox 7848d8e6ea v0.4.2: SQLite, subscriptions, Sonarr webhook, daily digest
- Enable maubot's bundled SQLite (database: true, webapp: true)
- Schema: subscriptions(mxid, sonarr_series_id, title, added_at) +
  digest_state for once-daily idempotency
- Commands: !media subscribe / unsubscribe / subscriptions / digest
- @web.post(/sonarr-webhook): on Download events, mention subscribers
  in notifications_room (Bearer auth via sonarr_webhook_secret)
- Daily digest loop: fires at digest_hour (Indianapolis), summarises
  Emby recently-added, NZBGet+qBt 24h completions, queue depth.
  Approximate EST/EDT calc since maubot container ships without tzdata.
2026-04-28 19:13:19 -04:00

68 lines
2.4 KiB
Python

"""Sonarr / Radarr v3 HTTP clients (shared base)."""
from __future__ import annotations
import aiohttp
class ArrError(RuntimeError):
pass
class ArrClient:
def __init__(self, session: aiohttp.ClientSession, base_url: str, api_key: str) -> None:
self.session = session
self.base = base_url.rstrip("/")
self.headers = {"X-Api-Key": api_key, "Accept": "application/json"}
async def _get(self, path: str, params: dict | None = None) -> dict | list:
async with self.session.get(f"{self.base}{path}", headers=self.headers, params=params) as r:
if r.status >= 400:
raise ArrError(f"GET {path}{r.status}: {(await r.text())[:200]}")
return await r.json()
async def health(self) -> list[dict]:
"""Returns active health-check warnings (empty list = healthy)."""
data = await self._get("/api/v3/health")
return data if isinstance(data, list) else (data or [])
async def queue(self, page_size: int = 50) -> list[dict]:
params = {
"pageSize": page_size,
"includeUnknownSeriesItems": "false",
"includeUnknownMovieItems": "false",
"includeSeries": "true",
"includeMovie": "true",
"includeEpisode": "true",
}
data = await self._get("/api/v3/queue", params=params)
return (data or {}).get("records", []) if isinstance(data, dict) else (data or [])
class SonarrClient(ArrClient):
async def list_series(self) -> list[dict]:
"""Full list of series Sonarr knows about (used for subscription lookup)."""
data = await self._get("/api/v3/series")
return data if isinstance(data, list) else (data or [])
async def calendar(self, start: str, end: str) -> list[dict]:
data = await self._get(
"/api/v3/calendar",
params={"start": start, "end": end, "includeSeries": "true"},
)
return data if isinstance(data, list) else (data or [])
async def missing(self, page_size: int = 10) -> list[dict]:
params = {
"pageSize": page_size,
"sortKey": "airDateUtc",
"sortDirection": "descending",
"monitored": "true",
"includeSeries": "true",
}
data = await self._get("/api/v3/wanted/missing", params=params)
return (data or {}).get("records", []) if isinstance(data, dict) else (data or [])
class RadarrClient(ArrClient):
pass