Spaces:
No application file
No application file
Update bot/telegram/media.py
Browse files- bot/telegram/media.py +28 -5
bot/telegram/media.py
CHANGED
|
@@ -1,12 +1,22 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
-
Returns: (file_path, file_size, file_name)
|
|
|
|
| 10 |
"""
|
| 11 |
media = m.video or m.document
|
| 12 |
if not media:
|
|
@@ -18,6 +28,19 @@ async def download_to_temp(app: Client, m: Message) -> tuple[str, int, str]:
|
|
| 18 |
out_path = make_temp_file_path(file_name)
|
| 19 |
os.makedirs(os.path.dirname(out_path), exist_ok=True)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# PATH: bot/telegram/media.py
|
| 2 |
import os
|
| 3 |
+
import time
|
| 4 |
+
from typing import Callable, Optional, Tuple
|
| 5 |
from hydrogram import Client
|
| 6 |
from hydrogram.types import Message
|
| 7 |
from bot.temp.files import make_temp_file_path
|
| 8 |
|
| 9 |
+
ProgressCb = Callable[[int, int], None]
|
| 10 |
+
|
| 11 |
+
async def download_to_temp(
|
| 12 |
+
app: Client,
|
| 13 |
+
m: Message,
|
| 14 |
+
progress: Optional[ProgressCb] = None,
|
| 15 |
+
progress_args: tuple = (),
|
| 16 |
+
) -> Tuple[str, int, str, float]:
|
| 17 |
"""
|
| 18 |
+
Returns: (file_path, file_size, file_name, elapsed_sec)
|
| 19 |
+
progress(done, total) will be called if supported by Hydrogram.
|
| 20 |
"""
|
| 21 |
media = m.video or m.document
|
| 22 |
if not media:
|
|
|
|
| 28 |
out_path = make_temp_file_path(file_name)
|
| 29 |
os.makedirs(os.path.dirname(out_path), exist_ok=True)
|
| 30 |
|
| 31 |
+
t0 = time.time()
|
| 32 |
+
|
| 33 |
+
# Hydrogram (Pyrogram-like) usually supports progress/progress_args.
|
| 34 |
+
# If not supported, fall back.
|
| 35 |
+
try:
|
| 36 |
+
await app.download_media(
|
| 37 |
+
message=m,
|
| 38 |
+
file_name=out_path,
|
| 39 |
+
progress=progress,
|
| 40 |
+
progress_args=progress_args,
|
| 41 |
+
)
|
| 42 |
+
except TypeError:
|
| 43 |
+
await app.download_media(message=m, file_name=out_path)
|
| 44 |
+
|
| 45 |
+
elapsed = max(0.001, time.time() - t0)
|
| 46 |
+
return out_path, file_size, file_name, elapsed
|