mirror of
https://github.com/myronblair/kino-app
synced 2026-07-28 13:33:49 -05:00
auto-commit for b082c1c8-f6e3-4cfd-82ee-0396c70d1a00
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
"""Sonarr v3 API client. https://sonarr.tv/docs/api/"""
|
||||
import requests
|
||||
from typing import List, Dict
|
||||
|
||||
|
||||
def _h(api_key: str) -> dict:
|
||||
return {"X-Api-Key": api_key, "Content-Type": "application/json"}
|
||||
|
||||
|
||||
def _poster(images):
|
||||
for img in images or []:
|
||||
if img.get("coverType") == "poster":
|
||||
return img.get("remoteUrl") or img.get("url") or ""
|
||||
return ""
|
||||
|
||||
|
||||
def test_connection(base_url: str, api_key: str) -> bool:
|
||||
try:
|
||||
r = requests.get(f"{base_url.rstrip('/')}/api/v3/system/status", headers=_h(api_key), timeout=10)
|
||||
return r.ok
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def list_series(base_url: str, api_key: str) -> List[Dict]:
|
||||
r = requests.get(f"{base_url.rstrip('/')}/api/v3/series", headers=_h(api_key), timeout=30)
|
||||
r.raise_for_status()
|
||||
out = []
|
||||
for s in r.json() or []:
|
||||
stats = s.get("statistics") or {}
|
||||
out.append({
|
||||
"sonarr_id": s["id"],
|
||||
"title": s.get("title", ""),
|
||||
"year": s.get("year"),
|
||||
"overview": s.get("overview") or "",
|
||||
"poster_url": _poster(s.get("images")),
|
||||
"tvdb_id": s.get("tvdbId"),
|
||||
"tmdb_id": s.get("tmdbId"),
|
||||
"season_count": stats.get("seasonCount", 0),
|
||||
"episode_file_count": stats.get("episodeFileCount", 0),
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
def list_episodes(base_url: str, api_key: str, series_id: int) -> List[Dict]:
|
||||
r = requests.get(
|
||||
f"{base_url.rstrip('/')}/api/v3/episode",
|
||||
params={"seriesId": series_id, "includeEpisodeFile": True},
|
||||
headers=_h(api_key), timeout=30,
|
||||
)
|
||||
r.raise_for_status()
|
||||
out = []
|
||||
for e in r.json() or []:
|
||||
ef = e.get("episodeFile") or {}
|
||||
out.append({
|
||||
"sonarr_episode_id": e["id"],
|
||||
"season_number": e.get("seasonNumber", 0),
|
||||
"episode_number": e.get("episodeNumber", 0),
|
||||
"title": e.get("title", ""),
|
||||
"description": e.get("overview") or "",
|
||||
"air_date": (e.get("airDate") or ""),
|
||||
"duration_minutes": int((ef.get("runTime") or 0) / 60) if ef.get("runTime") else 0,
|
||||
"has_file": bool(e.get("hasFile")),
|
||||
"file_path": ef.get("path") if ef else None,
|
||||
})
|
||||
return out
|
||||
Reference in New Issue
Block a user