File size: 2,917 Bytes
42ffd82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""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