# coding=utf-8 """ RSS HTML 报告渲染模块 提供 RSS 订阅内容的 HTML 格式报告生成功能 """ from datetime import datetime from typing import Dict, List, Optional, Callable from trendradar.report.helpers import html_escape def render_rss_html_content( rss_items: List[Dict], total_count: int, feeds_info: Optional[Dict[str, str]] = None, *, get_time_func: Optional[Callable[[], datetime]] = None, ) -> str: """渲染 RSS HTML 内容 Args: rss_items: RSS 条目列表,每个条目包含: - title: 标题 - feed_id: RSS 源 ID - feed_name: RSS 源名称 - url: 链接 - published_at: 发布时间 - summary: 摘要(可选) - author: 作者(可选) total_count: 条目总数 feeds_info: RSS 源 ID 到名称的映射 get_time_func: 获取当前时间的函数(可选,默认使用 datetime.now) Returns: 渲染后的 HTML 字符串 """ html = """ RSS 订阅内容
RSS 订阅内容
订阅条目 """ html += f"{total_count} 条" html += """
生成时间 """ # 使用提供的时间函数或默认 datetime.now if get_time_func: now = get_time_func() else: now = datetime.now() html += now.strftime("%m-%d %H:%M") html += """
""" # 按 feed_id 分组 feeds_map: Dict[str, List[Dict]] = {} for item in rss_items: feed_id = item.get("feed_id", "unknown") if feed_id not in feeds_map: feeds_map[feed_id] = [] feeds_map[feed_id].append(item) # 渲染每个 RSS 源的内容 for feed_id, items in feeds_map.items(): feed_name = items[0].get("feed_name", feed_id) if items else feed_id if feeds_info and feed_id in feeds_info: feed_name = feeds_info[feed_id] escaped_feed_name = html_escape(feed_name) html += f"""
{escaped_feed_name}
{len(items)} 条
""" for item in items: raw_title = item.get("title", "") if not raw_title or not raw_title.strip(): raw_title = item.get("url", "") or item.get("feed_name", "") escaped_title = html_escape(raw_title) url = item.get("url", "") published_at = item.get("published_at", "") author = item.get("author", "") summary = item.get("summary", "") html += """
""" if published_at: html += f'{html_escape(published_at)}' if author: html += f'by {html_escape(author)}' html += """
""" if url: escaped_url = html_escape(url) html += f'{escaped_title}' else: html += escaped_title html += """
""" if summary: escaped_summary = html_escape(summary) html += f"""

{escaped_summary}

""" html += """
""" html += """
""" html += """
""" return html