k
This commit is contained in:
@@ -17,7 +17,6 @@ from contextlib import AbstractContextManager, nullcontext
|
||||
|
||||
import requests
|
||||
|
||||
from API.alldebrid import is_magnet_link
|
||||
from Provider import internetarchive as ia_provider
|
||||
from Provider import alldebrid as ad_provider
|
||||
from Provider import openlibrary as ol_provider
|
||||
@@ -279,15 +278,34 @@ class Download_File(Cmdlet):
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if (provider_name
|
||||
and str(provider_name).lower() == "alldebrid"
|
||||
and is_magnet_link(str(url))):
|
||||
magnet_spec = ad_provider.resolve_magnet_spec(str(url))
|
||||
if magnet_spec:
|
||||
_, magnet_id = ad_provider.prepare_magnet(magnet_spec, config)
|
||||
if magnet_id is not None:
|
||||
provider_for_url = None
|
||||
if provider_name and get_provider is not None:
|
||||
provider_for_url = get_provider(provider_name, config)
|
||||
|
||||
if provider_for_url is not None:
|
||||
try:
|
||||
handled, handled_path = provider_for_url.handle_url(
|
||||
str(url),
|
||||
output_dir=final_output_dir,
|
||||
)
|
||||
except Exception as exc:
|
||||
raise DownloadError(str(exc))
|
||||
if handled:
|
||||
if handled_path:
|
||||
downloaded_path = Path(handled_path)
|
||||
self._emit_local_file(
|
||||
downloaded_path=downloaded_path,
|
||||
source=str(url),
|
||||
title_hint=downloaded_path.stem,
|
||||
tags_hint=None,
|
||||
media_kind_hint="file",
|
||||
full_metadata=None,
|
||||
provider_hint=str(provider_name),
|
||||
progress=progress,
|
||||
config=config,
|
||||
)
|
||||
downloaded_count += 1
|
||||
continue
|
||||
continue
|
||||
|
||||
if provider_name and get_provider is not None and SearchResult is not None:
|
||||
# OpenLibrary URLs should be handled by the OpenLibrary provider.
|
||||
@@ -841,16 +859,16 @@ class Download_File(Cmdlet):
|
||||
return expanded_items
|
||||
|
||||
def _process_provider_items(self,
|
||||
*,
|
||||
piped_items: Sequence[Any],
|
||||
final_output_dir: Path,
|
||||
config: Dict[str,
|
||||
Any],
|
||||
quiet_mode: bool,
|
||||
registry: Dict[str,
|
||||
Any],
|
||||
progress: PipelineProgress,
|
||||
) -> tuple[int, int]:
|
||||
*,
|
||||
piped_items: Sequence[Any],
|
||||
final_output_dir: Path,
|
||||
config: Dict[str,
|
||||
Any],
|
||||
quiet_mode: bool,
|
||||
registry: Dict[str,
|
||||
Any],
|
||||
progress: PipelineProgress,
|
||||
) -> tuple[int, int]:
|
||||
downloaded_count = 0
|
||||
queued_magnet_submissions = 0
|
||||
get_search_provider = registry.get("get_search_provider")
|
||||
@@ -916,9 +934,10 @@ class Download_File(Cmdlet):
|
||||
downloaded_path: Optional[Path] = None
|
||||
attempted_provider_download = False
|
||||
provider_sr = None
|
||||
provider_obj = None
|
||||
if table and get_search_provider and SearchResult:
|
||||
provider = get_search_provider(str(table), config)
|
||||
if provider is not None:
|
||||
provider_obj = get_search_provider(str(table), config)
|
||||
if provider_obj is not None:
|
||||
attempted_provider_download = True
|
||||
sr = SearchResult(
|
||||
table=str(table),
|
||||
@@ -944,9 +963,53 @@ class Download_File(Cmdlet):
|
||||
except Exception:
|
||||
output_dir = final_output_dir
|
||||
|
||||
downloaded_path = provider.download(sr, output_dir)
|
||||
downloaded_path = provider_obj.download(sr, output_dir)
|
||||
provider_sr = sr
|
||||
|
||||
if downloaded_path is None:
|
||||
download_items = getattr(provider_obj, "download_items", None)
|
||||
if callable(download_items):
|
||||
|
||||
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=tags_list,
|
||||
media_kind_hint="file",
|
||||
full_metadata=metadata,
|
||||
progress=progress,
|
||||
config=config,
|
||||
provider_hint=str(table) if table else None,
|
||||
)
|
||||
|
||||
try:
|
||||
downloaded_extra = download_items(
|
||||
sr,
|
||||
output_dir,
|
||||
emit=_on_emit,
|
||||
progress=progress,
|
||||
quiet_mode=quiet_mode,
|
||||
path_from_result=self._path_from_download_result,
|
||||
config=config,
|
||||
)
|
||||
except TypeError:
|
||||
downloaded_extra = download_items(
|
||||
sr,
|
||||
output_dir,
|
||||
emit=_on_emit,
|
||||
progress=progress,
|
||||
quiet_mode=quiet_mode,
|
||||
path_from_result=self._path_from_download_result,
|
||||
)
|
||||
except Exception:
|
||||
downloaded_extra = 0
|
||||
|
||||
if downloaded_extra:
|
||||
downloaded_count += int(downloaded_extra)
|
||||
continue
|
||||
|
||||
# OpenLibrary: if provider download failed, do NOT try to download the OpenLibrary page HTML.
|
||||
if (downloaded_path is None and attempted_provider_download
|
||||
and str(table or "").lower() == "openlibrary"):
|
||||
@@ -1044,45 +1107,6 @@ 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)
|
||||
|
||||
Reference in New Issue
Block a user