patch 0015ec4a3cf416b0e597b5f97681b4f443977e37
Author: E. Bosch <presidev@AT@gmail.com>
Date: Tue Feb 8 02:04:55 CET 2022
* telegram: Improve webpage (media) handler
Add suport for webpage update after webpending
hunk ./telegram.py 13
-from utils import sanitize_filename
+from utils import sanitize_filename, remove_slash, remove_http_s
hunk ./telegram.py 42
+ self.webpending = {}
hunk ./telegram.py 67
+ (self.handle_raw, telethon.events.Raw),
hunk ./telegram.py 246
- async def handle_telegram_message(self, event):
+ async def handle_raw(self, update):
+ if isinstance(update, tgty.UpdateWebPage) and isinstance(update.webpage, tgty.WebPage):
+ event = self.webpending.pop(update.webpage.id, None)
+ if event:
+ await self.handle_telegram_message(event, update.webpage)
+
+ async def handle_telegram_message(self, event, upd_to_webpend=None):
hunk ./telegram.py 261
- if event.message.media:
- text = await self.handle_telegram_media(event.message)
+ if upd_to_webpend:
+ text = await self.handle_webpage(upd_to_webpend, event.message)
+ elif event.message.media:
+ text = await self.handle_telegram_media(event)
hunk ./telegram.py 320
- async def handle_telegram_media(self, message):
+ async def handle_telegram_media(self, event):
+ message = event.message
hunk ./telegram.py 326
- if message.web_preview:
- media_type = 'web'
- logo = await self.download_telegram_media(message)
+ if isinstance(message.media, tgty.MessageMediaWebPage):
hunk ./telegram.py 328
- media_url_or_data = message.message
- if message.media.webpage.title and logo:
- caption = ' | {} | {}'.format(message.media.webpage.title, logo)
- elif message.media.webpage.title:
- caption = ' | {}'.format(message.media.webpage.title)
- elif logo:
- caption = ' | {}'.format(logo)
+ if isinstance(message.media.webpage, tgty.WebPage):
+ # web
+ return await self.handle_webpage(message.media.webpage, message)
+ elif isinstance(message.media.webpage, tgty.WebPagePending):
+ media_type = 'webpending'
+ media_url_or_data = message.message
+ caption = ''
+ self.webpending[message.media.webpage.id] = event
hunk ./telegram.py 337
+ media_type = 'webunknown'
+ media_url_or_data = message.message
hunk ./telegram.py 340
-
hunk ./telegram.py 395
+ else:
+ media_type = 'unknown'
+ caption = ''
+ to_download = False
+ media_url_or_data = message.message
hunk ./telegram.py 404
+ return self.format_media(media_type, media_url_or_data, caption)
+
+ async def handle_webpage(self, webpage, message):
+ media_type = 'web'
+ logo = await self.download_telegram_media(message)
+ if webpage.url != webpage.display_url \
+ and remove_slash(webpage.url) != webpage.display_url \
+ and remove_http_s(webpage.url) != webpage.display_url:
+ media_url_or_data = '{} | {}'.format(webpage.url, webpage.display_url)
+ else:
+ media_url_or_data = webpage.url
+ if message:
+ # sometimes the 1st line of message contains the title, don't repeat it
+ message_line = message.message.splitlines()[0]
+ if message_line != webpage.title:
+ title = webpage.title
+ else:
+ title = ''
+ else:
+ title = webpage.title
+
+ if title and logo:
+ caption = ' | {} | {}'.format(title, logo)
+ elif title:
+ caption = ' | {}'.format(title)
+ elif logo:
+ caption = ' | {}'.format(logo)
+ else:
+ caption = ''
+
+ return self.format_media(media_type, media_url_or_data, caption)
+
+ def format_media(self, media_type, media_url_or_data, caption):
hunk ./utils.py 48
+
+def remove_slash(url):
+ return url[:-1] if url[-1:] == '/' else url
+
+def remove_http_s(url):
+ if url[:8] == 'https://':
+ surl = url[8:]
+ elif url[:7] == 'http://':
+ surl = url[7:]
+ else:
+ surl = url
+ return remove_slash(surl)