maubot-media/media_bot/clients/arr.py
Maddox de153892cc Initial commit: media bot v0.1.0
Maubot plugin: Matrix companion for the homelab media stack.

Services wrapped:
- Seerr: search, request, requests, trending
- Emby: nowplaying, recent, watched
- Sonarr/Radarr: queue, upcoming, missing
- NZBGet/qBittorrent: activity

Each Matrix sender is mapped to per-service user IDs via plugin config;
unmapped senders are rejected. Replies are MXID-prefixed for shared rooms.
2026-04-28 08:22:38 -04:00

58 lines
2 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 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 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