Add YAPF style + ignore, and format tracked Python files

This commit is contained in:
2025-12-29 18:42:02 -08:00
parent c019c00aed
commit 507946a3e4
108 changed files with 11664 additions and 6494 deletions

View File

@@ -37,7 +37,7 @@ def _get_debrid_api_key(config: Dict[str, Any]) -> Optional[str]:
# 2) store.debrid block (canonical for debrid store configuration)
try:
from config import get_debrid_api_key
from SYS.config import get_debrid_api_key
key = get_debrid_api_key(config, service="All-debrid")
return key.strip() if key else None
@@ -97,23 +97,27 @@ class AllDebrid(Provider):
# Quiet mode when download-file is mid-pipeline.
quiet = (
bool(self.config.get("_quiet_background_output"))
if isinstance(self.config, dict)
else False
if isinstance(self.config,
dict) else False
)
unlocked_url = target
try:
unlocked = client.unlock_link(target)
if isinstance(unlocked, str) and unlocked.strip().startswith(
("http://", "https://")
):
if isinstance(unlocked,
str) and unlocked.strip().startswith(("http://",
"https://")):
unlocked_url = unlocked.strip()
except Exception as exc:
# Fall back to the raw link, but warn.
log(f"[alldebrid] Failed to unlock link: {exc}", file=sys.stderr)
# Prefer provider title as the output filename.
suggested = sanitize_filename(str(getattr(result, "title", "") or "").strip())
suggested = sanitize_filename(
str(getattr(result,
"title",
"") or "").strip()
)
suggested_name = suggested if suggested else None
try:
@@ -142,11 +146,9 @@ class AllDebrid(Provider):
try:
if downloaded_path.exists():
size = downloaded_path.stat().st_size
if (
size > 0
and size <= 250_000
and downloaded_path.suffix.lower() not in (".html", ".htm")
):
if (size > 0 and size <= 250_000
and downloaded_path.suffix.lower() not in (".html",
".htm")):
head = downloaded_path.read_bytes()[:512]
try:
text = head.decode("utf-8", errors="ignore").lower()
@@ -173,9 +175,10 @@ class AllDebrid(Provider):
return None
@staticmethod
def _flatten_files(
items: Any, *, _prefix: Optional[List[str]] = None
) -> Iterable[Dict[str, Any]]:
def _flatten_files(items: Any,
*,
_prefix: Optional[List[str]] = None) -> Iterable[Dict[str,
Any]]:
"""Flatten AllDebrid magnet file tree into file dicts, preserving relative paths.
API commonly returns:
@@ -211,7 +214,9 @@ 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)
@@ -222,7 +227,8 @@ class AllDebrid(Provider):
self,
query: str,
limit: int = 50,
filters: Optional[Dict[str, Any]] = None,
filters: Optional[Dict[str,
Any]] = None,
**kwargs: Any,
) -> List[SearchResult]:
q = (query or "").strip()
@@ -247,7 +253,9 @@ class AllDebrid(Provider):
return []
q_lower = q.lower()
needle = "" if q_lower in {"*", "all", "list"} else q_lower
needle = "" if q_lower in {"*",
"all",
"list"} else q_lower
# Second-stage: list files for a specific magnet id.
if view == "files":
@@ -262,17 +270,16 @@ class AllDebrid(Provider):
except Exception:
return []
magnet_status: Dict[str, Any] = {}
magnet_status: Dict[str,
Any] = {}
try:
magnet_status = client.magnet_status(magnet_id)
except Exception:
magnet_status = {}
magnet_name = str(
magnet_status.get("filename")
or magnet_status.get("name")
or magnet_status.get("hash")
or f"magnet-{magnet_id}"
magnet_status.get("filename") or magnet_status.get("name")
or magnet_status.get("hash") or f"magnet-{magnet_id}"
)
status_code = magnet_status.get("statusCode")
status_text = str(magnet_status.get("status") or "").strip() or "unknown"
@@ -285,25 +292,40 @@ class AllDebrid(Provider):
title=magnet_name,
path=f"alldebrid:magnet:{magnet_id}",
detail=status_text,
annotations=["folder", "not-ready"],
annotations=["folder",
"not-ready"],
media_kind="folder",
tag={"alldebrid", "folder", str(magnet_id), "not-ready"},
tag={"alldebrid",
"folder",
str(magnet_id),
"not-ready"},
columns=[
("Folder", magnet_name),
("ID", str(magnet_id)),
("Status", status_text),
("Ready", "no"),
("Folder",
magnet_name),
("ID",
str(magnet_id)),
("Status",
status_text),
("Ready",
"no"),
],
full_metadata={"magnet": magnet_status, "magnet_id": magnet_id},
full_metadata={
"magnet": magnet_status,
"magnet_id": magnet_id
},
)
]
try:
files_result = client.magnet_links([magnet_id])
magnet_files = (
files_result.get(str(magnet_id), {}) if isinstance(files_result, dict) else {}
files_result.get(str(magnet_id),
{}) if isinstance(files_result,
dict) else {}
)
file_tree = magnet_files.get("files", []) if isinstance(magnet_files, dict) else []
file_tree = magnet_files.get("files",
[]) if isinstance(magnet_files,
dict) else []
except Exception as exc:
log(
f"[alldebrid] Failed to list files for magnet {magnet_id}: {exc}",
@@ -313,8 +335,10 @@ class AllDebrid(Provider):
results: List[SearchResult] = []
for file_node in self._flatten_files(file_tree):
file_name = str(file_node.get("n") or file_node.get("name") or "").strip()
file_url = str(file_node.get("l") or file_node.get("link") or "").strip()
file_name = str(file_node.get("n") or file_node.get("name")
or "").strip()
file_url = str(file_node.get("l") or file_node.get("link")
or "").strip()
relpath = str(file_node.get("_relpath") or file_name or "").strip()
file_size = file_node.get("s") or file_node.get("size")
if not file_name or not file_url:
@@ -341,11 +365,16 @@ class AllDebrid(Provider):
annotations=["file"],
media_kind="file",
size_bytes=size_bytes,
tag={"alldebrid", "file", str(magnet_id)},
tag={"alldebrid",
"file",
str(magnet_id)},
columns=[
("File", file_name),
("Folder", magnet_name),
("ID", str(magnet_id)),
("File",
file_name),
("Folder",
magnet_name),
("ID",
str(magnet_id)),
],
full_metadata={
"magnet": magnet_status,
@@ -386,9 +415,7 @@ class AllDebrid(Provider):
continue
magnet_name = str(
magnet.get("filename")
or magnet.get("name")
or magnet.get("hash")
magnet.get("filename") or magnet.get("name") or magnet.get("hash")
or f"magnet-{magnet_id}"
)
magnet_name_lower = magnet_name.lower()
@@ -422,15 +449,24 @@ class AllDebrid(Provider):
annotations=["folder"],
media_kind="folder",
size_bytes=size_bytes,
tag={"alldebrid", "folder", str(magnet_id)}
tag={"alldebrid",
"folder",
str(magnet_id)}
| ({"ready"} if ready else {"not-ready"}),
columns=[
("Folder", magnet_name),
("ID", str(magnet_id)),
("Status", status_text),
("Ready", "yes" if ready else "no"),
("Folder",
magnet_name),
("ID",
str(magnet_id)),
("Status",
status_text),
("Ready",
"yes" if ready else "no"),
],
full_metadata={"magnet": magnet, "magnet_id": magnet_id},
full_metadata={
"magnet": magnet,
"magnet_id": magnet_id
},
)
)