from pathlib import PurePath import httpx class RadarrClient: def __init__(self, url: str, api_key: str) -> None: self._url = url.rstrip("/") self._headers = {"X-Api-Key": api_key} async def _get(self, endpoint: str) -> object: async with httpx.AsyncClient(timeout=30) as client: response = await client.get(f"{self._url}/api/v3/{endpoint}", headers=self._headers) response.raise_for_status() return response.json() async def system_status(self) -> dict: result = await self._get("system/status") if not isinstance(result, dict): raise ValueError("Réponse Radarr inattendue") return result async def movies(self) -> list[dict]: # Radarr does not always embed movieFile by default depending on client version. # The documented way is to request it explicitly. result = await self._get("movie?includeMovieFile=true") if not isinstance(result, list): raise ValueError("Réponse Radarr inattendue") return result async def movie(self, movie_id: int) -> dict: result = await self._get(f"movie/{movie_id}?includeMovieFile=true") if not isinstance(result, dict): raise ValueError("Réponse Radarr inattendue") return result async def naming(self) -> dict: """Configuration Media Management: formats de renommage.""" result = await self._get("config/naming") if not isinstance(result, dict): raise ValueError("Réponse Radarr inattendue") return result def movie_file_path(movie: dict) -> str | None: movie_file = movie.get("movieFile") or {} path = movie_file.get("path") if path: return str(path) relative_path = movie_file.get("relativePath") movie_path = movie.get("path") if relative_path and movie_path: return str(PurePath(str(movie_path)) / str(relative_path)) return None