telegram: Add option "download_notice" to show the start of a big download
patch 24a50b886f4cef2e1da003b74921982f5da0941c
Author: E. Bosch <presidev@AT@gmail.com>
Date: Mon Oct 16 00:24:53 CEST 2023
* telegram: Add option "download_notice" to show the start of a big download
diff -rN -u old-irgramd/irgramd new-irgramd/irgramd
--- old-irgramd/irgramd 2024-11-22 11:11:29.910258281 +0100
+++ new-irgramd/irgramd 2024-11-22 11:11:29.910258281 +0100
@@ -74,6 +74,7 @@
tornado.options.define('config', default='irgramdrc', metavar='CONFIGFILE', help='Config file absolute or relative to `config_dir` (command line options override it)')
tornado.options.define('config_dir', default='~/.config/irgramd', metavar='PATH', help='Configuration directory where telegram session info is saved')
tornado.options.define('download_media', default=True, help='Enable download of any media (photos, documents, etc.), if not set only a message of media will be shown')
+ tornado.options.define('download_notice', default=10, metavar='SIZE (MiB)', help='Enable a notice when a download starts if its size is greater than SIZE, this is useful when a download takes some time to be completed')
tornado.options.define('emoji_ascii', default=False, help='Replace emoji with ASCII emoticons')
tornado.options.define('geo_url', type=str, default=None, metavar='TEMPLATE_URL', help='Use custom URL for showing geo latitude/longitude location, eg. OpenStreetMap')
tornado.options.define('hist_timestamp_format', metavar='DATETIME_FORMAT', help='Format string for timestamps in history, see https://www.strfti.me')
diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py
--- old-irgramd/telegram.py 2024-11-22 11:11:29.910258281 +0100
+++ new-irgramd/telegram.py 2024-11-22 11:11:29.914258275 +0100
@@ -39,6 +39,7 @@
self.logger = logging.getLogger()
self.config_dir = settings['config_dir']
self.download = settings['download_media']
+ self.notice_size = settings['download_notice'] * 1048576
self.media_dir = settings['media_dir']
self.media_url = settings['media_url']
self.api_id = settings['api_id']
@@ -551,7 +552,7 @@
user = self.get_irc_user_from_telegram(msg.sender_id)
mid = self.mid.num_to_id_offset(msg.peer_id, msg.id)
- text = await self.render_text(msg, mid, upd_to_webpend)
+ text = await self.render_text(msg, mid, upd_to_webpend, user)
text_send = self.set_history_timestamp(text, history, msg.date)
chan = await self.relay_telegram_message(msg, user, text_send)
@@ -559,11 +560,11 @@
self.refwd_me = False
- async def render_text(self, message, mid, upd_to_webpend, history=False):
+ async def render_text(self, message, mid, upd_to_webpend, user=None):
if upd_to_webpend:
text = await self.handle_webpage(upd_to_webpend, message)
elif message.media:
- text = await self.handle_telegram_media(message)
+ text = await self.handle_telegram_media(message, user, mid)
else:
text = message.message
@@ -682,10 +683,11 @@
return '|Fwd{}{}{}{}| '.format(space, forwarded_peer_name, space2, secondary_name)
- async def handle_telegram_media(self, message):
+ async def handle_telegram_media(self, message, user, mid):
caption = ' | {}'.format(message.message) if message.message else ''
to_download = True
media_url_or_data = ''
+ size = 0
if isinstance(message.media, tgty.MessageMediaWebPage):
to_download = False
@@ -702,24 +704,30 @@
media_url_or_data = message.message
caption = ''
elif message.photo:
- size = message.media.photo.sizes[-1]
- if hasattr(size, 'w') and hasattr(size, 'h'):
- media_type = 'photo:{}x{}'.format(size.w, size.h)
+ ph_size = message.media.photo.sizes[-1]
+ if isinstance(ph_size, tgty.PhotoSizeProgressive):
+ size = ph_size.sizes[-1]
+ else:
+ size = ph_size.size
+ if hasattr(ph_size, 'w') and hasattr(ph_size, 'h'):
+ media_type = 'photo:{}x{}'.format(ph_size.w, ph_size.h)
else:
media_type = 'photo'
elif message.audio: media_type = 'audio'
elif message.voice: media_type = 'rec'
elif message.video:
- size = get_human_size(message.media.document.size)
+ size = message.media.document.size
+ h_size = get_human_size(size)
attrib = next(x for x in message.media.document.attributes if isinstance(x, tgty.DocumentAttributeVideo))
dur = get_human_duration(attrib.duration)
- media_type = 'video:{},{}'.format(size, dur)
+ media_type = 'video:{},{}'.format(h_size, dur)
elif message.video_note: media_type = 'videorec'
elif message.gif: media_type = 'anim'
elif message.sticker: media_type = 'sticker'
elif message.document:
- size = get_human_size(message.media.document.size)
- media_type = 'file:{}'.format(size)
+ size = message.media.document.size
+ h_size = get_human_size(size)
+ media_type = 'file:{}'.format(h_size)
elif message.contact:
media_type = 'contact'
caption = ''
@@ -773,6 +781,9 @@
media_url_or_data = message.message
if to_download:
+ if self.download and size > self.notice_size:
+ await self.relay_telegram_message(message, user, '[{}] [{}] [Downloading]'.format(mid, media_type))
+
media_url_or_data = await self.download_telegram_media(message)
return self.format_media(media_type, media_url_or_data, caption)