telegram: Add compact message IDs to filenames of media,
patch 59776df1810cf643a8b7d70ab0502afde6a5c47f
Author: E. Bosch <presidev@AT@gmail.com>
Date: Sun Dec 17 02:49:18 CET 2023
* telegram: Add compact message IDs to filenames of media,
this will prevent most of the possible collisions of media with the same filename
diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py
--- old-irgramd/telegram.py 2024-11-22 10:09:38.380200674 +0100
+++ new-irgramd/telegram.py 2024-11-22 10:09:38.380200674 +0100
@@ -22,7 +22,7 @@
from include import CHAN_MAX_LENGHT, NICK_MAX_LENGTH
from irc import IRCUser
-from utils import sanitize_filename, is_url_equiv, extract_url, get_human_size, get_human_duration, get_highlighted, fix_braces, format_timestamp
+from utils import sanitize_filename, add_filename, is_url_equiv, extract_url, get_human_size, get_human_duration, get_highlighted, fix_braces, format_timestamp
import emoji2emoticon as e
# Test IP table
@@ -569,14 +569,14 @@
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)
+ text = await self.handle_webpage(upd_to_webpend, message, mid)
elif message.media:
text = await self.handle_telegram_media(message, user, mid)
else:
text = message.message
if message.action:
- final_text = await self.handle_telegram_action(message)
+ final_text = await self.handle_telegram_action(message, mid)
return final_text
elif message.is_reply:
refwd_text = await self.handle_telegram_reply(message)
@@ -669,14 +669,14 @@
self.irc.iid_to_tid[channel] = chat.id
await self.irc.join_irc_channel(self.irc.irc_nick, channel, full_join=True)
- async def handle_telegram_action(self, message):
+ async def handle_telegram_action(self, message, mid):
if isinstance(message.action, tgty.MessageActionPinMessage):
replied = await message.get_reply_message()
cid = self.mid.num_to_id_offset(replied.peer_id, replied.id)
action_text = 'has pinned message [{}]'.format(cid)
elif isinstance(message.action, tgty.MessageActionChatEditPhoto):
_, media_type = self.scan_photo_attributes(message.action.photo)
- photo_url = await self.download_telegram_media(message)
+ photo_url = await self.download_telegram_media(message, mid)
action_text = 'has changed chat [{}] {}'.format(media_type, photo_url)
else:
action_text = ''
@@ -744,7 +744,7 @@
to_download = False
if isinstance(message.media.webpage, tgty.WebPage):
# web
- return await self.handle_webpage(message.media.webpage, message)
+ return await self.handle_webpage(message.media.webpage, message, mid)
elif isinstance(message.media.webpage, tgty.WebPagePending):
media_type = 'webpending'
media_url_or_data = message.message
@@ -831,7 +831,7 @@
if to_download:
relay_attr = (message, user, mid, media_type)
- media_url_or_data = await self.download_telegram_media(message, filename, size, relay_attr)
+ media_url_or_data = await self.download_telegram_media(message, mid, filename, size, relay_attr)
return self.format_media(media_type, media_url_or_data, caption)
@@ -854,9 +854,9 @@
target_mine = ''
return target_mine
- async def handle_webpage(self, webpage, message):
+ async def handle_webpage(self, webpage, message, mid):
media_type = 'web'
- logo = await self.download_telegram_media(message)
+ logo = await self.download_telegram_media(message, mid)
if is_url_equiv(webpage.url, webpage.display_url):
url_data = webpage.url
else:
@@ -914,11 +914,12 @@
return size, media_type
- async def download_telegram_media(self, message, filename=None, size=0, relay_attr=None):
+ async def download_telegram_media(self, message, mid, filename=None, size=0, relay_attr=None):
if not self.download:
return ''
if filename:
- new_file = sanitize_filename(filename)
+ idd_file = add_filename(filename, mid)
+ new_file = sanitize_filename(idd_file)
new_path = os.path.join(self.telegram_media_dir, new_file)
if os.path.exists(new_path):
local_path = new_path
@@ -931,7 +932,9 @@
local_path = await message.download_media(self.telegram_media_dir)
if not local_path: return ''
filetype = os.path.splitext(local_path)[1]
- new_file = str(self.media_cn) + filetype
+ gen_file = str(self.media_cn) + filetype
+ idd_file = add_filename(gen_file, mid)
+ new_file = sanitize_filename(idd_file)
self.media_cn += 1
new_path = os.path.join(self.telegram_media_dir, new_file)
diff -rN -u old-irgramd/utils.py new-irgramd/utils.py
--- old-irgramd/utils.py 2024-11-22 10:09:38.380200674 +0100
+++ new-irgramd/utils.py 2024-11-22 10:09:38.384200667 +0100
@@ -83,6 +83,15 @@
def sanitize_filename(fn):
return FILENAME_INVALID_CHARS.sub('', fn).strip('-').replace(' ','_')
+def add_filename(filename, add):
+ if add:
+ aux = filename.rsplit('.', 1)
+ name = aux[0]
+ ext = aux[1]
+ return '{}-{}.{}'.format(name, add, ext)
+ else:
+ return filename
+
def remove_slash(url):
return url[:-1] if url[-1:] == '/' else url