understanding commited on
Commit
53f81dc
·
verified ·
1 Parent(s): b9a2be1

Update bot/telegram/media.py

Browse files
Files changed (1) hide show
  1. bot/telegram/media.py +13 -67
bot/telegram/media.py CHANGED
@@ -1,64 +1,21 @@
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,23 +27,12 @@ async def download_to_temp(
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
 
1
  # PATH: bot/telegram/media.py
2
  import os
 
 
 
3
  from typing import Callable, Any, Optional, Tuple
 
4
  from hydrogram import Client
5
  from hydrogram.types import Message
 
6
  from bot.temp.files import make_temp_file_path
7
 
8
+ ProgressCb = Optional[Callable[[int, int], Any]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  async def download_to_temp(
11
  app: Client,
12
  m: Message,
13
+ progress: ProgressCb = None,
14
+ progress_args: tuple = (),
15
+ **_ignore, # <-- future-proof: ignores unknown kwargs safely
16
+ ) -> Tuple[str, int, str]:
17
  """
18
+ Returns: (file_path, file_size, file_name)
 
 
 
 
 
 
19
  """
20
  media = m.video or m.document
21
  if not media:
 
27
  out_path = make_temp_file_path(file_name)
28
  os.makedirs(os.path.dirname(out_path), exist_ok=True)
29
 
30
+ # Hydrogram (pyrogram-like) supports progress/progress_args
31
+ await app.download_media(
32
+ message=m,
33
+ file_name=out_path,
34
+ progress=progress,
35
+ progress_args=progress_args,
36
+ )
 
 
 
 
 
 
 
 
 
37
 
 
 
38
  return out_path, file_size, file_name