File size: 1,625 Bytes
df4a9b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import time
import asyncio
from pyrogram.errors import FloodWait
from database import update_progress

async def progreso(cur, tot, msg, times, act, orig_id=None):
    if not msg: return
    now = time.time()
    if not orig_id: orig_id = msg.id # Fallback
    
    safe_cur = int(cur) if cur else 0
    safe_tot = int(tot) if tot else 0
    
    if safe_tot == 0: return

    per = safe_cur * 100 / safe_tot
    
    if isinstance(times, (int, float)):
         times = [times, 0]
    
    last_time = times[0]
    last_per = times[1]
    
    if (now - last_time) > 8 or (per - last_per) > 5 or safe_cur == safe_tot:
        times[0] = now
        times[1] = per
        
        try:
            mb_cur = safe_cur / (1024 * 1024)
            mb_tot = safe_tot / (1024 * 1024)
            
            action_text = act if act else "Enviando"
            emoji = "⬇️" if "Descargando" in action_text else "📤"
            
            from database import task_progress
            existing_url = task_progress.get(msg.chat.id, {}).get(orig_id, {}).get('url', 'N/A')
            update_progress(msg.chat.id, orig_id, percent=per, status=f"{action_text} {emoji}", speed=f"{mb_cur:.1f}MB", url=existing_url)
            
            txt = f"{emoji} {action_text} 📁 `{mb_cur:.1f}/{mb_tot:.1f} MB` (`{per:.1f}%`)"
            print(f"🔄 Progress: {mb_cur:.1f}/{mb_tot:.1f} MB ({per:.1f}%)")
            await msg.edit_text(txt)
            
        except FloodWait as e:
            await asyncio.sleep(e.value)
        except Exception as e:
            pass