This commit is contained in:
2026-01-05 07:51:19 -08:00
parent 8545367e28
commit 1f765cffda
32 changed files with 3447 additions and 3250 deletions

View File

@@ -22,8 +22,8 @@ from Provider import internetarchive as ia_provider
from Provider import alldebrid as ad_provider
from Provider import openlibrary as ol_provider
from SYS.download import DownloadError, _download_direct_file
from SYS.models import DownloadOptions, DownloadMediaResult
from API.HTTP import _download_direct_file
from SYS.models import DownloadError, DownloadOptions, DownloadMediaResult
from SYS.logger import log, debug
from SYS.pipeline_progress import PipelineProgress
from SYS.result_table import ResultTable
@@ -890,7 +890,6 @@ class Download_File(Cmdlet):
return expanded_items
def _process_provider_items(
self,
*,
piped_items: Sequence[Any],
final_output_dir: Path,
@@ -900,8 +899,9 @@ class Download_File(Cmdlet):
registry: Dict[str,
Any],
progress: PipelineProgress,
) -> int:
) -> tuple[int, int]:
downloaded_count = 0
queued_magnet_submissions = 0
get_search_provider = registry.get("get_search_provider")
SearchResult = registry.get("SearchResult")
@@ -911,8 +911,17 @@ class Download_File(Cmdlet):
config=config
)
total_items = len(expanded_items)
processed_items = 0
try:
if total_items:
progress.set_percent(0)
except Exception:
pass
for item in expanded_items:
try:
label = "item"
table = get_field(item, "table")
title = get_field(item, "title")
target = get_field(item, "path") or get_field(item, "url")
@@ -933,6 +942,25 @@ class Download_File(Cmdlet):
if isinstance(extra_md, dict):
full_metadata = extra_md
try:
label = title or target
label = str(label or "item").strip()
if total_items:
pct = int(round((processed_items / max(1, total_items)) * 100))
progress.set_percent(pct)
progress.set_status(
f"downloading {processed_items + 1}/{total_items}: {label}"
)
except Exception:
pass
transfer_label = label
if str(table or "").lower() == "hifi":
try:
progress.begin_transfer(label=transfer_label, total=None)
except Exception:
pass
# If this looks like a provider item and providers are available, prefer provider.download()
downloaded_path: Optional[Path] = None
attempted_provider_download = False
@@ -1065,6 +1093,45 @@ class Download_File(Cmdlet):
continue
# Magnet targets (e.g., torrent provider results) -> submit/download via AllDebrid
if downloaded_path is None and isinstance(target, str) and is_magnet_link(str(target)):
magnet_spec = ad_provider.resolve_magnet_spec(str(target))
if magnet_spec:
def _on_emit(path: Path, file_url: str, relpath: str, metadata: Dict[str, Any]) -> None:
title_hint = metadata.get("name") or relpath or title
self._emit_local_file(
downloaded_path=path,
source=file_url or target,
title_hint=title_hint,
tags_hint=None,
media_kind_hint="file",
full_metadata=metadata,
progress=progress,
config=config,
provider_hint="alldebrid",
)
downloaded, magnet_id = ad_provider.download_magnet(
magnet_spec,
str(target),
final_output_dir,
config,
progress,
quiet_mode,
self._path_from_download_result,
_on_emit,
)
if downloaded > 0:
downloaded_count += downloaded
continue
# If queued but not yet ready, skip the generic unsupported-target error.
if magnet_id is not None:
queued_magnet_submissions += 1
continue
# Fallback: if we have a direct HTTP URL, download it directly
if (downloaded_path is None and isinstance(target,
str)
@@ -1080,6 +1147,7 @@ class Download_File(Cmdlet):
file=sys.stderr,
)
continue
debug(
f"[download-file] Provider item looks like direct URL, downloading: {target}"
)
@@ -1150,8 +1218,22 @@ class Download_File(Cmdlet):
log(f"Download failed: {e}", file=sys.stderr)
except Exception as e:
log(f"Error downloading item: {e}", file=sys.stderr)
finally:
if str(table or "").lower() == "hifi":
try:
progress.finish_transfer(label=transfer_label)
except Exception:
pass
processed_items += 1
try:
pct = int(round((processed_items / max(1, total_items)) * 100))
progress.set_percent(pct)
if processed_items >= total_items:
progress.clear_status()
except Exception:
pass
return downloaded_count
return downloaded_count, queued_magnet_submissions
# === Streaming helpers (yt-dlp) ===
@@ -2687,6 +2769,15 @@ class Download_File(Cmdlet):
debug(f"Output directory: {final_output_dir}")
try:
PipelineProgress(pipeline_context).ensure_local_ui(
label="download-file",
total_items=len(supported_url),
items_preview=supported_url,
)
except Exception:
pass
clip_spec = parsed.get("clip")
query_spec = parsed.get("query")
@@ -3572,7 +3663,7 @@ class Download_File(Cmdlet):
if early_exit is not None:
return int(early_exit)
downloaded_count += self._process_provider_items(
provider_downloaded, magnet_submissions = self._process_provider_items(
piped_items=piped_items,
final_output_dir=final_output_dir,
config=config,
@@ -3580,9 +3671,13 @@ class Download_File(Cmdlet):
registry=registry,
progress=progress,
)
downloaded_count += provider_downloaded
if downloaded_count > 0 or streaming_downloaded > 0:
debug(f"✓ Successfully processed {downloaded_count} file(s)")
if downloaded_count > 0 or streaming_downloaded > 0 or magnet_submissions > 0:
msg = f"✓ Successfully processed {downloaded_count} file(s)"
if magnet_submissions:
msg += f" and queued {magnet_submissions} magnet(s)"
debug(msg)
return 0
if streaming_exit_code is not None: