understanding commited on
Commit
b9a2be1
·
verified ·
1 Parent(s): 5f5f7a2

Update bot/telegram/media.py

Browse files
Files changed (1) hide show
  1. 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
- 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,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
- 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
 
 
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