dfd
This commit is contained in:
@@ -2,6 +2,8 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Optional, Tuple
|
||||
from urllib.parse import urlparse
|
||||
@@ -143,6 +145,21 @@ class Telegram(Provider):
|
||||
session_base = self._session_base_path()
|
||||
chat, message_id = _parse_telegram_message_url(url)
|
||||
|
||||
def _format_bytes(num: Optional[int]) -> str:
|
||||
try:
|
||||
if num is None:
|
||||
return "?B"
|
||||
n = float(num)
|
||||
suffixes = ["B", "KB", "MB", "GB", "TB"]
|
||||
for s in suffixes:
|
||||
if n < 1024 or s == suffixes[-1]:
|
||||
if s == "B":
|
||||
return f"{int(n)}{s}"
|
||||
return f"{n:.1f}{s}"
|
||||
n /= 1024
|
||||
except Exception:
|
||||
return "?B"
|
||||
|
||||
client = TelegramClient(str(session_base), app_id, api_hash)
|
||||
try:
|
||||
# This prompts on first run for phone/code and persists the session.
|
||||
@@ -226,7 +243,35 @@ class Telegram(Provider):
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
downloaded = _resolve(client.download_media(message, file=str(output_dir)))
|
||||
# Progress callback: prints to stderr so it doesn't interfere with pipeline stdout.
|
||||
last_print = {"t": 0.0}
|
||||
def _progress(current: int, total: int) -> None:
|
||||
try:
|
||||
now = time.monotonic()
|
||||
# Throttle to avoid spamming.
|
||||
if now - float(last_print.get("t", 0.0)) < 0.25 and current < total:
|
||||
return
|
||||
last_print["t"] = now
|
||||
|
||||
pct = ""
|
||||
try:
|
||||
if total and total > 0:
|
||||
pct = f" {min(100.0, (current / total) * 100.0):5.1f}%"
|
||||
except Exception:
|
||||
pct = ""
|
||||
|
||||
line = f"[telegram] Downloading{pct} ({_format_bytes(current)}/{_format_bytes(total)})"
|
||||
sys.stderr.write("\r" + line)
|
||||
sys.stderr.flush()
|
||||
except Exception:
|
||||
return
|
||||
|
||||
downloaded = _resolve(client.download_media(message, file=str(output_dir), progress_callback=_progress))
|
||||
try:
|
||||
sys.stderr.write("\n")
|
||||
sys.stderr.flush()
|
||||
except Exception:
|
||||
pass
|
||||
if not downloaded:
|
||||
raise Exception("Telegram download returned no file")
|
||||
downloaded_path = Path(str(downloaded))
|
||||
|
||||
Reference in New Issue
Block a user