hj
Some checks failed
smoke-mm / Install & smoke test mm --help (push) Has been cancelled
Some checks failed
smoke-mm / Install & smoke test mm --help (push) Has been cancelled
This commit is contained in:
@@ -13,6 +13,74 @@ from urllib.parse import urlparse
|
||||
from ProviderCore.base import Provider, SearchResult
|
||||
|
||||
|
||||
_TELEGRAM_DEFAULT_TIMESTAMP_STEM_RE = re.compile(
|
||||
r"^(?P<prefix>photo|video|document|audio|voice|animation)_(?P<date>\d{4}-\d{2}-\d{2})_(?P<time>\d{2}-\d{2}-\d{2})(?: \(\d+\))?$",
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
|
||||
|
||||
def _unique_path(path: Path) -> Path:
|
||||
try:
|
||||
if not path.exists():
|
||||
return path
|
||||
except Exception:
|
||||
return path
|
||||
|
||||
stem = path.stem
|
||||
suffix = path.suffix
|
||||
parent = path.parent
|
||||
for i in range(1, 10_000):
|
||||
candidate = parent / f"{stem} ({i}){suffix}"
|
||||
try:
|
||||
if not candidate.exists():
|
||||
return candidate
|
||||
except Exception:
|
||||
return candidate
|
||||
return parent / f"{stem} (copy){suffix}"
|
||||
|
||||
|
||||
def _maybe_strip_telegram_timestamped_default_filename(*, downloaded_path: Path) -> Path:
|
||||
"""Normalize Telethon's default timestamped names.
|
||||
|
||||
Examples:
|
||||
- photo_2025-12-27_02-58-09.jpg -> photo.jpg
|
||||
"""
|
||||
try:
|
||||
stem = downloaded_path.stem
|
||||
suffix = downloaded_path.suffix
|
||||
except Exception:
|
||||
return downloaded_path
|
||||
|
||||
if not suffix:
|
||||
return downloaded_path
|
||||
|
||||
m = _TELEGRAM_DEFAULT_TIMESTAMP_STEM_RE.fullmatch(str(stem))
|
||||
if not m:
|
||||
return downloaded_path
|
||||
|
||||
prefix = str(m.group("prefix") or "").strip().lower()
|
||||
if not prefix:
|
||||
return downloaded_path
|
||||
|
||||
new_candidate = downloaded_path.with_name(f"{prefix}{suffix}")
|
||||
if new_candidate == downloaded_path:
|
||||
return downloaded_path
|
||||
|
||||
new_path = _unique_path(new_candidate)
|
||||
try:
|
||||
if downloaded_path.exists():
|
||||
try:
|
||||
downloaded_path.rename(new_path)
|
||||
return new_path
|
||||
except Exception:
|
||||
shutil.move(str(downloaded_path), str(new_path))
|
||||
return new_path
|
||||
except Exception:
|
||||
return downloaded_path
|
||||
|
||||
return downloaded_path
|
||||
|
||||
|
||||
def _looks_like_telegram_message_url(url: str) -> bool:
|
||||
try:
|
||||
parsed = urlparse(str(url))
|
||||
@@ -945,6 +1013,13 @@ class Telegram(Provider):
|
||||
raise Exception("Telegram download returned no file")
|
||||
downloaded_path = Path(str(downloaded))
|
||||
|
||||
# Telethon's default media filenames include timestamps (e.g. photo_YYYY-MM-DD_HH-MM-SS.jpg).
|
||||
# Strip those timestamps ONLY when Telegram didn't provide an explicit filename.
|
||||
if not file_name:
|
||||
downloaded_path = _maybe_strip_telegram_timestamped_default_filename(
|
||||
downloaded_path=downloaded_path,
|
||||
)
|
||||
|
||||
date_iso = None
|
||||
try:
|
||||
if msg_date is not None and hasattr(msg_date, "isoformat"):
|
||||
|
||||
Reference in New Issue
Block a user