userbotenma / utils /progress.py
saske560's picture
Upload 3 files
ce258ed verified
Raw History Blame Contribute Delete
2.92 kB
"""utils/progress.py β€” Progress bar helpers for download/upload."""
import time
FILLED = "β¬’"
EMPTY = "⬑"
BAR_W = 10
# ── Formatters ────────────────────────────────────────────────────────────────
def fmt_bytes(n: float) -> str:
if n < 1024:
return f"{n:.0f} B"
n /= 1024
if n < 1024:
return f"{n:.1f} KB"
n /= 1024
if n < 1024:
return f"{n:.2f} MB"
n /= 1024
return f"{n:.2f} GB"
def fmt_speed(bps: float) -> str:
if bps <= 0:
return "calculando..."
if bps >= 1024 ** 2:
return f"{bps / 1024**2:.2f} MB/s"
if bps >= 1024:
return f"{bps / 1024:.1f} KB/s"
return f"{bps:.0f} B/s"
def fmt_eta(secs: float) -> str:
s = max(0, int(secs))
if s < 60:
return f"{s}s"
if s < 3600:
return f"{s // 60}m {s % 60:02d}s"
return f"{s // 3600}h {(s % 3600) // 60:02d}m"
def make_bar(pct: float) -> str:
filled = round(pct / 100 * BAR_W)
return FILLED * filled + EMPTY * (BAR_W - filled)
# ── Text builders ─────────────────────────────────────────────────────────────
def dl_text(current: int, total: int, start_time: float,
op: str = "πŸ“₯ Descargando") -> str:
pct = (current / total * 100) if total else 0
elapsed = max(time.time() - start_time, 0.01)
speed = current / elapsed
eta = (total - current) / speed if speed > 0 else 0
bar = make_bar(pct)
total_s = fmt_bytes(total) if total else "?"
return (
f"**{op} desde Telegram**\n\n"
f"`{bar}` **{pct:.1f}%**\n\n"
f"πŸ“¦ **Progreso:** `{fmt_bytes(current)}` / `{total_s}`\n"
f"⚑ **Velocidad:** `{fmt_speed(speed)}`\n"
f"⏱️ **Restante:** `~{fmt_eta(eta)}`\n"
f"πŸ’Ύ **TamaΓ±o:** `{total_s}`"
)
def ul_text(current: int, total: int, start_time: float) -> str:
return dl_text(current, total, start_time, op="πŸ“€ Subiendo")
# ── Pyrogram progress callback ────────────────────────────────────────────────
async def progress_cb(current: int, total: int,
status_msg, t0: float, last_edit: list,
op: str = "πŸ“₯ Descargando") -> None:
"""
Pass as `progress=progress_cb, progress_args=(msg, t0, last_edit, op)`.
Throttles edits to once per 1.5 s to avoid FloodWait.
"""
now = time.time()
if current < total and (now - last_edit[0]) < 1.5:
return
last_edit[0] = now
try:
await status_msg.edit(dl_text(current, total, t0, op))
except Exception:
pass