j
This commit is contained in:
@@ -13,6 +13,156 @@ from ProviderCore.base import Provider, SearchResult
|
||||
from ProviderCore.download import sanitize_filename
|
||||
from SYS.logger import log
|
||||
|
||||
# Helper for download-file: render selectable formats for a details URL.
|
||||
def maybe_show_formats_table(
|
||||
*,
|
||||
raw_urls: Any,
|
||||
piped_items: Any,
|
||||
parsed: Dict[str, Any],
|
||||
config: Dict[str, Any],
|
||||
quiet_mode: bool,
|
||||
get_field: Any,
|
||||
) -> Optional[int]:
|
||||
"""If input is a single Internet Archive details URL, render a formats table.
|
||||
|
||||
Returns an exit code when handled; otherwise None.
|
||||
"""
|
||||
if quiet_mode:
|
||||
return None
|
||||
|
||||
try:
|
||||
total_inputs = int(len(raw_urls or []) + len(piped_items or []))
|
||||
except Exception:
|
||||
total_inputs = 0
|
||||
|
||||
if total_inputs != 1:
|
||||
return None
|
||||
|
||||
item = piped_items[0] if piped_items else None
|
||||
target = ""
|
||||
if item is not None:
|
||||
try:
|
||||
target = str(get_field(item,
|
||||
"path") or get_field(item,
|
||||
"url") or "").strip()
|
||||
except Exception:
|
||||
target = ""
|
||||
if not target and raw_urls:
|
||||
target = str(raw_urls[0]).strip()
|
||||
if not target:
|
||||
return None
|
||||
|
||||
identifier = ""
|
||||
try:
|
||||
md = get_field(item, "full_metadata") if item is not None else None
|
||||
if isinstance(md, dict):
|
||||
identifier = str(md.get("identifier") or "").strip()
|
||||
except Exception:
|
||||
identifier = ""
|
||||
if not identifier:
|
||||
try:
|
||||
identifier = str(extract_identifier(target) or "").strip()
|
||||
except Exception:
|
||||
identifier = ""
|
||||
if not identifier:
|
||||
return None
|
||||
|
||||
# Only show picker for item pages (details); direct download URLs should download immediately.
|
||||
try:
|
||||
if not is_details_url(target):
|
||||
return None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
try:
|
||||
files = list_download_files(identifier)
|
||||
except Exception as exc:
|
||||
log(f"download-file: Internet Archive lookup failed: {exc}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if not files:
|
||||
log("download-file: Internet Archive item has no downloadable files", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
title = ""
|
||||
try:
|
||||
title = str(get_field(item, "title") or "").strip() if item is not None else ""
|
||||
except Exception:
|
||||
title = ""
|
||||
table_title = (
|
||||
f"Internet Archive: {title}".strip().rstrip(":")
|
||||
if title else f"Internet Archive: {identifier}"
|
||||
)
|
||||
|
||||
try:
|
||||
from SYS.result_table import ResultTable
|
||||
from SYS import pipeline as pipeline_context
|
||||
except Exception as exc:
|
||||
log(f"download-file: ResultTable unavailable: {exc}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
base_args: List[str] = []
|
||||
out_arg = parsed.get("path") or parsed.get("output")
|
||||
if out_arg:
|
||||
base_args.extend(["-path", str(out_arg)])
|
||||
|
||||
table = ResultTable(table_title).set_preserve_order(True)
|
||||
table.set_table("internetarchive.formats")
|
||||
table.set_source_command("download-file", base_args)
|
||||
|
||||
rows: List[Dict[str, Any]] = []
|
||||
for f in files:
|
||||
name = str(f.get("name") or "").strip()
|
||||
if not name:
|
||||
continue
|
||||
fmt = str(f.get("format") or "").strip()
|
||||
src = str(f.get("source") or "").strip()
|
||||
direct_url = str(f.get("direct_url") or "").strip()
|
||||
if not direct_url:
|
||||
continue
|
||||
|
||||
size_val: Any = f.get("size")
|
||||
try:
|
||||
size_val = int(size_val) if size_val not in (None, "") else ""
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
row_item: Dict[str, Any] = {
|
||||
"table": "internetarchive",
|
||||
"title": fmt or name,
|
||||
"path": direct_url,
|
||||
"url": direct_url,
|
||||
"columns": [
|
||||
("Format", fmt),
|
||||
("Name", name),
|
||||
("Size", size_val),
|
||||
("Source", src),
|
||||
],
|
||||
"_selection_args": [direct_url],
|
||||
"full_metadata": {
|
||||
"identifier": identifier,
|
||||
"name": name,
|
||||
"format": fmt,
|
||||
"source": src,
|
||||
"size": f.get("size"),
|
||||
},
|
||||
}
|
||||
rows.append(row_item)
|
||||
table.add_result(row_item)
|
||||
|
||||
if not rows:
|
||||
log("download-file: no downloadable files found for this item", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
try:
|
||||
pipeline_context.set_last_result_table(table, rows, subject=item)
|
||||
pipeline_context.set_current_stage_table(table)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
log("Internet Archive item detected: select a file with @N to download", file=sys.stderr)
|
||||
return 0
|
||||
|
||||
|
||||
def _ia() -> Any:
|
||||
try:
|
||||
@@ -322,6 +472,7 @@ class InternetArchive(Provider):
|
||||
collection="..." # optional (upload)
|
||||
mediatype="..." # optional (upload)
|
||||
"""
|
||||
URL = ("archive.org",)
|
||||
|
||||
def __init__(self, config: Optional[Dict[str, Any]] = None):
|
||||
super().__init__(config)
|
||||
|
||||
Reference in New Issue
Block a user