Spaces:
Paused
Paused
Download utils/progress.py from saske560/userbotenma: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/spaces/saske560/userbotenma/resolve/main/utils/progress.py
- Command line
-
hf download hf://spaces/saske560/userbotenma/utils/progress.py
-
curl -L -o progress.py https://huggingface.co/spaces/saske560/userbotenma/resolve/main/utils/progress.py
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 | |