From bbb1dc98d2f10d7b2bb1a35746623c4bd48fc851 Mon Sep 17 00:00:00 2001 From: stickman33 Date: Wed, 25 Sep 2024 19:59:04 +0300 Subject: [PATCH 1/2] add video files support --- telegram_to_rss/generate_feed.py | 26 +++++++++++++++++-------- telegram_to_rss/poll_telegram.py | 33 ++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/telegram_to_rss/generate_feed.py b/telegram_to_rss/generate_feed.py index fa4dbbc..7783315 100644 --- a/telegram_to_rss/generate_feed.py +++ b/telegram_to_rss/generate_feed.py @@ -49,25 +49,36 @@ def generate_feed(feed_render_dir: Path, feed: Feed): title = message_text[:100] ET.SubElement(rss_item_el, "title").text = title - images = "" + media_content = "" media_download_failure = False + + # processing mediafiles for media_path in feed_entry.media: if media_path == "FAIL": media_download_failure = True else: media_url = "{}/static/{}".format(base_url, media_path) - images += '
'.format(media_url) - content = feed_entry.message.replace("\n", "
") + images + + # checking file type + if media_path.endswith(('.jpg', '.png', '.gif')): + media_content += '
media'.format(media_url) + elif media_path.endswith('.mp4'): + media_content += ( + '
' + ).format(media_url, media_url) + + # creating feed with text and media + content = feed_entry.message.replace("\n", "
") + media_content if feed_entry.has_unsupported_media: content += "
This message has unsupported attachment. Open Telegram to view it." if media_download_failure: content += "
Downloading some of the media for this message failed. Open Telegram to view it." - ET.SubElement(rss_item_el, "description").text = content + ET.SubElement(rss_item_el, "description").text = content ET.SubElement(rss_item_el, "pubDate").text = feed_entry.date.isoformat() - ET.SubElement(rss_item_el, "link", {"href": feed_entry_url}).text = ( - feed_entry_url - ) + ET.SubElement(rss_item_el, "link", {"href": feed_entry_url}).text = feed_entry_url final_feed_file = feed_render_dir.joinpath("{}.xml".format(feed.id)) @@ -78,7 +89,6 @@ def generate_feed(feed_render_dir: Path, feed: Feed): logging.info("generate_feed -> done %s %s", feed.name, feed.id) - async def update_feeds_cache(feed_render_dir: str): feeds = await Feed.all().prefetch_related( Prefetch("entries", queryset=FeedEntry.all().order_by("-date")) diff --git a/telegram_to_rss/poll_telegram.py b/telegram_to_rss/poll_telegram.py index 345677e..6a09c53 100644 --- a/telegram_to_rss/poll_telegram.py +++ b/telegram_to_rss/poll_telegram.py @@ -4,6 +4,7 @@ from tortoise.transactions import atomic from pathlib import Path import logging +import pprint class TelegramPoller: @@ -178,7 +179,36 @@ def progress_callback(current, total, media_path=media_path): f"TelegramPoller._process_new_dialog_messages -> downloading media failed with {e} for message {dialog_message.id} {dialog_message.date} {dialog_message.text}", ) last_processed_message.downloaded_media.append("FAIL") + elif isinstance(dialog_message.media, types.MessageMediaDocument): + document = dialog_message.media.document + mime_type = getattr(document, 'mime_type', None) + if mime_type and mime_type.startswith("video/"): + try: + feed_entry_media_id = "{}-{}".format( + to_feed_entry_id(feed, dialog_message), + len(last_processed_message.downloaded_media), + ) + media_path = self._static_path.joinpath(feed_entry_media_id) + + def progress_callback(current, total, media_path=media_path): + logging.debug( + "TelegramPoller._process_new_dialog_messages -> downloading video %s: %s out of %s", + media_path, + current, + total, + ) + + res_path = await dialog_message.download_media( + file=media_path, progress_callback=progress_callback + ) + last_processed_message.downloaded_media.append(Path(res_path).name) + except Exception as e: + logging.warning( + f"TelegramPoller._process_new_dialog_messages -> downloading video failed with {e} for message {dialog_message.id} {dialog_message.date} {dialog_message.text}", + ) + last_processed_message.downloaded_media.append("FAIL") + # creating FeedEntry with mediafiles feed_entries: list[FeedEntry] = [] for dialog_message in filtered_dialog_messages: feed_entry_id = to_feed_entry_id(feed, dialog_message) @@ -190,10 +220,9 @@ def progress_callback(current, total, media_path=media_path): date=dialog_message.date, media=dialog_message.downloaded_media, has_unsupported_media=dialog_message.media is not None - and not isinstance(dialog_message.media, types.MessageMediaPhoto), + and not isinstance(dialog_message.media, (types.MessageMediaPhoto, types.MessageMediaDocument)), ) ) - return feed_entries From 24ff9e943ab4e3fe5e3f99ebe75e9f8b5e75cbee Mon Sep 17 00:00:00 2001 From: stickman33 Date: Wed, 25 Sep 2024 19:59:56 +0300 Subject: [PATCH 2/2] remove unused import --- telegram_to_rss/poll_telegram.py | 1 - 1 file changed, 1 deletion(-) diff --git a/telegram_to_rss/poll_telegram.py b/telegram_to_rss/poll_telegram.py index 6a09c53..5aacc71 100644 --- a/telegram_to_rss/poll_telegram.py +++ b/telegram_to_rss/poll_telegram.py @@ -4,7 +4,6 @@ from tortoise.transactions import atomic from pathlib import Path import logging -import pprint class TelegramPoller: