senders.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. # coding=utf-8
  2. """
  3. 消息发送器模块
  4. 将报告数据发送到各种通知渠道:
  5. - 飞书 (Feishu/Lark)
  6. - 钉钉 (DingTalk)
  7. - 企业微信 (WeCom/WeWork)
  8. - Telegram
  9. - 邮件 (Email)
  10. - ntfy
  11. - Bark
  12. - Slack
  13. 每个发送函数都支持分批发送,并通过参数化配置实现与 CONFIG 的解耦。
  14. """
  15. import smtplib
  16. import time
  17. from datetime import datetime
  18. from email.header import Header
  19. from email.mime.multipart import MIMEMultipart
  20. from email.mime.text import MIMEText
  21. from email.utils import formataddr, formatdate, make_msgid
  22. from pathlib import Path
  23. from typing import Callable, Dict, List, Optional
  24. from urllib.parse import urlparse
  25. import requests
  26. from .batch import add_batch_headers, get_max_batch_header_size
  27. from .formatters import convert_markdown_to_mrkdwn, strip_markdown
  28. # === SMTP 邮件配置 ===
  29. SMTP_CONFIGS = {
  30. # Gmail(使用 STARTTLS)
  31. "gmail.com": {"server": "smtp.gmail.com", "port": 587, "encryption": "TLS"},
  32. # QQ邮箱(使用 SSL,更稳定)
  33. "qq.com": {"server": "smtp.qq.com", "port": 465, "encryption": "SSL"},
  34. # Outlook(使用 STARTTLS)
  35. "outlook.com": {"server": "smtp-mail.outlook.com", "port": 587, "encryption": "TLS"},
  36. "hotmail.com": {"server": "smtp-mail.outlook.com", "port": 587, "encryption": "TLS"},
  37. "live.com": {"server": "smtp-mail.outlook.com", "port": 587, "encryption": "TLS"},
  38. # 网易邮箱(使用 SSL,更稳定)
  39. "163.com": {"server": "smtp.163.com", "port": 465, "encryption": "SSL"},
  40. "126.com": {"server": "smtp.126.com", "port": 465, "encryption": "SSL"},
  41. # 新浪邮箱(使用 SSL)
  42. "sina.com": {"server": "smtp.sina.com", "port": 465, "encryption": "SSL"},
  43. # 搜狐邮箱(使用 SSL)
  44. "sohu.com": {"server": "smtp.sohu.com", "port": 465, "encryption": "SSL"},
  45. # 天翼邮箱(使用 SSL)
  46. "189.cn": {"server": "smtp.189.cn", "port": 465, "encryption": "SSL"},
  47. # 阿里云邮箱(使用 TLS)
  48. "aliyun.com": {"server": "smtp.aliyun.com", "port": 465, "encryption": "TLS"},
  49. # Yandex邮箱(使用 TLS)
  50. "yandex.com": {"server": "smtp.yandex.com", "port": 465, "encryption": "TLS"},
  51. }
  52. def send_to_feishu(
  53. webhook_url: str,
  54. report_data: Dict,
  55. report_type: str,
  56. update_info: Optional[Dict] = None,
  57. proxy_url: Optional[str] = None,
  58. mode: str = "daily",
  59. account_label: str = "",
  60. *,
  61. batch_size: int = 29000,
  62. batch_interval: float = 1.0,
  63. split_content_func: Callable = None,
  64. get_time_func: Callable = None,
  65. rss_items: Optional[list] = None,
  66. rss_new_items: Optional[list] = None,
  67. ) -> bool:
  68. """
  69. 发送到飞书(支持分批发送,支持热榜+RSS合并)
  70. Args:
  71. webhook_url: 飞书 Webhook URL
  72. report_data: 报告数据
  73. report_type: 报告类型
  74. update_info: 更新信息(可选)
  75. proxy_url: 代理 URL(可选)
  76. mode: 报告模式 (daily/current)
  77. account_label: 账号标签(多账号时显示)
  78. batch_size: 批次大小(字节)
  79. batch_interval: 批次发送间隔(秒)
  80. split_content_func: 内容分批函数
  81. get_time_func: 获取当前时间的函数
  82. rss_items: RSS 统计条目列表(可选,用于合并推送)
  83. rss_new_items: RSS 新增条目列表(可选,用于新增区块)
  84. Returns:
  85. bool: 发送是否成功
  86. """
  87. headers = {"Content-Type": "application/json"}
  88. proxies = None
  89. if proxy_url:
  90. proxies = {"http": proxy_url, "https": proxy_url}
  91. # 日志前缀
  92. log_prefix = f"飞书{account_label}" if account_label else "飞书"
  93. # 预留批次头部空间,避免添加头部后超限
  94. header_reserve = get_max_batch_header_size("feishu")
  95. batches = split_content_func(
  96. report_data,
  97. "feishu",
  98. update_info,
  99. max_bytes=batch_size - header_reserve,
  100. mode=mode,
  101. rss_items=rss_items,
  102. rss_new_items=rss_new_items,
  103. )
  104. # 统一添加批次头部(已预留空间,不会超限)
  105. batches = add_batch_headers(batches, "feishu", batch_size)
  106. print(f"{log_prefix}消息分为 {len(batches)} 批次发送 [{report_type}]")
  107. # 逐批发送
  108. for i, batch_content in enumerate(batches, 1):
  109. content_size = len(batch_content.encode("utf-8"))
  110. print(
  111. f"发送{log_prefix}第 {i}/{len(batches)} 批次,大小:{content_size} 字节 [{report_type}]"
  112. )
  113. total_titles = sum(
  114. len(stat["titles"]) for stat in report_data["stats"] if stat["count"] > 0
  115. )
  116. now = get_time_func() if get_time_func else datetime.now()
  117. payload = {
  118. "msg_type": "text",
  119. "content": {
  120. "total_titles": total_titles,
  121. "timestamp": now.strftime("%Y-%m-%d %H:%M:%S"),
  122. "report_type": report_type,
  123. "text": batch_content,
  124. },
  125. }
  126. try:
  127. response = requests.post(
  128. webhook_url, headers=headers, json=payload, proxies=proxies, timeout=30
  129. )
  130. if response.status_code == 200:
  131. result = response.json()
  132. # 检查飞书的响应状态
  133. if result.get("StatusCode") == 0 or result.get("code") == 0:
  134. print(f"{log_prefix}第 {i}/{len(batches)} 批次发送成功 [{report_type}]")
  135. # 批次间间隔
  136. if i < len(batches):
  137. time.sleep(batch_interval)
  138. else:
  139. error_msg = result.get("msg") or result.get("StatusMessage", "未知错误")
  140. print(
  141. f"{log_prefix}第 {i}/{len(batches)} 批次发送失败 [{report_type}],错误:{error_msg}"
  142. )
  143. return False
  144. else:
  145. print(
  146. f"{log_prefix}第 {i}/{len(batches)} 批次发送失败 [{report_type}],状态码:{response.status_code}"
  147. )
  148. return False
  149. except Exception as e:
  150. print(f"{log_prefix}第 {i}/{len(batches)} 批次发送出错 [{report_type}]:{e}")
  151. return False
  152. print(f"{log_prefix}所有 {len(batches)} 批次发送完成 [{report_type}]")
  153. return True
  154. def send_to_dingtalk(
  155. webhook_url: str,
  156. report_data: Dict,
  157. report_type: str,
  158. update_info: Optional[Dict] = None,
  159. proxy_url: Optional[str] = None,
  160. mode: str = "daily",
  161. account_label: str = "",
  162. *,
  163. batch_size: int = 20000,
  164. batch_interval: float = 1.0,
  165. split_content_func: Callable = None,
  166. rss_items: Optional[list] = None,
  167. rss_new_items: Optional[list] = None,
  168. ) -> bool:
  169. """
  170. 发送到钉钉(支持分批发送,支持热榜+RSS合并)
  171. Args:
  172. webhook_url: 钉钉 Webhook URL
  173. report_data: 报告数据
  174. report_type: 报告类型
  175. update_info: 更新信息(可选)
  176. proxy_url: 代理 URL(可选)
  177. mode: 报告模式 (daily/current)
  178. account_label: 账号标签(多账号时显示)
  179. batch_size: 批次大小(字节)
  180. batch_interval: 批次发送间隔(秒)
  181. split_content_func: 内容分批函数
  182. rss_items: RSS 统计条目列表(可选,用于合并推送)
  183. rss_new_items: RSS 新增条目列表(可选,用于新增区块)
  184. Returns:
  185. bool: 发送是否成功
  186. """
  187. headers = {"Content-Type": "application/json"}
  188. proxies = None
  189. if proxy_url:
  190. proxies = {"http": proxy_url, "https": proxy_url}
  191. # 日志前缀
  192. log_prefix = f"钉钉{account_label}" if account_label else "钉钉"
  193. # 预留批次头部空间,避免添加头部后超限
  194. header_reserve = get_max_batch_header_size("dingtalk")
  195. batches = split_content_func(
  196. report_data,
  197. "dingtalk",
  198. update_info,
  199. max_bytes=batch_size - header_reserve,
  200. mode=mode,
  201. rss_items=rss_items,
  202. rss_new_items=rss_new_items,
  203. )
  204. # 统一添加批次头部(已预留空间,不会超限)
  205. batches = add_batch_headers(batches, "dingtalk", batch_size)
  206. print(f"{log_prefix}消息分为 {len(batches)} 批次发送 [{report_type}]")
  207. # 逐批发送
  208. for i, batch_content in enumerate(batches, 1):
  209. content_size = len(batch_content.encode("utf-8"))
  210. print(
  211. f"发送{log_prefix}第 {i}/{len(batches)} 批次,大小:{content_size} 字节 [{report_type}]"
  212. )
  213. payload = {
  214. "msgtype": "markdown",
  215. "markdown": {
  216. "title": f"TrendRadar 热点分析报告 - {report_type}",
  217. "text": batch_content,
  218. },
  219. }
  220. try:
  221. response = requests.post(
  222. webhook_url, headers=headers, json=payload, proxies=proxies, timeout=30
  223. )
  224. if response.status_code == 200:
  225. result = response.json()
  226. if result.get("errcode") == 0:
  227. print(f"{log_prefix}第 {i}/{len(batches)} 批次发送成功 [{report_type}]")
  228. # 批次间间隔
  229. if i < len(batches):
  230. time.sleep(batch_interval)
  231. else:
  232. print(
  233. f"{log_prefix}第 {i}/{len(batches)} 批次发送失败 [{report_type}],错误:{result.get('errmsg')}"
  234. )
  235. return False
  236. else:
  237. print(
  238. f"{log_prefix}第 {i}/{len(batches)} 批次发送失败 [{report_type}],状态码:{response.status_code}"
  239. )
  240. return False
  241. except Exception as e:
  242. print(f"{log_prefix}第 {i}/{len(batches)} 批次发送出错 [{report_type}]:{e}")
  243. return False
  244. print(f"{log_prefix}所有 {len(batches)} 批次发送完成 [{report_type}]")
  245. return True
  246. def send_to_wework(
  247. webhook_url: str,
  248. report_data: Dict,
  249. report_type: str,
  250. update_info: Optional[Dict] = None,
  251. proxy_url: Optional[str] = None,
  252. mode: str = "daily",
  253. account_label: str = "",
  254. *,
  255. batch_size: int = 4000,
  256. batch_interval: float = 1.0,
  257. msg_type: str = "markdown",
  258. split_content_func: Callable = None,
  259. rss_items: Optional[list] = None,
  260. rss_new_items: Optional[list] = None,
  261. ) -> bool:
  262. """
  263. 发送到企业微信(支持分批发送,支持 markdown 和 text 两种格式,支持热榜+RSS合并)
  264. Args:
  265. webhook_url: 企业微信 Webhook URL
  266. report_data: 报告数据
  267. report_type: 报告类型
  268. update_info: 更新信息(可选)
  269. proxy_url: 代理 URL(可选)
  270. mode: 报告模式 (daily/current)
  271. account_label: 账号标签(多账号时显示)
  272. batch_size: 批次大小(字节)
  273. batch_interval: 批次发送间隔(秒)
  274. msg_type: 消息类型 (markdown/text)
  275. split_content_func: 内容分批函数
  276. rss_items: RSS 统计条目列表(可选,用于合并推送)
  277. rss_new_items: RSS 新增条目列表(可选,用于新增区块)
  278. Returns:
  279. bool: 发送是否成功
  280. """
  281. headers = {"Content-Type": "application/json"}
  282. proxies = None
  283. if proxy_url:
  284. proxies = {"http": proxy_url, "https": proxy_url}
  285. # 日志前缀
  286. log_prefix = f"企业微信{account_label}" if account_label else "企业微信"
  287. # 获取消息类型配置(markdown 或 text)
  288. is_text_mode = msg_type.lower() == "text"
  289. if is_text_mode:
  290. print(f"{log_prefix}使用 text 格式(个人微信模式)[{report_type}]")
  291. else:
  292. print(f"{log_prefix}使用 markdown 格式(群机器人模式)[{report_type}]")
  293. # text 模式使用 wework_text,markdown 模式使用 wework
  294. header_format_type = "wework_text" if is_text_mode else "wework"
  295. # 获取分批内容,预留批次头部空间
  296. header_reserve = get_max_batch_header_size(header_format_type)
  297. batches = split_content_func(
  298. report_data, "wework", update_info, max_bytes=batch_size - header_reserve, mode=mode,
  299. rss_items=rss_items,
  300. rss_new_items=rss_new_items,
  301. )
  302. # 统一添加批次头部(已预留空间,不会超限)
  303. batches = add_batch_headers(batches, header_format_type, batch_size)
  304. print(f"{log_prefix}消息分为 {len(batches)} 批次发送 [{report_type}]")
  305. # 逐批发送
  306. for i, batch_content in enumerate(batches, 1):
  307. # 根据消息类型构建 payload
  308. if is_text_mode:
  309. # text 格式:去除 markdown 语法
  310. plain_content = strip_markdown(batch_content)
  311. payload = {"msgtype": "text", "text": {"content": plain_content}}
  312. content_size = len(plain_content.encode("utf-8"))
  313. else:
  314. # markdown 格式:保持原样
  315. payload = {"msgtype": "markdown", "markdown": {"content": batch_content}}
  316. content_size = len(batch_content.encode("utf-8"))
  317. print(
  318. f"发送{log_prefix}第 {i}/{len(batches)} 批次,大小:{content_size} 字节 [{report_type}]"
  319. )
  320. try:
  321. response = requests.post(
  322. webhook_url, headers=headers, json=payload, proxies=proxies, timeout=30
  323. )
  324. if response.status_code == 200:
  325. result = response.json()
  326. if result.get("errcode") == 0:
  327. print(f"{log_prefix}第 {i}/{len(batches)} 批次发送成功 [{report_type}]")
  328. # 批次间间隔
  329. if i < len(batches):
  330. time.sleep(batch_interval)
  331. else:
  332. print(
  333. f"{log_prefix}第 {i}/{len(batches)} 批次发送失败 [{report_type}],错误:{result.get('errmsg')}"
  334. )
  335. return False
  336. else:
  337. print(
  338. f"{log_prefix}第 {i}/{len(batches)} 批次发送失败 [{report_type}],状态码:{response.status_code}"
  339. )
  340. return False
  341. except Exception as e:
  342. print(f"{log_prefix}第 {i}/{len(batches)} 批次发送出错 [{report_type}]:{e}")
  343. return False
  344. print(f"{log_prefix}所有 {len(batches)} 批次发送完成 [{report_type}]")
  345. return True
  346. def send_to_telegram(
  347. bot_token: str,
  348. chat_id: str,
  349. report_data: Dict,
  350. report_type: str,
  351. update_info: Optional[Dict] = None,
  352. proxy_url: Optional[str] = None,
  353. mode: str = "daily",
  354. account_label: str = "",
  355. *,
  356. batch_size: int = 4000,
  357. batch_interval: float = 1.0,
  358. split_content_func: Callable = None,
  359. rss_items: Optional[list] = None,
  360. rss_new_items: Optional[list] = None,
  361. ) -> bool:
  362. """
  363. 发送到 Telegram(支持分批发送,支持热榜+RSS合并)
  364. Args:
  365. bot_token: Telegram Bot Token
  366. chat_id: Telegram Chat ID
  367. report_data: 报告数据
  368. report_type: 报告类型
  369. update_info: 更新信息(可选)
  370. proxy_url: 代理 URL(可选)
  371. mode: 报告模式 (daily/current)
  372. account_label: 账号标签(多账号时显示)
  373. batch_size: 批次大小(字节)
  374. batch_interval: 批次发送间隔(秒)
  375. split_content_func: 内容分批函数
  376. rss_items: RSS 统计条目列表(可选,用于合并推送)
  377. rss_new_items: RSS 新增条目列表(可选,用于新增区块)
  378. Returns:
  379. bool: 发送是否成功
  380. """
  381. headers = {"Content-Type": "application/json"}
  382. url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
  383. proxies = None
  384. if proxy_url:
  385. proxies = {"http": proxy_url, "https": proxy_url}
  386. # 日志前缀
  387. log_prefix = f"Telegram{account_label}" if account_label else "Telegram"
  388. # 获取分批内容,预留批次头部空间
  389. header_reserve = get_max_batch_header_size("telegram")
  390. batches = split_content_func(
  391. report_data, "telegram", update_info, max_bytes=batch_size - header_reserve, mode=mode,
  392. rss_items=rss_items,
  393. rss_new_items=rss_new_items,
  394. )
  395. # 统一添加批次头部(已预留空间,不会超限)
  396. batches = add_batch_headers(batches, "telegram", batch_size)
  397. print(f"{log_prefix}消息分为 {len(batches)} 批次发送 [{report_type}]")
  398. # 逐批发送
  399. for i, batch_content in enumerate(batches, 1):
  400. content_size = len(batch_content.encode("utf-8"))
  401. print(
  402. f"发送{log_prefix}第 {i}/{len(batches)} 批次,大小:{content_size} 字节 [{report_type}]"
  403. )
  404. payload = {
  405. "chat_id": chat_id,
  406. "text": batch_content,
  407. "parse_mode": "HTML",
  408. "disable_web_page_preview": True,
  409. }
  410. try:
  411. response = requests.post(
  412. url, headers=headers, json=payload, proxies=proxies, timeout=30
  413. )
  414. if response.status_code == 200:
  415. result = response.json()
  416. if result.get("ok"):
  417. print(f"{log_prefix}第 {i}/{len(batches)} 批次发送成功 [{report_type}]")
  418. # 批次间间隔
  419. if i < len(batches):
  420. time.sleep(batch_interval)
  421. else:
  422. print(
  423. f"{log_prefix}第 {i}/{len(batches)} 批次发送失败 [{report_type}],错误:{result.get('description')}"
  424. )
  425. return False
  426. else:
  427. print(
  428. f"{log_prefix}第 {i}/{len(batches)} 批次发送失败 [{report_type}],状态码:{response.status_code}"
  429. )
  430. return False
  431. except Exception as e:
  432. print(f"{log_prefix}第 {i}/{len(batches)} 批次发送出错 [{report_type}]:{e}")
  433. return False
  434. print(f"{log_prefix}所有 {len(batches)} 批次发送完成 [{report_type}]")
  435. return True
  436. def send_to_email(
  437. from_email: str,
  438. password: str,
  439. to_email: str,
  440. report_type: str,
  441. html_file_path: str,
  442. custom_smtp_server: Optional[str] = None,
  443. custom_smtp_port: Optional[int] = None,
  444. *,
  445. get_time_func: Callable = None,
  446. ) -> bool:
  447. """
  448. 发送邮件通知
  449. Args:
  450. from_email: 发件人邮箱
  451. password: 邮箱密码/授权码
  452. to_email: 收件人邮箱(多个用逗号分隔)
  453. report_type: 报告类型
  454. html_file_path: HTML 报告文件路径
  455. custom_smtp_server: 自定义 SMTP 服务器(可选)
  456. custom_smtp_port: 自定义 SMTP 端口(可选)
  457. get_time_func: 获取当前时间的函数
  458. Returns:
  459. bool: 发送是否成功
  460. """
  461. try:
  462. if not html_file_path or not Path(html_file_path).exists():
  463. print(f"错误:HTML文件不存在或未提供: {html_file_path}")
  464. return False
  465. print(f"使用HTML文件: {html_file_path}")
  466. with open(html_file_path, "r", encoding="utf-8") as f:
  467. html_content = f.read()
  468. domain = from_email.split("@")[-1].lower()
  469. if custom_smtp_server and custom_smtp_port:
  470. # 使用自定义 SMTP 配置
  471. smtp_server = custom_smtp_server
  472. smtp_port = int(custom_smtp_port)
  473. # 根据端口判断加密方式:465=SSL, 587=TLS
  474. if smtp_port == 465:
  475. use_tls = False # SSL 模式(SMTP_SSL)
  476. elif smtp_port == 587:
  477. use_tls = True # TLS 模式(STARTTLS)
  478. else:
  479. # 其他端口优先尝试 TLS(更安全,更广泛支持)
  480. use_tls = True
  481. elif domain in SMTP_CONFIGS:
  482. # 使用预设配置
  483. config = SMTP_CONFIGS[domain]
  484. smtp_server = config["server"]
  485. smtp_port = config["port"]
  486. use_tls = config["encryption"] == "TLS"
  487. else:
  488. print(f"未识别的邮箱服务商: {domain},使用通用 SMTP 配置")
  489. smtp_server = f"smtp.{domain}"
  490. smtp_port = 587
  491. use_tls = True
  492. msg = MIMEMultipart("alternative")
  493. # 严格按照 RFC 标准设置 From header
  494. sender_name = "TrendRadar"
  495. msg["From"] = formataddr((sender_name, from_email))
  496. # 设置收件人
  497. recipients = [addr.strip() for addr in to_email.split(",")]
  498. if len(recipients) == 1:
  499. msg["To"] = recipients[0]
  500. else:
  501. msg["To"] = ", ".join(recipients)
  502. # 设置邮件主题
  503. now = get_time_func() if get_time_func else datetime.now()
  504. subject = f"TrendRadar 热点分析报告 - {report_type} - {now.strftime('%m月%d日 %H:%M')}"
  505. msg["Subject"] = Header(subject, "utf-8")
  506. # 设置其他标准 header
  507. msg["MIME-Version"] = "1.0"
  508. msg["Date"] = formatdate(localtime=True)
  509. msg["Message-ID"] = make_msgid()
  510. # 添加纯文本部分(作为备选)
  511. text_content = f"""
  512. TrendRadar 热点分析报告
  513. ========================
  514. 报告类型:{report_type}
  515. 生成时间:{now.strftime('%Y-%m-%d %H:%M:%S')}
  516. 请使用支持HTML的邮件客户端查看完整报告内容。
  517. """
  518. text_part = MIMEText(text_content, "plain", "utf-8")
  519. msg.attach(text_part)
  520. html_part = MIMEText(html_content, "html", "utf-8")
  521. msg.attach(html_part)
  522. print(f"正在发送邮件到 {to_email}...")
  523. print(f"SMTP 服务器: {smtp_server}:{smtp_port}")
  524. print(f"发件人: {from_email}")
  525. try:
  526. if use_tls:
  527. # TLS 模式
  528. server = smtplib.SMTP(smtp_server, smtp_port, timeout=30)
  529. server.set_debuglevel(0) # 设为1可以查看详细调试信息
  530. server.ehlo()
  531. server.starttls()
  532. server.ehlo()
  533. else:
  534. # SSL 模式
  535. server = smtplib.SMTP_SSL(smtp_server, smtp_port, timeout=30)
  536. server.set_debuglevel(0)
  537. server.ehlo()
  538. # 登录
  539. server.login(from_email, password)
  540. # 发送邮件
  541. server.send_message(msg)
  542. server.quit()
  543. print(f"邮件发送成功 [{report_type}] -> {to_email}")
  544. return True
  545. except smtplib.SMTPServerDisconnected:
  546. print("邮件发送失败:服务器意外断开连接,请检查网络或稍后重试")
  547. return False
  548. except smtplib.SMTPAuthenticationError as e:
  549. print("邮件发送失败:认证错误,请检查邮箱和密码/授权码")
  550. print(f"详细错误: {str(e)}")
  551. return False
  552. except smtplib.SMTPRecipientsRefused as e:
  553. print(f"邮件发送失败:收件人地址被拒绝 {e}")
  554. return False
  555. except smtplib.SMTPSenderRefused as e:
  556. print(f"邮件发送失败:发件人地址被拒绝 {e}")
  557. return False
  558. except smtplib.SMTPDataError as e:
  559. print(f"邮件发送失败:邮件数据错误 {e}")
  560. return False
  561. except smtplib.SMTPConnectError as e:
  562. print(f"邮件发送失败:无法连接到 SMTP 服务器 {smtp_server}:{smtp_port}")
  563. print(f"详细错误: {str(e)}")
  564. return False
  565. except Exception as e:
  566. print(f"邮件发送失败 [{report_type}]:{e}")
  567. import traceback
  568. traceback.print_exc()
  569. return False
  570. def send_to_ntfy(
  571. server_url: str,
  572. topic: str,
  573. token: Optional[str],
  574. report_data: Dict,
  575. report_type: str,
  576. update_info: Optional[Dict] = None,
  577. proxy_url: Optional[str] = None,
  578. mode: str = "daily",
  579. account_label: str = "",
  580. *,
  581. batch_size: int = 3800,
  582. split_content_func: Callable = None,
  583. rss_items: Optional[list] = None,
  584. rss_new_items: Optional[list] = None,
  585. ) -> bool:
  586. """
  587. 发送到 ntfy(支持分批发送,严格遵守4KB限制,支持热榜+RSS合并)
  588. Args:
  589. server_url: ntfy 服务器 URL
  590. topic: ntfy 主题
  591. token: ntfy 访问令牌(可选)
  592. report_data: 报告数据
  593. report_type: 报告类型
  594. update_info: 更新信息(可选)
  595. proxy_url: 代理 URL(可选)
  596. mode: 报告模式 (daily/current)
  597. account_label: 账号标签(多账号时显示)
  598. batch_size: 批次大小(字节)
  599. split_content_func: 内容分批函数
  600. rss_items: RSS 统计条目列表(可选,用于合并推送)
  601. rss_new_items: RSS 新增条目列表(可选,用于新增区块)
  602. Returns:
  603. bool: 发送是否成功
  604. """
  605. # 日志前缀
  606. log_prefix = f"ntfy{account_label}" if account_label else "ntfy"
  607. # 避免 HTTP header 编码问题
  608. report_type_en_map = {
  609. "当日汇总": "Daily Summary",
  610. "当前榜单汇总": "Current Ranking",
  611. "增量更新": "Incremental Update",
  612. "实时增量": "Realtime Incremental",
  613. "实时当前榜单": "Realtime Current Ranking",
  614. }
  615. report_type_en = report_type_en_map.get(report_type, "News Report")
  616. headers = {
  617. "Content-Type": "text/plain; charset=utf-8",
  618. "Markdown": "yes",
  619. "Title": report_type_en,
  620. "Priority": "default",
  621. "Tags": "news",
  622. }
  623. if token:
  624. headers["Authorization"] = f"Bearer {token}"
  625. # 构建完整URL,确保格式正确
  626. base_url = server_url.rstrip("/")
  627. if not base_url.startswith(("http://", "https://")):
  628. base_url = f"https://{base_url}"
  629. url = f"{base_url}/{topic}"
  630. proxies = None
  631. if proxy_url:
  632. proxies = {"http": proxy_url, "https": proxy_url}
  633. # 获取分批内容,预留批次头部空间
  634. header_reserve = get_max_batch_header_size("ntfy")
  635. batches = split_content_func(
  636. report_data, "ntfy", update_info, max_bytes=batch_size - header_reserve, mode=mode,
  637. rss_items=rss_items,
  638. rss_new_items=rss_new_items,
  639. )
  640. # 统一添加批次头部(已预留空间,不会超限)
  641. batches = add_batch_headers(batches, "ntfy", batch_size)
  642. total_batches = len(batches)
  643. print(f"{log_prefix}消息分为 {total_batches} 批次发送 [{report_type}]")
  644. # 反转批次顺序,使得在ntfy客户端显示时顺序正确
  645. # ntfy显示最新消息在上面,所以我们从最后一批开始推送
  646. reversed_batches = list(reversed(batches))
  647. print(f"{log_prefix}将按反向顺序推送(最后批次先推送),确保客户端显示顺序正确")
  648. # 逐批发送(反向顺序)
  649. success_count = 0
  650. for idx, batch_content in enumerate(reversed_batches, 1):
  651. # 计算正确的批次编号(用户视角的编号)
  652. actual_batch_num = total_batches - idx + 1
  653. content_size = len(batch_content.encode("utf-8"))
  654. print(
  655. f"发送{log_prefix}第 {actual_batch_num}/{total_batches} 批次(推送顺序: {idx}/{total_batches}),大小:{content_size} 字节 [{report_type}]"
  656. )
  657. # 检查消息大小,确保不超过4KB
  658. if content_size > 4096:
  659. print(f"警告:{log_prefix}第 {actual_batch_num} 批次消息过大({content_size} 字节),可能被拒绝")
  660. # 更新 headers 的批次标识
  661. current_headers = headers.copy()
  662. if total_batches > 1:
  663. current_headers["Title"] = f"{report_type_en} ({actual_batch_num}/{total_batches})"
  664. try:
  665. response = requests.post(
  666. url,
  667. headers=current_headers,
  668. data=batch_content.encode("utf-8"),
  669. proxies=proxies,
  670. timeout=30,
  671. )
  672. if response.status_code == 200:
  673. print(f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次发送成功 [{report_type}]")
  674. success_count += 1
  675. if idx < total_batches:
  676. # 公共服务器建议 2-3 秒,自托管可以更短
  677. interval = 2 if "ntfy.sh" in server_url else 1
  678. time.sleep(interval)
  679. elif response.status_code == 429:
  680. print(
  681. f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次速率限制 [{report_type}],等待后重试"
  682. )
  683. time.sleep(10) # 等待10秒后重试
  684. # 重试一次
  685. retry_response = requests.post(
  686. url,
  687. headers=current_headers,
  688. data=batch_content.encode("utf-8"),
  689. proxies=proxies,
  690. timeout=30,
  691. )
  692. if retry_response.status_code == 200:
  693. print(f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次重试成功 [{report_type}]")
  694. success_count += 1
  695. else:
  696. print(
  697. f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次重试失败,状态码:{retry_response.status_code}"
  698. )
  699. elif response.status_code == 413:
  700. print(
  701. f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次消息过大被拒绝 [{report_type}],消息大小:{content_size} 字节"
  702. )
  703. else:
  704. print(
  705. f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次发送失败 [{report_type}],状态码:{response.status_code}"
  706. )
  707. try:
  708. print(f"错误详情:{response.text}")
  709. except:
  710. pass
  711. except requests.exceptions.ConnectTimeout:
  712. print(f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次连接超时 [{report_type}]")
  713. except requests.exceptions.ReadTimeout:
  714. print(f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次读取超时 [{report_type}]")
  715. except requests.exceptions.ConnectionError as e:
  716. print(f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次连接错误 [{report_type}]:{e}")
  717. except Exception as e:
  718. print(f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次发送异常 [{report_type}]:{e}")
  719. # 判断整体发送是否成功
  720. if success_count == total_batches:
  721. print(f"{log_prefix}所有 {total_batches} 批次发送完成 [{report_type}]")
  722. return True
  723. elif success_count > 0:
  724. print(f"{log_prefix}部分发送成功:{success_count}/{total_batches} 批次 [{report_type}]")
  725. return True # 部分成功也视为成功
  726. else:
  727. print(f"{log_prefix}发送完全失败 [{report_type}]")
  728. return False
  729. def send_to_bark(
  730. bark_url: str,
  731. report_data: Dict,
  732. report_type: str,
  733. update_info: Optional[Dict] = None,
  734. proxy_url: Optional[str] = None,
  735. mode: str = "daily",
  736. account_label: str = "",
  737. *,
  738. batch_size: int = 3600,
  739. batch_interval: float = 1.0,
  740. split_content_func: Callable = None,
  741. rss_items: Optional[list] = None,
  742. rss_new_items: Optional[list] = None,
  743. ) -> bool:
  744. """
  745. 发送到 Bark(支持分批发送,使用 markdown 格式,支持热榜+RSS合并)
  746. Args:
  747. bark_url: Bark URL(包含 device_key)
  748. report_data: 报告数据
  749. report_type: 报告类型
  750. update_info: 更新信息(可选)
  751. proxy_url: 代理 URL(可选)
  752. mode: 报告模式 (daily/current)
  753. account_label: 账号标签(多账号时显示)
  754. batch_size: 批次大小(字节)
  755. batch_interval: 批次发送间隔(秒)
  756. split_content_func: 内容分批函数
  757. rss_items: RSS 统计条目列表(可选,用于合并推送)
  758. rss_new_items: RSS 新增条目列表(可选,用于新增区块)
  759. Returns:
  760. bool: 发送是否成功
  761. """
  762. # 日志前缀
  763. log_prefix = f"Bark{account_label}" if account_label else "Bark"
  764. proxies = None
  765. if proxy_url:
  766. proxies = {"http": proxy_url, "https": proxy_url}
  767. # 解析 Bark URL,提取 device_key 和 API 端点
  768. # Bark URL 格式: https://api.day.app/device_key 或 https://bark.day.app/device_key
  769. parsed_url = urlparse(bark_url)
  770. device_key = parsed_url.path.strip('/').split('/')[0] if parsed_url.path else None
  771. if not device_key:
  772. print(f"{log_prefix} URL 格式错误,无法提取 device_key: {bark_url}")
  773. return False
  774. # 构建正确的 API 端点
  775. api_endpoint = f"{parsed_url.scheme}://{parsed_url.netloc}/push"
  776. # 获取分批内容,预留批次头部空间
  777. header_reserve = get_max_batch_header_size("bark")
  778. batches = split_content_func(
  779. report_data, "bark", update_info, max_bytes=batch_size - header_reserve, mode=mode,
  780. rss_items=rss_items,
  781. rss_new_items=rss_new_items,
  782. )
  783. # 统一添加批次头部(已预留空间,不会超限)
  784. batches = add_batch_headers(batches, "bark", batch_size)
  785. total_batches = len(batches)
  786. print(f"{log_prefix}消息分为 {total_batches} 批次发送 [{report_type}]")
  787. # 反转批次顺序,使得在Bark客户端显示时顺序正确
  788. # Bark显示最新消息在上面,所以我们从最后一批开始推送
  789. reversed_batches = list(reversed(batches))
  790. print(f"{log_prefix}将按反向顺序推送(最后批次先推送),确保客户端显示顺序正确")
  791. # 逐批发送(反向顺序)
  792. success_count = 0
  793. for idx, batch_content in enumerate(reversed_batches, 1):
  794. # 计算正确的批次编号(用户视角的编号)
  795. actual_batch_num = total_batches - idx + 1
  796. content_size = len(batch_content.encode("utf-8"))
  797. print(
  798. f"发送{log_prefix}第 {actual_batch_num}/{total_batches} 批次(推送顺序: {idx}/{total_batches}),大小:{content_size} 字节 [{report_type}]"
  799. )
  800. # 检查消息大小(Bark使用APNs,限制4KB)
  801. if content_size > 4096:
  802. print(
  803. f"警告:{log_prefix}第 {actual_batch_num}/{total_batches} 批次消息过大({content_size} 字节),可能被拒绝"
  804. )
  805. # 构建JSON payload
  806. payload = {
  807. "title": report_type,
  808. "markdown": batch_content,
  809. "device_key": device_key,
  810. "sound": "default",
  811. "group": "TrendRadar",
  812. "action": "none", # 点击推送跳到 APP 不弹出弹框,方便阅读
  813. }
  814. try:
  815. response = requests.post(
  816. api_endpoint,
  817. json=payload,
  818. proxies=proxies,
  819. timeout=30,
  820. )
  821. if response.status_code == 200:
  822. result = response.json()
  823. if result.get("code") == 200:
  824. print(f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次发送成功 [{report_type}]")
  825. success_count += 1
  826. # 批次间间隔
  827. if idx < total_batches:
  828. time.sleep(batch_interval)
  829. else:
  830. print(
  831. f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次发送失败 [{report_type}],错误:{result.get('message', '未知错误')}"
  832. )
  833. else:
  834. print(
  835. f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次发送失败 [{report_type}],状态码:{response.status_code}"
  836. )
  837. try:
  838. print(f"错误详情:{response.text}")
  839. except:
  840. pass
  841. except requests.exceptions.ConnectTimeout:
  842. print(f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次连接超时 [{report_type}]")
  843. except requests.exceptions.ReadTimeout:
  844. print(f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次读取超时 [{report_type}]")
  845. except requests.exceptions.ConnectionError as e:
  846. print(f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次连接错误 [{report_type}]:{e}")
  847. except Exception as e:
  848. print(f"{log_prefix}第 {actual_batch_num}/{total_batches} 批次发送异常 [{report_type}]:{e}")
  849. # 判断整体发送是否成功
  850. if success_count == total_batches:
  851. print(f"{log_prefix}所有 {total_batches} 批次发送完成 [{report_type}]")
  852. return True
  853. elif success_count > 0:
  854. print(f"{log_prefix}部分发送成功:{success_count}/{total_batches} 批次 [{report_type}]")
  855. return True # 部分成功也视为成功
  856. else:
  857. print(f"{log_prefix}发送完全失败 [{report_type}]")
  858. return False
  859. def send_to_slack(
  860. webhook_url: str,
  861. report_data: Dict,
  862. report_type: str,
  863. update_info: Optional[Dict] = None,
  864. proxy_url: Optional[str] = None,
  865. mode: str = "daily",
  866. account_label: str = "",
  867. *,
  868. batch_size: int = 4000,
  869. batch_interval: float = 1.0,
  870. split_content_func: Callable = None,
  871. rss_items: Optional[list] = None,
  872. rss_new_items: Optional[list] = None,
  873. ) -> bool:
  874. """
  875. 发送到 Slack(支持分批发送,使用 mrkdwn 格式,支持热榜+RSS合并)
  876. Args:
  877. webhook_url: Slack Webhook URL
  878. report_data: 报告数据
  879. report_type: 报告类型
  880. update_info: 更新信息(可选)
  881. proxy_url: 代理 URL(可选)
  882. mode: 报告模式 (daily/current)
  883. account_label: 账号标签(多账号时显示)
  884. batch_size: 批次大小(字节)
  885. batch_interval: 批次发送间隔(秒)
  886. split_content_func: 内容分批函数
  887. rss_items: RSS 统计条目列表(可选,用于合并推送)
  888. rss_new_items: RSS 新增条目列表(可选,用于新增区块)
  889. Returns:
  890. bool: 发送是否成功
  891. """
  892. headers = {"Content-Type": "application/json"}
  893. proxies = None
  894. if proxy_url:
  895. proxies = {"http": proxy_url, "https": proxy_url}
  896. # 日志前缀
  897. log_prefix = f"Slack{account_label}" if account_label else "Slack"
  898. # 获取分批内容,预留批次头部空间
  899. header_reserve = get_max_batch_header_size("slack")
  900. batches = split_content_func(
  901. report_data, "slack", update_info, max_bytes=batch_size - header_reserve, mode=mode,
  902. rss_items=rss_items,
  903. rss_new_items=rss_new_items,
  904. )
  905. # 统一添加批次头部(已预留空间,不会超限)
  906. batches = add_batch_headers(batches, "slack", batch_size)
  907. print(f"{log_prefix}消息分为 {len(batches)} 批次发送 [{report_type}]")
  908. # 逐批发送
  909. for i, batch_content in enumerate(batches, 1):
  910. # 转换 Markdown 到 mrkdwn 格式
  911. mrkdwn_content = convert_markdown_to_mrkdwn(batch_content)
  912. content_size = len(mrkdwn_content.encode("utf-8"))
  913. print(
  914. f"发送{log_prefix}第 {i}/{len(batches)} 批次,大小:{content_size} 字节 [{report_type}]"
  915. )
  916. # 构建 Slack payload(使用简单的 text 字段,支持 mrkdwn)
  917. payload = {"text": mrkdwn_content}
  918. try:
  919. response = requests.post(
  920. webhook_url, headers=headers, json=payload, proxies=proxies, timeout=30
  921. )
  922. # Slack Incoming Webhooks 成功时返回 "ok" 文本
  923. if response.status_code == 200 and response.text == "ok":
  924. print(f"{log_prefix}第 {i}/{len(batches)} 批次发送成功 [{report_type}]")
  925. # 批次间间隔
  926. if i < len(batches):
  927. time.sleep(batch_interval)
  928. else:
  929. error_msg = response.text if response.text else f"状态码:{response.status_code}"
  930. print(
  931. f"{log_prefix}第 {i}/{len(batches)} 批次发送失败 [{report_type}],错误:{error_msg}"
  932. )
  933. return False
  934. except Exception as e:
  935. print(f"{log_prefix}第 {i}/{len(batches)} 批次发送出错 [{report_type}]:{e}")
  936. return False
  937. print(f"{log_prefix}所有 {len(batches)} 批次发送完成 [{report_type}]")
  938. return True