From 6c87887c99b890fa17dfa6830239b36c34ff67c3 Mon Sep 17 00:00:00 2001 From: juanjo <130799031+juanminguezsanz2023@users.noreply.github.com> Date: Tue, 4 Aug 2026 00:03:43 +0200 Subject: [PATCH] 03-ago: [videoclub] unrestrict de enlaces RealDebrid en descarga al NAS (pi_bot) --- vigilancia/.env.example | 3 +++ vigilancia/nas_dl.py | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/vigilancia/.env.example b/vigilancia/.env.example index 0a89319..9095fa0 100644 --- a/vigilancia/.env.example +++ b/vigilancia/.env.example @@ -78,3 +78,6 @@ NAS_PORT=5000 NAS_USER= NAS_PASS= NAS_HTTPS=0 + +# Token RealDebrid (opcional): desrestrict de enlaces /d/ antes de anadir al NAS +TORRENTIO_RD_TOKEN= diff --git a/vigilancia/nas_dl.py b/vigilancia/nas_dl.py index a2abe8b..595b7fc 100644 --- a/vigilancia/nas_dl.py +++ b/vigilancia/nas_dl.py @@ -33,7 +33,8 @@ def _load_env(path): ENV = _load_env(os.path.join(BASE, ".env")) # Respaldo: si no hay credenciales NAS aqui, probar el .env de videoclub/ _VIDEOCLUB_ENV = _load_env(os.path.join(BASE, "..", "videoclub", ".env")) -for _k in ("NAS_HOST", "NAS_PORT", "NAS_USER", "NAS_PASS", "NAS_HTTPS"): +for _k in ("NAS_HOST", "NAS_PORT", "NAS_USER", "NAS_PASS", "NAS_HTTPS", + "TORRENTIO_RD_TOKEN"): if not ENV.get(_k): ENV[_k] = _VIDEOCLUB_ENV.get(_k, "") NAS_HOST = os.getenv("NAS_HOST", ENV.get("NAS_HOST", "192.168.50.31")).strip().rstrip("/") @@ -41,10 +42,36 @@ NAS_PORT = os.getenv("NAS_PORT", ENV.get("NAS_PORT", "5000")).strip() NAS_USER = os.getenv("NAS_USER", ENV.get("NAS_USER", "")).strip() NAS_PASS = os.getenv("NAS_PASS", ENV.get("NAS_PASS", "")).strip() NAS_HTTPS = os.getenv("NAS_HTTPS", ENV.get("NAS_HTTPS", "0")).strip() == "1" +RD_TOKEN = os.getenv("TORRENTIO_RD_TOKEN", ENV.get("TORRENTIO_RD_TOKEN", "")).strip() BASE_URL = f"{'https' if NAS_HTTPS else 'http'}://{NAS_HOST}:{NAS_PORT}" +def unrestrict_rd(uri): + """Convierte un enlace real-debrid.com/d/XXX en el enlace directo descargable. + Si el enlace no es de RealDebrid, lo devuelve tal cual.""" + if "real-debrid.com/" not in uri: + return uri + if not RD_TOKEN: + print("[RD] Falta TORRENTIO_RD_TOKEN en .env") + return uri + try: + data = urllib.parse.urlencode({"link": uri}).encode() + req = urllib.request.Request( + "https://api.real-debrid.com/rest/1.0/unrestrict/link", + data=data, headers={"Authorization": f"Bearer {RD_TOKEN}"}) + with urllib.request.urlopen(req, timeout=30) as r: + d = json.loads(r.read().decode("utf-8")) + directo = d.get("download") + if directo: + print(f"[RD] Desrestrict OK: {directo[:80]}") + return directo + print(f"[RD] No devolvio enlace directo: {d.get('error', d)}") + except Exception as e: + print(f"[RD] Error desrestrict: {e}") + return uri + + def _api(path, params, session=None): data = urllib.parse.urlencode(params).encode() headers = {"Content-Type": "application/x-www-form-urlencoded"} @@ -80,7 +107,9 @@ def login(): def add_torrent(uri, session=None): - """Anade una URI (http/magnet/.torrent) a Download Station.""" + """Anade una URI (http/magnet/.torrent) a Download Station. + Si es enlace de RealDebrid, primero se desrestricta a enlace directo.""" + uri = unrestrict_rd(uri) if not session: session = login() if not session: