warp-beacon 2.6.41__py3-none-any.whl → 2.6.43__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- __version__ = "2.6.41"
1
+ __version__ = "2.6.43"
2
2
 
@@ -8,13 +8,13 @@ import logging
8
8
  import html
9
9
 
10
10
  import uvloop
11
+ #from contextlib import ExitStack
11
12
 
12
13
  from pyrogram import Client, filters
13
14
  from pyrogram.enums import ParseMode, ChatType
14
15
  from pyrogram.handlers import MessageHandler, CallbackQueryHandler
15
16
  from pyrogram.types import InputMediaAudio, InputMediaPhoto, InputMediaVideo, InputMediaAnimation, InlineKeyboardButton, InlineKeyboardMarkup
16
17
  from pyrogram.errors import NetworkMigrate, BadRequest, MultiMediaTooLong, MessageIdInvalid
17
- from pyrogram.raw.types import InputFile, InputFileBig
18
18
 
19
19
  import warp_beacon
20
20
  from warp_beacon.__version__ import __version__
@@ -29,6 +29,7 @@ from warp_beacon.jobs import Origin
29
29
  from warp_beacon.telegram.utils import Utils
30
30
  from warp_beacon.telegram.caption_shortener import CaptionShortner
31
31
  from warp_beacon.scheduler.scheduler import IGScheduler
32
+ from warp_beacon.telegram.progress_file_reader import ProgressFileReader
32
33
 
33
34
  class Bot(object):
34
35
  should_exit = None
@@ -226,7 +227,7 @@ class Bot(object):
226
227
 
227
228
  return caption
228
229
 
229
- def build_tg_args(self, job: UploadJob, uploaded_files: list[Union[InputFile, InputFileBig]]) -> dict:
230
+ def build_tg_args(self, job: UploadJob, file_io: ProgressFileReader = None) -> dict:
230
231
  args = {}
231
232
  if job.media_type == JobType.VIDEO:
232
233
  if job.tg_file_id:
@@ -242,7 +243,7 @@ class Bot(object):
242
243
  else:
243
244
  if job.placeholder_message_id:
244
245
  args["media"] = InputMediaVideo(
245
- media=next(iter(uploaded_files)),
246
+ media=file_io,
246
247
  supports_streaming=True,
247
248
  width=job.media_info["width"],
248
249
  height=job.media_info["height"],
@@ -251,7 +252,7 @@ class Bot(object):
251
252
  caption=self.build_signature_caption(job)
252
253
  )
253
254
  else:
254
- args["video"] = next(iter(uploaded_files))
255
+ args["video"] = file_io
255
256
  args["supports_streaming"] = True
256
257
  args["width"] = job.media_info["width"]
257
258
  args["height"] = job.media_info["height"]
@@ -273,11 +274,11 @@ class Bot(object):
273
274
  else:
274
275
  if job.placeholder_message_id:
275
276
  args["media"] = InputMediaPhoto(
276
- media=next(iter(uploaded_files)),
277
+ media=file_io,
277
278
  caption=self.build_signature_caption(job)
278
279
  )
279
280
  else:
280
- args["photo"] = next(iter(uploaded_files))
281
+ args["photo"] = file_io
281
282
  args["caption"] = self.build_signature_caption(job)
282
283
 
283
284
  args["file_name"] = os.path.basename(job.local_media_path)
@@ -293,7 +294,7 @@ class Bot(object):
293
294
  else:
294
295
  if job.placeholder_message_id:
295
296
  args["media"] = InputMediaAudio(
296
- media=next(iter(uploaded_files)),
297
+ media=file_io,
297
298
  performer=job.media_info["performer"],
298
299
  thumb=job.media_info["thumb"],
299
300
  duration=round(job.media_info["duration"]),
@@ -301,7 +302,7 @@ class Bot(object):
301
302
  caption=self.build_signature_caption(job)
302
303
  )
303
304
  else:
304
- args["audio"] = next(iter(uploaded_files))
305
+ args["audio"] = file_io
305
306
  args["performer"] = job.media_info["performer"]
306
307
  args["thumb"] = job.media_info["thumb"]
307
308
  args["duration"] = round(job.media_info["duration"])
@@ -322,7 +323,7 @@ class Bot(object):
322
323
  else:
323
324
  if job.placeholder_message_id:
324
325
  args["media"] = InputMediaAnimation(
325
- media=next(iter(uploaded_files)),
326
+ media=file_io,
326
327
  thumb=job.media_info["thumb"],
327
328
  duration=round(job.media_info["duration"]),
328
329
  width=job.media_info["width"],
@@ -330,7 +331,7 @@ class Bot(object):
330
331
  caption=self.build_signature_caption(job)
331
332
  )
