This commit is contained in:
nose
2025-12-19 02:29:42 -08:00
parent d637532237
commit 52cf3f5c9f
24 changed files with 1284 additions and 176 deletions

View File

@@ -12,6 +12,7 @@ from __future__ import annotations
import sys
from pathlib import Path
from typing import Any, Dict, List, Optional, Sequence
from urllib.parse import urlparse
from SYS.download import DownloadError, _download_direct_file
from SYS.logger import log, debug
@@ -102,7 +103,7 @@ class Download_File(Cmdlet):
get_search_provider = None
SearchResult = None
def _emit_local_file(downloaded_path: Path, source: Optional[str], title_hint: Optional[str], tags_hint: Optional[List[str]], media_kind_hint: Optional[str], full_metadata: Optional[Dict[str, Any]]) -> None:
def _emit_local_file(downloaded_path: Path, source: Optional[str], title_hint: Optional[str], tags_hint: Optional[List[str]], media_kind_hint: Optional[str], full_metadata: Optional[Dict[str, Any]], provider_hint: Optional[str] = None) -> None:
title_val = (title_hint or downloaded_path.stem or "Unknown").strip() or downloaded_path.stem
hash_value = self._compute_file_hash(downloaded_path)
tag: List[str] = []
@@ -121,6 +122,8 @@ class Download_File(Cmdlet):
"media_kind": media_kind_hint or "file",
"tag": tag,
}
if provider_hint:
payload["provider"] = str(provider_hint)
if full_metadata:
payload["full_metadata"] = full_metadata
if source and str(source).startswith("http"):
@@ -140,6 +143,79 @@ class Download_File(Cmdlet):
try:
debug(f"Processing URL: {url}")
# Telegram message URLs are not direct files; route through the provider.
try:
parsed = urlparse(str(url))
host = (parsed.hostname or "").lower().strip()
except Exception:
host = ""
is_telegram = host in {"t.me", "telegram.me"} or host.endswith(".t.me")
if is_telegram and SearchResult:
try:
from ProviderCore.registry import get_provider as _get_provider
except Exception:
_get_provider = None
if _get_provider is None:
raise DownloadError("Telegram provider registry not available")
provider = _get_provider("telegram", config)
if provider is None:
raise DownloadError("Telegram provider not configured or not available (check telethon/app_id/api_hash)")
sr = SearchResult(table="telegram", title=str(url), path=str(url), full_metadata={})
downloaded_path = None
telegram_info: Optional[Dict[str, Any]] = None
if hasattr(provider, "download_url"):
try:
downloaded_path, telegram_info = provider.download_url(str(url), final_output_dir) # type: ignore[attr-defined]
except Exception as exc:
raise DownloadError(str(exc))
else:
downloaded_path = provider.download(sr, final_output_dir)
if not downloaded_path:
raise DownloadError("Telegram download returned no file")
channel = ""
post = None
if isinstance(telegram_info, dict):
try:
chat_info = telegram_info.get("chat") if isinstance(telegram_info.get("chat"), dict) else {}
msg_info = telegram_info.get("message") if isinstance(telegram_info.get("message"), dict) else {}
channel = str(chat_info.get("title") or chat_info.get("username") or "").strip()
post = msg_info.get("id")
except Exception:
channel = ""
post = None
title_hint = None
tags_hint: List[str] = []
if channel:
tags_hint.append(f"channel:{channel}")
if post is not None:
tags_hint.append(f"post:{post}")
if channel and post is not None:
title_hint = f"{channel} {post}"
elif post is not None:
title_hint = f"post:{post}"
else:
title_hint = downloaded_path.stem
_emit_local_file(
downloaded_path=downloaded_path,
source=str(url),
title_hint=title_hint,
tags_hint=tags_hint,
media_kind_hint="file",
full_metadata=telegram_info,
provider_hint="telegram",
)
downloaded_count += 1
debug("✓ Downloaded via Telegram provider and emitted")
continue
result_obj = _download_direct_file(url, final_output_dir, quiet=quiet_mode)
file_path = None
if hasattr(result_obj, "path"):