Files
Bram 9f87297d35
Build and Push Docker Images / build-and-push (push) Successful in 18s
poahh
2026-02-11 21:06:16 +01:00

71 lines
2.5 KiB
Python

"""Channel loading and path configuration."""
import json
import re
from pathlib import Path
from .config import CHANNELS_FILE, MOVIES_ROOT, SERIES_ROOT
def resolve_path(path_key: str) -> Path | None:
"""Resolve a channel path key to an absolute Path. Returns None if invalid."""
path_key = path_key.strip("/")
if path_key == "movies":
return MOVIES_ROOT
if path_key.startswith("series/"):
subpath = path_key[7:]
return SERIES_ROOT / subpath
return None
def normalize_path_config(entry) -> dict:
"""
Normalize a path entry from channels.json. Entry can be:
- String: "series/W817" -> {path_key, display_name, tmdb_search, tmdb_id}
- Object: {"path": "series/W817", "display_name": "...", "tmdb_search": "...", "tmdb_id": ...}
"""
if isinstance(entry, str):
return {"path_key": entry.strip(), "display_name": None, "tmdb_search": None, "tmdb_id": None}
if isinstance(entry, dict):
path_key = (entry.get("path") or entry.get("path_key") or "").strip()
if not path_key:
return {"path_key": "", "display_name": None, "tmdb_search": None, "tmdb_id": None}
raw_id = entry.get("tmdb_id")
tmdb_id = None
if raw_id is not None:
if isinstance(raw_id, int) and raw_id > 0:
tmdb_id = raw_id
else:
m = re.match(r"^(\d+)", str(raw_id).strip())
if m:
tmdb_id = int(m.group(1))
return {
"path_key": path_key,
"display_name": (entry.get("display_name") or "").strip() or None,
"tmdb_search": (entry.get("tmdb_search") or "").strip() or None,
"tmdb_id": tmdb_id,
}
return {"path_key": "", "display_name": None, "tmdb_search": None, "tmdb_id": None}
class ChannelRepository:
"""Loads and provides channel configuration from channels.json."""
def __init__(self, channels_file: Path = CHANNELS_FILE):
self._channels_file = channels_file
def get_all(self) -> list[dict]:
"""Load and return all channels."""
if not self._channels_file.exists():
return []
with open(self._channels_file, encoding="utf-8") as f:
data = json.load(f)
return data.get("channels", [])
def get_by_id(self, channel_id: str) -> dict | None:
"""Return the channel with the given id, or None."""
for ch in self.get_all():
if ch.get("id") == channel_id:
return ch
return None