332
333
  else:
333
- args["animation"] = next(iter(uploaded_files))
334
+ args["animation"] = file_io
334
335
  args["width"] = job.media_info["width"]
335
336
  args["height"] = job.media_info["height"]
336
337
  args["duration"] = round(job.media_info["duration"])
@@ -358,27 +359,23 @@ class Bot(object):
358
359
  args["media"].append(tg_chunk)
359
360
  else:
360
361
  mediafs = []
361
- file_index = 0
362
362
  for chunk in job.media_collection:
363
363
  tg_chunk = []
364
- for el in chunk:
365
- uploaded_file = uploaded_files[file_index]
366
- file_index += 1
367
-
368
- if el.media_type == JobType.VIDEO:
364
+ for j in chunk:
365
+ if j.media_type == JobType.VIDEO:
369
366
  vid = InputMediaVideo(
370
- media=uploaded_file,
367
+ media=ProgressFileReader(j.local_media_path, self.progress_callback),
371
368
  supports_streaming=True,
372
- width=el.media_info["width"],
373
- height=el.media_info["height"],
374
- duration=round(el.media_info["duration"]),
375
- thumb=el.media_info["thumb"],
369
+ width=j.media_info["width"],
370
+ height=j.media_info["height"],
371
+ duration=round(j.media_info["duration"]),
372
+ thumb=j.media_info["thumb"],
376
373
  caption=self.build_signature_caption(job)
377
374
  )
378
375
  tg_chunk.append(vid)
379
- elif el.media_type == JobType.IMAGE:
376
+ elif j.media_type == JobType.IMAGE:
380
377
  photo = InputMediaPhoto(
381
- media=uploaded_file,
378
+ media=ProgressFileReader(j.local_media_path, self.progress_callback),
382
379
  caption=self.build_signature_caption(job)
383
380
  )
384
381
  tg_chunk.append(photo)
@@ -408,25 +405,8 @@ class Bot(object):
408
405
 
409
406
  return args
410
407
 
411
- async def upload_with_progress(self, job: UploadJob) -> list[Union[InputFile, InputFileBig]]:
412
- def progress_callback(current: int, total: int) -> None:
413
- logging.info("[%s] Uploaded %.1f%%", job.local_media_path, current * 100 / total)
414
-
415
- if job.media_type == JobType.COLLECTION:
416
- col_uploaded_files = []
417
- for chunk in job.media_collection:
418
- for col in chunk:
419
- uploaded_file = await self.client.save_file(
420
- path=col.local_media_path,
421
- progress=progress_callback
422
- )
423
- col_uploaded_files.append(uploaded_file)
424
- return col_uploaded_files
425
-
426
- return [await self.client.save_file(
427
- path=job.local_media_path,
428
- progress=progress_callback
429
- )]
408
+ def progress_callback(self, filename: str, current: int, total: int) -> None:
409
+ logging.info("[%s] Uploaded %.1f%%", filename, current * 100 / total)
430
410
 
431
411
  async def upload_job(self, job: UploadJob) -> list[str]:
432
412
  tg_file_ids = []
@@ -441,8 +421,8 @@ class Bot(object):
441
421
  await Utils.ensure_me_loaded(self.client)
442
422
  if job.placeholder_message_id:
443
423
  try:
444
- uploaded_files = await self.upload_with_progress(job)
445
- reply_message = await self.client.edit_message_media(**self.build_tg_args(job, uploaded_files))
424
+ with ProgressFileReader(job.local_media_path, self.progress_callback) as f:
425
+ reply_message = await self.client.edit_message_media(**self.build_tg_args(job, f))
446
426
  except MessageIdInvalid:
447
427
  logging.warning("Placeholder message not found. Looks like placeholder message was deleted by administrator.")
448
428
  job.placeholder_message_id = None
@@ -455,31 +435,41 @@ class Bot(object):
455
435
  JobType.ANIMATION: self.client.send_animation
456
436
  }
457
437
  try:
458
- uploaded_files = await self.upload_with_progress(job)
459
- reply_message = await send_funcs[job.media_type](**self.build_tg_args(job, uploaded_files))
438
+ with ProgressFileReader(job.local_media_path, self.progress_callback) as f:
439
+ reply_message = await send_funcs[job.media_type](**self.build_tg_args(job, f))
460
440
  except ValueError as e:
461
441
  err_text = str(e)
462
442
  if "Expected" in err_text:
463
443
  logging.warning("Expectations exceeded reality.")
464
444
  logging.warning(err_text)
465
445
  expectation, reality = Utils.parse_expected_patronum_error(err_text)
