Spaces:
Runtime error
Runtime error
Update bot/telegram/media.py
Browse files- bot/telegram/media.py +6 -42
bot/telegram/media.py
CHANGED
|
@@ -1,59 +1,23 @@
|
|
| 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,
|
| 23 |
-
|
| 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 |
-
|
| 31 |
|
| 32 |
out_path = make_temp_file_path(file_name)
|
| 33 |
os.makedirs(os.path.dirname(out_path), exist_ok=True)
|
| 34 |
|
| 35 |
-
|
| 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
|
|
|
|
| 1 |
# PATH: bot/telegram/media.py
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
from hydrogram import Client
|
| 4 |
from hydrogram.types import Message
|
|
|
|
| 5 |
from bot.temp.files import make_temp_file_path
|
| 6 |
|
| 7 |
+
async def download_to_temp(app: Client, m: Message) -> tuple[str, int, str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
+
Returns: (file_path, file_size, file_name)
|
| 10 |
+
NOTE: Always returns exactly 3 values.
|
| 11 |
"""
|
| 12 |
media = m.video or m.document
|
| 13 |
if not media:
|
| 14 |
raise RuntimeError("No media to download")
|
| 15 |
|
| 16 |
file_name = getattr(media, "file_name", None) or "video.bin"
|
| 17 |
+
file_size = int(getattr(media, "file_size", 0) or 0)
|
| 18 |
|
| 19 |
out_path = make_temp_file_path(file_name)
|
| 20 |
os.makedirs(os.path.dirname(out_path), exist_ok=True)
|
| 21 |
|
| 22 |
+
await app.download_media(message=m, file_name=out_path)
|
| 23 |
+
return out_path, file_size, file_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|