This commit is contained in:
2026-01-04 02:23:50 -08:00
parent 3acf21a673
commit 8545367e28
6 changed files with 2925 additions and 94 deletions

View File

@@ -12,6 +12,7 @@ import sys
import time
from typing import Any, Dict, Optional, Set, List, Sequence, Tuple
import time
from urllib.parse import urlparse
from SYS.logger import log, debug
@@ -245,6 +246,73 @@ class AllDebridClient:
except Exception as exc:
raise AllDebridError(f"Failed to unlock link: {exc}")
def _link_delayed(self, delayed_id: int) -> Dict[str, Any]:
"""Poll delayed link status."""
try:
resp = self._request("link/delayed", {"id": int(delayed_id)})
if resp.get("status") != "success":
raise AllDebridError("link/delayed returned error status")
data = resp.get("data") or {}
return data if isinstance(data, dict) else {}
except AllDebridError:
raise
except Exception as exc:
raise AllDebridError(f"Failed to poll delayed link: {exc}")
def resolve_unlock_link(
self,
link: str,
*,
poll: bool = True,
max_wait_seconds: int = 30,
poll_interval_seconds: int = 5,
) -> Optional[str]:
"""Unlock a link and handle delayed links by polling link/delayed."""
try:
resp = self._request("link/unlock", {"link": link})
except AllDebridError:
raise
except Exception as exc:
raise AllDebridError(f"Failed to unlock link: {exc}")
if resp.get("status") != "success":
return None
data = resp.get("data") or {}
if not isinstance(data, dict):
return None
# Immediate link ready
for key in ("link", "file"):
val = data.get(key)
if isinstance(val, str) and val.strip():
return val.strip()
delayed_id = data.get("delayed")
if not poll or delayed_id is None:
return None
try:
delayed_int = int(delayed_id)
except Exception:
return None
deadline = time.time() + max_wait_seconds
while time.time() < deadline:
time.sleep(max(1, poll_interval_seconds))
status_data = self._link_delayed(delayed_int)
status = status_data.get("status")
if status == 2:
link_val = status_data.get("link")
if isinstance(link_val, str) and link_val.strip():
return link_val.strip()
return None
if status == 3:
raise AllDebridError("Delayed link generation failed")
return None
def check_host(self, hostname: str) -> Dict[str, Any]:
"""Check if a host is supported by AllDebrid.