k
This commit is contained in:
@@ -543,9 +543,72 @@ def adjust_output_dir_for_alldebrid(
|
||||
|
||||
class AllDebrid(Provider):
|
||||
# Magnet URIs should be routed through this provider.
|
||||
TABLE_AUTO_STAGES = {"alldebrid": ["download-file"]}
|
||||
URL = ("magnet:",)
|
||||
URL_DOMAINS = ()
|
||||
|
||||
@staticmethod
|
||||
def _resolve_magnet_spec_from_result(result: Any) -> Optional[str]:
|
||||
table = getattr(result, "table", None)
|
||||
media_kind = getattr(result, "media_kind", None)
|
||||
tags = getattr(result, "tag", None)
|
||||
full_metadata = getattr(result, "full_metadata", None)
|
||||
target = getattr(result, "path", None) or getattr(result, "url", None)
|
||||
|
||||
if not table or str(table).strip().lower() != "alldebrid":
|
||||
return None
|
||||
|
||||
kind_val = str(media_kind or "").strip().lower()
|
||||
is_folder = kind_val == "folder"
|
||||
if not is_folder and isinstance(tags, (list, set)):
|
||||
for tag in tags:
|
||||
if str(tag or "").strip().lower() == "folder":
|
||||
is_folder = True
|
||||
break
|
||||
if not is_folder:
|
||||
return resolve_magnet_spec(str(target or "")) if isinstance(target, str) else None
|
||||
|
||||
metadata = full_metadata if isinstance(full_metadata, dict) else {}
|
||||
candidates: List[str] = []
|
||||
|
||||
def _maybe_add(value: Any) -> None:
|
||||
if isinstance(value, str):
|
||||
cleaned = value.strip()
|
||||
if cleaned:
|
||||
candidates.append(cleaned)
|
||||
|
||||
magnet_block = metadata.get("magnet")
|
||||
if isinstance(magnet_block, dict):
|
||||
for inner in ("magnet", "magnet_link", "link", "url"):
|
||||
_maybe_add(magnet_block.get(inner))
|
||||
for inner in ("hash", "info_hash", "torrenthash", "magnethash"):
|
||||
_maybe_add(magnet_block.get(inner))
|
||||
else:
|
||||
_maybe_add(magnet_block)
|
||||
|
||||
for extra in ("magnet_link", "magnet_url", "magnet_spec"):
|
||||
_maybe_add(metadata.get(extra))
|
||||
_maybe_add(metadata.get("hash"))
|
||||
_maybe_add(metadata.get("info_hash"))
|
||||
|
||||
for candidate in candidates:
|
||||
spec = resolve_magnet_spec(candidate)
|
||||
if spec:
|
||||
return spec
|
||||
return resolve_magnet_spec(str(target)) if isinstance(target, str) else None
|
||||
|
||||
def handle_url(self, url: str, *, output_dir: Optional[Path] = None) -> Tuple[bool, Optional[Path]]:
|
||||
spec = resolve_magnet_spec(url)
|
||||
if not spec:
|
||||
return False, None
|
||||
|
||||
cfg = self.config if isinstance(self.config, dict) else {}
|
||||
try:
|
||||
prepare_magnet(spec, cfg)
|
||||
return True, None
|
||||
except Exception:
|
||||
return False, None
|
||||
|
||||
@classmethod
|
||||
def url_patterns(cls) -> Tuple[str, ...]:
|
||||
# Combine static patterns with cached host domains.
|
||||
@@ -744,11 +807,42 @@ class AllDebrid(Provider):
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def download_items(
|
||||
self,
|
||||
result: SearchResult,
|
||||
output_dir: Path,
|
||||
*,
|
||||
emit: Callable[[Path, str, str, Dict[str, Any]], None],
|
||||
progress: Any,
|
||||
quiet_mode: bool,
|
||||
path_from_result: Callable[[Any], Path],
|
||||
config: Optional[Dict[str, Any]] = None,
|
||||
) -> int:
|
||||
spec = self._resolve_magnet_spec_from_result(result)
|
||||
if not spec:
|
||||
return 0
|
||||
|
||||
cfg = config if isinstance(config, dict) else (self.config or {})
|
||||
|
||||
def _on_emit(path: Path, file_url: str, relpath: str, metadata: Dict[str, Any]) -> None:
|
||||
emit(path, file_url, relpath, metadata)
|
||||
|
||||
downloaded, _ = download_magnet(
|
||||
spec,
|
||||
str(getattr(result, "path", "") or ""),
|
||||
output_dir,
|
||||
cfg,
|
||||
progress,
|
||||
quiet_mode,
|
||||
path_from_result,
|
||||
_on_emit,
|
||||
)
|
||||
return downloaded
|
||||
|
||||
@staticmethod
|
||||
def _flatten_files(items: Any,
|
||||
*,
|
||||
_prefix: Optional[List[str]] = None) -> Iterable[Dict[str,
|
||||
Any]]:
|
||||
_prefix: Optional[List[str]] = None) -> Iterable[Dict[str, Any]]:
|
||||
"""Flatten AllDebrid magnet file tree into file dicts, preserving relative paths.
|
||||
|
||||
API commonly returns:
|
||||
@@ -784,9 +878,7 @@ class AllDebrid(Provider):
|
||||
|
||||
name = node.get("n") or node.get("name")
|
||||
link = node.get("l") or node.get("link")
|
||||
if isinstance(name,
|
||||
str) and name.strip() and isinstance(link,
|
||||
str) and link.strip():
|
||||
if isinstance(name, str) and name.strip() and isinstance(link, str) and link.strip():
|
||||
rel_parts = prefix + [name.strip()]
|
||||
relpath = "/".join([p for p in rel_parts if p])
|
||||
enriched = dict(node)
|
||||
@@ -932,6 +1024,19 @@ class AllDebrid(Provider):
|
||||
except Exception:
|
||||
size_bytes = None
|
||||
|
||||
metadata = {
|
||||
"magnet": magnet_status,
|
||||
"magnet_id": magnet_id,
|
||||
"magnet_name": magnet_name,
|
||||
"relpath": relpath,
|
||||
"file": file_node,
|
||||
"provider": "alldebrid",
|
||||
"provider_view": "files",
|
||||
}
|
||||
if file_url:
|
||||
metadata["_selection_args"] = ["-url", file_url]
|
||||
metadata["_selection_action"] = ["download-file", "-url", file_url]
|
||||
|
||||
results.append(
|
||||
SearchResult(
|
||||
table="alldebrid",
|
||||
@@ -952,15 +1057,7 @@ class AllDebrid(Provider):
|
||||
("ID",
|
||||
str(magnet_id)),
|
||||
],
|
||||
full_metadata={
|
||||
"magnet": magnet_status,
|
||||
"magnet_id": magnet_id,
|
||||
"magnet_name": magnet_name,
|
||||
"relpath": relpath,
|
||||
"file": file_node,
|
||||
"provider": "alldebrid",
|
||||
"provider_view": "files",
|
||||
},
|
||||
full_metadata=metadata,
|
||||
)
|
||||
)
|
||||
if len(results) >= max(1, limit):
|
||||
|
||||
Reference in New Issue
Block a user