Spaces:
Runtime error
Runtime error
Update bot/telegram/media.py
Browse files- bot/telegram/media.py +36 -15
bot/telegram/media.py
CHANGED
|
@@ -1,38 +1,59 @@
|
|
| 1 |
# PATH: bot/telegram/media.py
|
| 2 |
import os
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
from hydrogram import Client
|
| 5 |
from hydrogram.types import Message
|
|
|
|
| 6 |
from bot.temp.files import make_temp_file_path
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
async def download_to_temp(
|
| 11 |
app: Client,
|
| 12 |
m: Message,
|
| 13 |
-
progress: ProgressCb = None,
|
| 14 |
progress_args: tuple = (),
|
| 15 |
-
|
| 16 |
-
) -> Tuple[str, int, str]:
|
| 17 |
"""
|
| 18 |
-
Returns: (file_path,
|
|
|
|
| 19 |
"""
|
| 20 |
media = m.video or m.document
|
| 21 |
if not media:
|
| 22 |
raise RuntimeError("No media to download")
|
| 23 |
|
| 24 |
file_name = getattr(media, "file_name", None) or "video.bin"
|
| 25 |
-
|
| 26 |
|
| 27 |
out_path = make_temp_file_path(file_name)
|
| 28 |
os.makedirs(os.path.dirname(out_path), exist_ok=True)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
return out_path,
|
|
|
|
| 1 |
# PATH: bot/telegram/media.py
|
| 2 |
import os
|
| 3 |
+
import time
|
| 4 |
+
from typing import Callable, Optional, Tuple, Any
|
| 5 |
+
|
| 6 |
from hydrogram import Client
|
| 7 |
from hydrogram.types import Message
|
| 8 |
+
|
| 9 |
from bot.temp.files import make_temp_file_path
|
| 10 |
|
| 11 |
+
|
| 12 |
+
ProgressCb = Callable[[int, int], Any]
|
| 13 |
+
|
| 14 |
|
| 15 |
async def download_to_temp(
|
| 16 |
app: Client,
|
| 17 |
m: Message,
|
| 18 |
+
progress: Optional[ProgressCb] = None,
|
| 19 |
progress_args: tuple = (),
|
| 20 |
+
) -> Tuple[str, int, str, float]:
|
|
|
|
| 21 |
"""
|
| 22 |
+
Returns: (file_path, file_size_bytes, file_name, elapsed_seconds)
|
| 23 |
+
progress(done:int, total:int) -> awaited by Hydrogram (pyrogram-like)
|
| 24 |
"""
|
| 25 |
media = m.video or m.document
|
| 26 |
if not media:
|
| 27 |
raise RuntimeError("No media to download")
|
| 28 |
|
| 29 |
file_name = getattr(media, "file_name", None) or "video.bin"
|
| 30 |
+
tg_size = int(getattr(media, "file_size", 0) or 0)
|
| 31 |
|
| 32 |
out_path = make_temp_file_path(file_name)
|
| 33 |
os.makedirs(os.path.dirname(out_path), exist_ok=True)
|
| 34 |
|
| 35 |
+
t0 = time.time()
|
| 36 |
+
|
| 37 |
+
# Hydrogram (Pyrogram-like) supports progress + progress_args
|
| 38 |
+
if progress:
|
| 39 |
+
await app.download_media(
|
| 40 |
+
message=m,
|
| 41 |
+
file_name=out_path,
|
| 42 |
+
progress=progress,
|
| 43 |
+
progress_args=progress_args,
|
| 44 |
+
)
|
| 45 |
+
else:
|
| 46 |
+
await app.download_media(message=m, file_name=out_path)
|
| 47 |
+
|
| 48 |
+
sec = max(0.001, time.time() - t0)
|
| 49 |
+
|
| 50 |
+
# get real size from disk (telegram sometimes gives 0)
|
| 51 |
+
real_size = 0
|
| 52 |
+
try:
|
| 53 |
+
real_size = os.path.getsize(out_path)
|
| 54 |
+
except Exception:
|
| 55 |
+
real_size = 0
|
| 56 |
+
|
| 57 |
+
size = real_size if real_size > 0 else tg_size
|
| 58 |
|
| 59 |
+
return out_path, int(size), file_name, sec
|