Refactor RSS feed generation and post parsing to improve content handling

This commit is contained in:
vvzvlad
2025-02-03 18:42:27 +03:00
parent e4f3b5cede
commit a4f1ffd7c9
2 changed files with 17 additions and 4 deletions
+13 -1
View File
@@ -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 = '<div class="message-service">Channel created</div>'
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', '<br>')
@@ -263,6 +273,7 @@ class PostParser:
def _format_reactions_and_views(self, message: Message) -> Union[str, None]:
try:
html_parts = []
html_parts.append("<br>")
if reactions := getattr(message, "reactions", None):
reactions_html = ''
@@ -274,7 +285,8 @@ class PostParser:
views_html = f'<span class="views">(Views: {views})</span>'
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)}")
+4 -3
View File
@@ -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"<![CDATA[{html_content}]]>")
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"<p>The Telegram channel @{channel} does not exist or is not accessible.</p>"
fe.description(f"<![CDATA[{error_html}]]>")
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)