466
- job_args = self.build_tg_args(job, uploaded_files)
467
- job_args[reality.value.lower()] = job_args.pop(expectation.value.lower())
468
- reply_message = await send_funcs[reality](**job_args)
446
+ with ProgressFileReader(job.local_media_path, self.progress_callback) as f:
447
+ job_args = self.build_tg_args(job, f)
448
+ job_args[reality.value.lower()] = job_args.pop(expectation.value.lower())
449
+ reply_message = await send_funcs[reality](**job_args)
469
450
 
470
451
  tg_file_id = Utils.extract_file_id(reply_message)
471
452
  tg_file_ids.append(tg_file_id)
472
453
  job.tg_file_id = tg_file_id
473
454
  logging.info("Uploaded media file with type '%s' tg_file_id is '%s'", job.media_type.value, job.tg_file_id)
474
455
  elif job.media_type == JobType.COLLECTION:
475
- uploaded_files = await self.upload_with_progress(job)
476
- col_job_args = self.build_tg_args(job, uploaded_files)
456
+ #uploaded_files = await self.upload_with_progress(job)
457
+ col_job_args = self.build_tg_args(job)
477
458
  sent_messages = []
478
459
  snd_grp_options = {"chat_id": job.chat_id, "reply_to_message_id": job.message_id}
479
460
  for i, media_chunk in enumerate(col_job_args["media"]):
480
461
  snd_grp_options["media"] = media_chunk
481
- messages = await self.client.send_media_group(**snd_grp_options)
482
- sent_messages += messages
462
+ messages = None
463
+ try:
464
+ messages = await self.client.send_media_group(**snd_grp_options)
465
+ finally:
466
+ try:
467
+ for med in snd_grp_options["media"]:
468
+ med.media.close()
469
+ except AttributeError:
470
+ pass
471
+ if messages:
472
+ sent_messages += messages
483
473
  if job.media_collection:
484
474
  for j, _ in enumerate(media_chunk):
485
475
  tg_file_id = Utils.extract_file_id(messages[j])
@@ -0,0 +1,41 @@
1
+ import io
2
+ import os
3
+ from types import TracebackType
4
+ from typing import Optional, Callable, Type
5
+
6
+ class ProgressFileReader(io.BufferedReader):
7
+ callback = None
8
+ raw = None
9
+ total = 0
10
+ read_bytes = 0
11
+ name = ""
12
+
13
+ def __init__(self, file_path: str, callback: Optional[Callable[[str, int, int], None]]) -> None:
14
+ self.raw = open(file_path, "rb")
15
+ super().__init__(self.raw)
16
+ self.callback = callback
17
+ self.total = os.path.getsize(file_path)
18
+ self.read_bytes = 0
19
+ self.name = os.path.basename(file_path)
20
+
21
+ def read(self, size: int = -1) -> bytes:
22
+ chunk = super().read(size)
23
+ self.read_bytes += len(chunk)
24
+ if self.callback:
25
+ self.callback(self.name, self.read_bytes, self.total)
26
+ return chunk
27
+
28
+ def close(self) -> None:
29
+ if not self.closed:
30
+ super().close()
31
+ self.raw.close()
32
+
33
+ def __enter__(self) -> "ProgressFileReader":
34
+ return self
35
+
36
+ def __exit__(self,
37
+ exc_type: Optional[Type[BaseException]],
38
+ exc_val: Optional[BaseException],
39
+ exc_tb: Optional[TracebackType]
40
+ ) -> None:
41
+ self.close()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: warp_beacon
3
- Version: 2.6.41
3
+ Version: 2.6.43
4
4
  Summary: Telegram bot for expanding external media links
5
5
  Home-page: https://github.com/sb0y/warp_beacon
6
6
  Author: Andrey Bagrintsev
@@ -4,7 +4,7 @@ var/warp_beacon/accounts.json,sha256=OsXdncs6h88xrF_AP6_WDCK1waGBn9SR-uYdIeK37GM
4
4
  var/warp_beacon/placeholder.gif,sha256=cE5CGJVaop4Sx21zx6j4AyoHU0ncmvQuS2o6hJfEH88,6064
5
5
  var/warp_beacon/proxies.json,sha256=VnjlQDXumOEq72ZFjbh6IqHS1TEHqn8HPYAZqWCeSIA,95
6
6
  warp_beacon/__init__.py,sha256=_rThNODmz0nDp_n4mWo_HKaNFE5jk1_7cRhHyYaencI,163
