Spaces:
No application file
No application file
Update bot/telegram/media.py
Browse files- bot/telegram/media.py +59 -13
bot/telegram/media.py
CHANGED
|
@@ -1,22 +1,64 @@
|
|
| 1 |
# PATH: bot/telegram/media.py
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
from hydrogram import Client
|
| 6 |
from hydrogram.types import Message
|
|
|
|
| 7 |
from bot.temp.files import make_temp_file_path
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
async def download_to_temp(
|
| 12 |
app: Client,
|
| 13 |
m: Message,
|
| 14 |
-
|
| 15 |
-
progress_args:
|
| 16 |
-
|
|
|
|
| 17 |
"""
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
"""
|
| 21 |
media = m.video or m.document
|
| 22 |
if not media:
|
|
@@ -28,19 +70,23 @@ async def download_to_temp(
|
|
| 28 |
out_path = make_temp_file_path(file_name)
|
| 29 |
os.makedirs(os.path.dirname(out_path), exist_ok=True)
|
| 30 |
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
| 34 |
-
# If not supported, fall back.
|
| 35 |
try:
|
|
|
|
|
|
|
| 36 |
await app.download_media(
|
| 37 |
message=m,
|
| 38 |
file_name=out_path,
|
| 39 |
-
progress=
|
| 40 |
progress_args=progress_args,
|
| 41 |
)
|
| 42 |
except TypeError:
|
|
|
|
| 43 |
await app.download_media(message=m, file_name=out_path)
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
| 1 |
# PATH: bot/telegram/media.py
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
+
import asyncio
|
| 5 |
+
import inspect
|
| 6 |
+
from typing import Callable, Any, Optional, Tuple
|
| 7 |
+
|
| 8 |
from hydrogram import Client
|
| 9 |
from hydrogram.types import Message
|
| 10 |
+
|
| 11 |
from bot.temp.files import make_temp_file_path
|
| 12 |
|
| 13 |
+
|
| 14 |
+
def _wrap_progress(cb: Callable[..., Any], extra_args: Tuple[Any, ...] = ()):
|
| 15 |
+
"""
|
| 16 |
+
Hydrogram/Pyrogram progress callback is usually sync:
|
| 17 |
+
progress(current, total, *args)
|
| 18 |
+
|
| 19 |
+
If user passes async cb, we schedule it.
|
| 20 |
+
"""
|
| 21 |
+
if not cb:
|
| 22 |
+
return None, ()
|
| 23 |
+
|
| 24 |
+
async_cb = inspect.iscoroutinefunction(cb)
|
| 25 |
+
|
| 26 |
+
def _progress(current: int, total: int, *args):
|
| 27 |
+
try:
|
| 28 |
+
cur = int(current or 0)
|
| 29 |
+
tot = int(total or 0)
|
| 30 |
+
except Exception:
|
| 31 |
+
cur, tot = 0, 0
|
| 32 |
+
|
| 33 |
+
if async_cb:
|
| 34 |
+
try:
|
| 35 |
+
asyncio.create_task(cb(cur, tot, *extra_args))
|
| 36 |
+
except Exception:
|
| 37 |
+
pass
|
| 38 |
+
else:
|
| 39 |
+
try:
|
| 40 |
+
cb(cur, tot, *extra_args)
|
| 41 |
+
except Exception:
|
| 42 |
+
pass
|
| 43 |
+
|
| 44 |
+
return _progress, ()
|
| 45 |
+
|
| 46 |
|
| 47 |
async def download_to_temp(
|
| 48 |
app: Client,
|
| 49 |
m: Message,
|
| 50 |
+
progress_cb: Optional[Callable[..., Any]] = None,
|
| 51 |
+
progress_args: Tuple[Any, ...] = (),
|
| 52 |
+
return_meta: bool = False,
|
| 53 |
+
):
|
| 54 |
"""
|
| 55 |
+
Download Telegram media (video/document) to a temp path.
|
| 56 |
+
|
| 57 |
+
Returns (default):
|
| 58 |
+
(file_path, file_size, file_name)
|
| 59 |
+
|
| 60 |
+
If return_meta=True:
|
| 61 |
+
(file_path, file_size, file_name, download_seconds)
|
| 62 |
"""
|
| 63 |
media = m.video or m.document
|
| 64 |
if not media:
|
|
|
|
| 70 |
out_path = make_temp_file_path(file_name)
|
| 71 |
os.makedirs(os.path.dirname(out_path), exist_ok=True)
|
| 72 |
|
| 73 |
+
prog, _ = _wrap_progress(progress_cb, progress_args)
|
| 74 |
|
| 75 |
+
t0 = time.monotonic()
|
|
|
|
| 76 |
try:
|
| 77 |
+
# Hydrogram usually matches Pyrogram's signature:
|
| 78 |
+
# download_media(message, file_name=..., progress=..., progress_args=...)
|
| 79 |
await app.download_media(
|
| 80 |
message=m,
|
| 81 |
file_name=out_path,
|
| 82 |
+
progress=prog,
|
| 83 |
progress_args=progress_args,
|
| 84 |
)
|
| 85 |
except TypeError:
|
| 86 |
+
# Fallback if this Hydrogram build doesn't support progress args
|
| 87 |
await app.download_media(message=m, file_name=out_path)
|
| 88 |
+
t1 = time.monotonic()
|
| 89 |
|
| 90 |
+
if return_meta:
|
| 91 |
+
return out_path, file_size, file_name, (t1 - t0)
|
| 92 |
+
return out_path, file_size, file_name
|