Spaces:
Runtime error
Runtime error
Update bot/telegram/media.py
Browse files- bot/telegram/media.py +28 -9
bot/telegram/media.py
CHANGED
|
@@ -1,19 +1,38 @@
|
|
| 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 |
media = m.video or m.document
|
| 9 |
if not media:
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
return out_path, file_size, file_name
|
|
|
|
| 1 |
# PATH: bot/telegram/media.py
|
| 2 |
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
from typing import Callable, Awaitable, Optional
|
| 5 |
+
|
| 6 |
from hydrogram import Client
|
| 7 |
from hydrogram.types import Message
|
|
|
|
| 8 |
|
| 9 |
+
ProgressCB = Callable[[int, int], Awaitable[None]]
|
| 10 |
+
|
| 11 |
+
async def download_to_temp(app: Client, m: Message, progress_cb: Optional[ProgressCB] = None):
|
| 12 |
media = m.video or m.document
|
| 13 |
if not media:
|
| 14 |
+
return "", 0, ""
|
| 15 |
+
|
| 16 |
+
file_name = getattr(media, "file_name", None) or "video.mp4"
|
| 17 |
+
suffix = "." + file_name.rsplit(".", 1)[1] if "." in file_name else ""
|
| 18 |
+
|
| 19 |
+
fd, path = tempfile.mkstemp(prefix="studytube_", suffix=suffix)
|
| 20 |
+
os.close(fd)
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
await app.download_media(message=m, file_name=path, progress=progress_cb)
|
| 24 |
+
except TypeError:
|
| 25 |
+
await app.download_media(message=m, file_name=path)
|
| 26 |
+
|
| 27 |
+
if not os.path.exists(path):
|
| 28 |
+
return "", 0, file_name
|
| 29 |
|
| 30 |
+
size = os.path.getsize(path)
|
| 31 |
+
expected = int(getattr(media, "file_size", 0) or 0)
|
| 32 |
|
| 33 |
+
if expected and size and expected != size:
|
| 34 |
+
try: os.remove(path)
|
| 35 |
+
except: pass
|
| 36 |
+
raise RuntimeError(f"download_size_mismatch expected={expected} got={size}")
|
| 37 |
|
| 38 |
+
return path, size, file_name
|
|
|