renderer.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. # coding=utf-8
  2. """
  3. 通知内容渲染模块
  4. 提供多平台通知内容渲染功能,生成格式化的推送消息
  5. """
  6. from datetime import datetime
  7. from typing import Dict, List, Optional, Callable
  8. from trendradar.report.formatter import format_title_for_platform
  9. # 默认区域顺序
  10. DEFAULT_REGION_ORDER = ["hotlist", "rss", "new_items", "standalone", "ai_analysis"]
  11. def render_feishu_content(
  12. report_data: Dict,
  13. update_info: Optional[Dict] = None,
  14. mode: str = "daily",
  15. separator: str = "---",
  16. region_order: Optional[List[str]] = None,
  17. get_time_func: Optional[Callable[[], datetime]] = None,
  18. rss_items: Optional[list] = None,
  19. show_new_section: bool = True,
  20. ) -> str:
  21. """渲染飞书通知内容(支持热榜+RSS合并)
  22. Args:
  23. report_data: 报告数据字典,包含 stats, new_titles, failed_ids, total_new_count
  24. update_info: 版本更新信息(可选)
  25. mode: 报告模式 ("daily", "incremental", "current")
  26. separator: 内容分隔符
  27. region_order: 区域显示顺序列表
  28. get_time_func: 获取当前时间的函数(可选,默认使用 datetime.now())
  29. rss_items: RSS 条目列表(可选,用于合并推送)
  30. show_new_section: 是否显示新增热点区域
  31. Returns:
  32. 格式化的飞书消息内容
  33. """
  34. if region_order is None:
  35. region_order = DEFAULT_REGION_ORDER
  36. # 生成热点词汇统计部分
  37. stats_content = ""
  38. if report_data["stats"]:
  39. stats_content += "📊 **热点词汇统计**\n\n"
  40. total_count = len(report_data["stats"])
  41. for i, stat in enumerate(report_data["stats"]):
  42. word = stat["word"]
  43. count = stat["count"]
  44. sequence_display = f"<font color='grey'>[{i + 1}/{total_count}]</font>"
  45. if count >= 10:
  46. stats_content += f"🔥 {sequence_display} **{word}** : <font color='red'>{count}</font> 条\n\n"
  47. elif count >= 5:
  48. stats_content += f"📈 {sequence_display} **{word}** : <font color='orange'>{count}</font> 条\n\n"
  49. else:
  50. stats_content += f"📌 {sequence_display} **{word}** : {count} 条\n\n"
  51. for j, title_data in enumerate(stat["titles"], 1):
  52. formatted_title = format_title_for_platform(
  53. "feishu", title_data, show_source=True
  54. )
  55. stats_content += f" {j}. {formatted_title}\n"
  56. if j < len(stat["titles"]):
  57. stats_content += "\n"
  58. if i < len(report_data["stats"]) - 1:
  59. stats_content += f"\n{separator}\n\n"
  60. # 生成新增新闻部分
  61. new_titles_content = ""
  62. if show_new_section and report_data["new_titles"]:
  63. new_titles_content += (
  64. f"🆕 **本次新增热点新闻** (共 {report_data['total_new_count']} 条)\n\n"
  65. )
  66. for source_data in report_data["new_titles"]:
  67. new_titles_content += (
  68. f"**{source_data['source_name']}** ({len(source_data['titles'])} 条):\n"
  69. )
  70. for j, title_data in enumerate(source_data["titles"], 1):
  71. title_data_copy = title_data.copy()
  72. title_data_copy["is_new"] = False
  73. formatted_title = format_title_for_platform(
  74. "feishu", title_data_copy, show_source=False
  75. )
  76. new_titles_content += f" {j}. {formatted_title}\n"
  77. new_titles_content += "\n"
  78. # RSS 内容
  79. rss_content = ""
  80. if rss_items:
  81. rss_content = _render_rss_section_feishu(rss_items, separator)
  82. # 准备各区域内容映射
  83. region_contents = {
  84. "hotlist": stats_content,
  85. "new_items": new_titles_content,
  86. "rss": rss_content,
  87. }
  88. # 按 region_order 顺序组装内容
  89. text_content = ""
  90. for region in region_order:
  91. content = region_contents.get(region, "")
  92. if content:
  93. if text_content:
  94. text_content += f"\n{separator}\n\n"
  95. text_content += content
  96. if not text_content:
  97. if mode == "incremental":
  98. mode_text = "增量模式下暂无新增匹配的热点词汇"
  99. elif mode == "current":
  100. mode_text = "当前榜单模式下暂无匹配的热点词汇"
  101. else:
  102. mode_text = "暂无匹配的热点词汇"
  103. text_content = f"📭 {mode_text}\n\n"
  104. if report_data["failed_ids"]:
  105. if text_content and "暂无匹配" not in text_content:
  106. text_content += f"\n{separator}\n\n"
  107. text_content += "⚠️ **数据获取失败的平台:**\n\n"
  108. for i, id_value in enumerate(report_data["failed_ids"], 1):
  109. text_content += f" • <font color='red'>{id_value}</font>\n"
  110. # 获取当前时间
  111. now = get_time_func() if get_time_func else datetime.now()
  112. text_content += (
  113. f"\n\n<font color='grey'>更新时间:{now.strftime('%Y-%m-%d %H:%M:%S')}</font>"
  114. )
  115. if update_info:
  116. text_content += f"\n<font color='grey'>TrendRadar 发现新版本 {update_info['remote_version']},当前 {update_info['current_version']}</font>"
  117. return text_content
  118. def render_dingtalk_content(
  119. report_data: Dict,
  120. update_info: Optional[Dict] = None,
  121. mode: str = "daily",
  122. region_order: Optional[List[str]] = None,
  123. get_time_func: Optional[Callable[[], datetime]] = None,
  124. rss_items: Optional[list] = None,
  125. show_new_section: bool = True,
  126. ) -> str:
  127. """渲染钉钉通知内容(支持热榜+RSS合并)
  128. Args:
  129. report_data: 报告数据字典,包含 stats, new_titles, failed_ids, total_new_count
  130. update_info: 版本更新信息(可选)
  131. mode: 报告模式 ("daily", "incremental", "current")
  132. region_order: 区域显示顺序列表
  133. get_time_func: 获取当前时间的函数(可选,默认使用 datetime.now())
  134. rss_items: RSS 条目列表(可选,用于合并推送)
  135. show_new_section: 是否显示新增热点区域
  136. Returns:
  137. 格式化的钉钉消息内容
  138. """
  139. if region_order is None:
  140. region_order = DEFAULT_REGION_ORDER
  141. total_titles = sum(
  142. len(stat["titles"]) for stat in report_data["stats"] if stat["count"] > 0
  143. )
  144. now = get_time_func() if get_time_func else datetime.now()
  145. # 头部信息
  146. header_content = f"**总新闻数:** {total_titles}\n\n"
  147. header_content += f"**时间:** {now.strftime('%Y-%m-%d %H:%M:%S')}\n\n"
  148. header_content += "**类型:** 热点分析报告\n\n"
  149. header_content += "---\n\n"
  150. # 生成热点词汇统计部分
  151. stats_content = ""
  152. if report_data["stats"]:
  153. stats_content += "📊 **热点词汇统计**\n\n"
  154. total_count = len(report_data["stats"])
  155. for i, stat in enumerate(report_data["stats"]):
  156. word = stat["word"]
  157. count = stat["count"]
  158. sequence_display = f"[{i + 1}/{total_count}]"
  159. if count >= 10:
  160. stats_content += f"🔥 {sequence_display} **{word}** : **{count}** 条\n\n"
  161. elif count >= 5:
  162. stats_content += f"📈 {sequence_display} **{word}** : **{count}** 条\n\n"
  163. else:
  164. stats_content += f"📌 {sequence_display} **{word}** : {count} 条\n\n"
  165. for j, title_data in enumerate(stat["titles"], 1):
  166. formatted_title = format_title_for_platform(
  167. "dingtalk", title_data, show_source=True
  168. )
  169. stats_content += f" {j}. {formatted_title}\n"
  170. if j < len(stat["titles"]):
  171. stats_content += "\n"
  172. if i < len(report_data["stats"]) - 1:
  173. stats_content += "\n---\n\n"
  174. # 生成新增新闻部分
  175. new_titles_content = ""
  176. if show_new_section and report_data["new_titles"]:
  177. new_titles_content += (
  178. f"🆕 **本次新增热点新闻** (共 {report_data['total_new_count']} 条)\n\n"
  179. )
  180. for source_data in report_data["new_titles"]:
  181. new_titles_content += f"**{source_data['source_name']}** ({len(source_data['titles'])} 条):\n\n"
  182. for j, title_data in enumerate(source_data["titles"], 1):
  183. title_data_copy = title_data.copy()
  184. title_data_copy["is_new"] = False
  185. formatted_title = format_title_for_platform(
  186. "dingtalk", title_data_copy, show_source=False
  187. )
  188. new_titles_content += f" {j}. {formatted_title}\n"
  189. new_titles_content += "\n"
  190. # RSS 内容
  191. rss_content = ""
  192. if rss_items:
  193. rss_content = _render_rss_section_markdown(rss_items)
  194. # 准备各区域内容映射
  195. region_contents = {
  196. "hotlist": stats_content,
  197. "new_items": new_titles_content,
  198. "rss": rss_content,
  199. }
  200. # 按 region_order 顺序组装内容
  201. text_content = header_content
  202. has_content = False
  203. for region in region_order:
  204. content = region_contents.get(region, "")
  205. if content:
  206. if has_content:
  207. text_content += "\n---\n\n"
  208. text_content += content
  209. has_content = True
  210. if not has_content:
  211. if mode == "incremental":
  212. mode_text = "增量模式下暂无新增匹配的热点词汇"
  213. elif mode == "current":
  214. mode_text = "当前榜单模式下暂无匹配的热点词汇"
  215. else:
  216. mode_text = "暂无匹配的热点词汇"
  217. text_content += f"📭 {mode_text}\n\n"
  218. if report_data["failed_ids"]:
  219. if "暂无匹配" not in text_content:
  220. text_content += "\n---\n\n"
  221. text_content += "⚠️ **数据获取失败的平台:**\n\n"
  222. for i, id_value in enumerate(report_data["failed_ids"], 1):
  223. text_content += f" • **{id_value}**\n"
  224. text_content += f"\n\n> 更新时间:{now.strftime('%Y-%m-%d %H:%M:%S')}"
  225. if update_info:
  226. text_content += f"\n> TrendRadar 发现新版本 **{update_info['remote_version']}**,当前 **{update_info['current_version']}**"
  227. return text_content
  228. # === RSS 内容渲染辅助函数(用于合并推送) ===
  229. def _render_rss_section_feishu(rss_items: list, separator: str = "---") -> str:
  230. """渲染 RSS 内容区块(飞书格式,用于合并推送)"""
  231. if not rss_items:
  232. return ""
  233. # 按 feed_id 分组
  234. feeds_map: Dict[str, list] = {}
  235. for item in rss_items:
  236. feed_id = item.get("feed_id", "unknown")
  237. if feed_id not in feeds_map:
  238. feeds_map[feed_id] = []
  239. feeds_map[feed_id].append(item)
  240. text_content = f"📰 **RSS 订阅更新** (共 {len(rss_items)} 条)\n\n"
  241. for feed_id, items in feeds_map.items():
  242. feed_name = items[0].get("feed_name", feed_id) if items else feed_id
  243. text_content += f"**{feed_name}** ({len(items)} 条)\n\n"
  244. for i, item in enumerate(items, 1):
  245. title = item.get("title", "")
  246. url = item.get("url", "")
  247. published_at = item.get("published_at", "")
  248. if url:
  249. text_content += f" {i}. [{title}]({url})"
  250. else:
  251. text_content += f" {i}. {title}"
  252. if published_at:
  253. text_content += f" <font color='grey'>- {published_at}</font>"
  254. text_content += "\n"
  255. if i < len(items):
  256. text_content += "\n"
  257. text_content += "\n"
  258. return text_content.rstrip("\n")
  259. def _render_rss_section_markdown(rss_items: list) -> str:
  260. """渲染 RSS 内容区块(通用 Markdown 格式,用于合并推送)"""
  261. if not rss_items:
  262. return ""
  263. # 按 feed_id 分组
  264. feeds_map: Dict[str, list] = {}
  265. for item in rss_items:
  266. feed_id = item.get("feed_id", "unknown")
  267. if feed_id not in feeds_map:
  268. feeds_map[feed_id] = []
  269. feeds_map[feed_id].append(item)
  270. text_content = f"📰 **RSS 订阅更新** (共 {len(rss_items)} 条)\n\n"
  271. for feed_id, items in feeds_map.items():
  272. feed_name = items[0].get("feed_name", feed_id) if items else feed_id
  273. text_content += f"**{feed_name}** ({len(items)} 条)\n"
  274. for i, item in enumerate(items, 1):
  275. title = item.get("title", "")
  276. url = item.get("url", "")
  277. published_at = item.get("published_at", "")
  278. if url:
  279. text_content += f" {i}. [{title}]({url})"
  280. else:
  281. text_content += f" {i}. {title}"
  282. if published_at:
  283. text_content += f" `{published_at}`"
  284. text_content += "\n"
  285. text_content += "\n"
  286. return text_content.rstrip("\n")