7
- warp_beacon/__version__.py,sha256=1O5-3aiIS1oVfkcy9Ig1YYhWv1eOUzjN7A_m_n8lHac,24
7
+ warp_beacon/__version__.py,sha256=yVx5xRZvnIRE-vKVDTmNqs22CePf9NwiXuds1CWxoMM,24
8
8
  warp_beacon/warp_beacon.py,sha256=ED43vNzdjDUJ_9qLCbri0bjWLWEJ69BENGj9i7G6AvM,342
9
9
  warp_beacon/yt_auth.py,sha256=GUTKqYr_tzDC-07Lx_ahWXSag8EyLxXBUnQbDBIkEmk,6022
10
10
  warp_beacon/compress/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -40,15 +40,16 @@ warp_beacon/scraper/youtube/youtube.py,sha256=x9v9p1coA9TvBhxjNAofGu4UBkAEdYPE2e
40
40
  warp_beacon/storage/__init__.py,sha256=0Vajd0oITKJfu2vmNx5uQSt3-L6vwIvUYWJo8HZCjco,3398
41
41
  warp_beacon/storage/mongo.py,sha256=qC4ZiO8XXvPnP0rJwz4CJx42pqFsyAjCiW10W5QdT6E,527
42
42
  warp_beacon/telegram/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
43
- warp_beacon/telegram/bot.py,sha256=hc-2Mha5V2ma0YmvrtLcRx60hGaifP0O1AHK8p4RFTA,19931
43
+ warp_beacon/telegram/bot.py,sha256=NGakTkZr7CVaxhH77c0b_UssZtcDZIJd2Pcn0C8mARA,19576
44
44
  warp_beacon/telegram/caption_shortener.py,sha256=EnguNCF52ne7y4P-iJAbI6K3sqoJqJbND_dX5Fhwkv0,1549
45
45
  warp_beacon/telegram/handlers.py,sha256=uvR6TPHSqdSxigp3wR-ewiE6t3TvVcbVLVcYGwkgD2s,9559
46
46
  warp_beacon/telegram/placeholder_message.py,sha256=wN9-BRiyrtHG-EvXtZkGJHt2CX71munQ57ITttjt0mw,6400
47
+ warp_beacon/telegram/progress_file_reader.py,sha256=6DfFefaaxt0O9psoYejKJK8dss1t5HkNNVvK0S5hPnU,1010
47
48
  warp_beacon/telegram/utils.py,sha256=1Lq67aRylVJzbwSyvAgjPAGjJZFATkICvAj3TJGuJiM,4635
48
49
  warp_beacon/uploader/__init__.py,sha256=j3qcuKhpchseZLGzSsSiogqe6WdMbkK8d3I-ConhNRs,5687
49
- warp_beacon-2.6.41.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
50
- warp_beacon-2.6.41.dist-info/METADATA,sha256=gv1A6UMSV3WdhANJh3foIQmrZNtIPk47aDWIp9pShCg,22706
51
- warp_beacon-2.6.41.dist-info/WHEEL,sha256=GHB6lJx2juba1wDgXDNlMTyM13ckjBMKf-OnwgKOCtA,91
52
- warp_beacon-2.6.41.dist-info/entry_points.txt,sha256=eSB61Rb89d56WY0O-vEIQwkn18J-4CMrJcLA_R_8h3g,119
53
- warp_beacon-2.6.41.dist-info/top_level.txt,sha256=aFsWDQBplsMOyVMGGJ8iu-auZ25z1e_IB4tM2M8kW1A,1187
54
- warp_beacon-2.6.41.dist-info/RECORD,,
50
+ warp_beacon-2.6.43.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
51
+ warp_beacon-2.6.43.dist-info/METADATA,sha256=KpshBI-u9REF1iqT1LfLS__OqQGNd5N9ksVWeRohRjQ,22706
52
+ warp_beacon-2.6.43.dist-info/WHEEL,sha256=GHB6lJx2juba1wDgXDNlMTyM13ckjBMKf-OnwgKOCtA,91
53
+ warp_beacon-2.6.43.dist-info/entry_points.txt,sha256=eSB61Rb89d56WY0O-vEIQwkn18J-4CMrJcLA_R_8h3g,119
54
+ warp_beacon-2.6.43.dist-info/top_level.txt,sha256=aXHYe44ttXYi5p6_BGE-9t-RbPH6gLEJDH7gYoWamN4,1229
55
+ warp_beacon-2.6.43.dist-info/RECORD,,
@@ -36,6 +36,7 @@ warp_beacon/telegram/bot
36
36
  warp_beacon/telegram/caption_shortener
37
37
  warp_beacon/telegram/handlers
38
38
  warp_beacon/telegram/placeholder_message
39
+ warp_beacon/telegram/progress_file_reader
39
40
  warp_beacon/telegram/utils
40
41
  warp_beacon/uploader
41
42
  warp_beacon/warp_beacon