From a4f1ffd7c9a6260fa3173e69f001444ef8e98cb6 Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Mon, 3 Feb 2025 18:42:27 +0300 Subject: [PATCH] Refactor RSS feed generation and post parsing to improve content handling --- post_parser.py | 14 +++++++++++++- rss_generator.py | 7 ++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/post_parser.py b/post_parser.py index 7e0e50e..0b9e16b 100644 --- a/post_parser.py +++ b/post_parser.py @@ -104,6 +104,9 @@ class PostParser: return "Unknown author" def _generate_title(self, message: Message) -> str: + # Check for "channel created" service message + if getattr(message, "channel_chat_created", False): + return "✨ Channel created" text = message.text or message.caption or '' if not text: if message.media == MessageMediaType.PHOTO: return "📷 Photo" @@ -139,6 +142,13 @@ class PostParser: return f"{trimmed.strip()}..." if trimmed else "" def _format_html(self, message: Message, naked: bool = False) -> str: + # Check for "channel created" service message + if getattr(message, "channel_chat_created", False): + service_html = '
Channel created
' + if not naked: + return self._wrap_html([service_html]) + return service_html + text = message.text.html if message.text else message.caption.html if message.caption else '' text = text.replace('\n', '
') @@ -263,6 +273,7 @@ class PostParser: def _format_reactions_and_views(self, message: Message) -> Union[str, None]: try: html_parts = [] + html_parts.append("
") if reactions := getattr(message, "reactions", None): reactions_html = '' @@ -274,7 +285,8 @@ class PostParser: views_html = f'(Views: {views})' html_parts.append(views_html) - return ' '.join(html_parts) if html_parts else None + html = ' '.join(html_parts) if html_parts else None + return html except Exception as e: logger.error(f"reactions_views_parsing_error: {str(e)}") diff --git a/rss_generator.py b/rss_generator.py index e260d56..8088c0d 100644 --- a/rss_generator.py +++ b/rss_generator.py @@ -95,7 +95,7 @@ async def generate_channel_rss(channel: str, post_parser: Optional[PostParser] = for post in group_posts: current_title = post.get('title', '') if current_title and current_title not in ['📷 Photo', '📹 Video', '📄 Document']: - main_post = post + main_post = description break merged_post = main_post.copy() @@ -119,7 +119,8 @@ async def generate_channel_rss(channel: str, post_parser: Optional[PostParser] = fe.link(href=post_link) html_content = post.get('html', '') - fe.description(f"") + text_content = post.get('text', '') + fe.description(f"{text_content}") fe.content(content=html_content, type='CDATA') pub_date = datetime.fromtimestamp(post['date'], tz=timezone.utc) @@ -160,7 +161,7 @@ def create_error_feed(channel: str, base_url: str) -> str: fe.title("Channel not found") fe.link(href=f"https://t.me/{channel}") error_html = f"

The Telegram channel @{channel} does not exist or is not accessible.

" - fe.description(f"") + fe.description(f"{error_html}") fe.content(content=error_html, type='CDATA') fe.pubDate(datetime.now(tz=timezone.utc)) fe.guid(f"https://t.me/{channel}", permalink=True)