This commit is contained in:
sansan 2025-10-08 22:26:25 +08:00
parent 0c2f6fa065
commit a118a788fd
5 changed files with 50 additions and 29 deletions

View File

@ -58,6 +58,9 @@ jobs:
EMAIL_TO: ${{ secrets.EMAIL_TO }}
EMAIL_SMTP_SERVER: ${{ secrets.EMAIL_SMTP_SERVER }}
EMAIL_SMTP_PORT: ${{ secrets.EMAIL_SMTP_PORT }}
NTFY_TOPIC: ${{ secrets.NTFY_TOPIC }}
NTFY_SERVER_URL: ${{ secrets.NTFY_SERVER_URL }}
NTFY_TOKEN: ${{ secrets.NTFY_TOKEN }}
GITHUB_ACTIONS: true
run: python main.py

View File

@ -58,6 +58,9 @@ notification:
email_to: "" # 收件人邮箱地址,多个收件人用逗号分隔
email_smtp_server: "" # SMTP服务器地址可选留空自动识别
email_smtp_port: "" # SMTP端口可选留空自动识别
ntfy_server_url: "https://ntfy.sh" # ntfy服务器地址默认使用公共服务可改为自托管地址
ntfy_topic: "" # ntfy主题名称
ntfy_token: "" # ntfy访问令牌可选用于私有主题
# 用于让关注度更高的新闻在更前面显示,即用算法重新组合不同平台的热搜排序形成你侧重的热搜,合起来是 1 就行
weight:

62
main.py
View File

@ -20,7 +20,7 @@ import requests
import yaml
VERSION = "2.4.1"
VERSION = "2.4.2"
# === SMTP邮件配置 ===
@ -3738,9 +3738,9 @@ def send_to_ntfy(
headers = {
"Content-Type": "text/plain; charset=utf-8",
"Markdown": "yes",
"Title": f"TrendRadar Report - {report_type_en}",
"Title": report_type_en,
"Priority": "default",
"Tags": "newspaper,📰",
"Tags": "news",
}
if token:
@ -3761,27 +3761,37 @@ def send_to_ntfy(
report_data, "ntfy", update_info, max_bytes=3800, mode=mode
)
print(f"ntfy消息分为 {len(batches)} 批次发送 [{report_type}]")
total_batches = len(batches)
print(f"ntfy消息分为 {total_batches} 批次发送 [{report_type}]")
# 逐批发送
# 反转批次顺序使得在ntfy客户端显示时顺序正确
# ntfy显示最新消息在上面所以我们从最后一批开始推送
reversed_batches = list(reversed(batches))
print(f"ntfy将按反向顺序推送最后批次先推送确保客户端显示顺序正确")
# 逐批发送(反向顺序)
success_count = 0
for i, batch_content in enumerate(batches, 1):
for idx, batch_content in enumerate(reversed_batches, 1):
# 计算正确的批次编号(用户视角的编号)
actual_batch_num = total_batches - idx + 1
batch_size = len(batch_content.encode("utf-8"))
print(
f"发送ntfy第 {i}/{len(batches)} 批次,大小:{batch_size} 字节 [{report_type}]"
f"发送ntfy第 {actual_batch_num}/{total_batches} 批次(推送顺序: {idx}/{total_batches},大小:{batch_size} 字节 [{report_type}]"
)
# 检查消息大小确保不超过4KB
if batch_size > 4096:
print(f"警告ntfy第 {i} 批次消息过大({batch_size} 字节),可能被拒绝")
print(f"警告ntfy第 {actual_batch_num} 批次消息过大({batch_size} 字节),可能被拒绝")
# 添加批次标识
# 添加批次标识(使用正确的批次编号)
current_headers = headers.copy()
if len(batches) > 1:
batch_header = f"**[第 {i}/{len(batches)} 批次]**\n\n"
if total_batches > 1:
batch_header = f"**[第 {actual_batch_num}/{total_batches} 批次]**\n\n"
batch_content = batch_header + batch_content
current_headers["Title"] = (
f"TrendRadar Report - {report_type_en} ({i}/{len(batches)})"
f"{report_type_en} ({actual_batch_num}/{total_batches})"
)
try:
@ -3794,15 +3804,15 @@ def send_to_ntfy(
)
if response.status_code == 200:
print(f"ntfy第 {i}/{len(batches)} 批次发送成功 [{report_type}]")
print(f"ntfy第 {actual_batch_num}/{total_batches} 批次发送成功 [{report_type}]")
success_count += 1
if i < len(batches):
if idx < total_batches:
# 公共服务器建议 2-3 秒,自托管可以更短
interval = 2 if "ntfy.sh" in server_url else 1
time.sleep(interval)
elif response.status_code == 429:
print(
f"ntfy第 {i}/{len(batches)} 批次速率限制 [{report_type}],等待后重试"
f"ntfy第 {actual_batch_num}/{total_batches} 批次速率限制 [{report_type}],等待后重试"
)
time.sleep(10) # 等待10秒后重试
# 重试一次
@ -3814,19 +3824,19 @@ def send_to_ntfy(
timeout=30,
)
if retry_response.status_code == 200:
print(f"ntfy第 {i}/{len(batches)} 批次重试成功 [{report_type}]")
print(f"ntfy第 {actual_batch_num}/{total_batches} 批次重试成功 [{report_type}]")
success_count += 1
else:
print(
f"ntfy第 {i}/{len(batches)} 批次重试失败,状态码:{retry_response.status_code}"
f"ntfy第 {actual_batch_num}/{total_batches} 批次重试失败,状态码:{retry_response.status_code}"
)
elif response.status_code == 413:
print(
f"ntfy第 {i}/{len(batches)} 批次消息过大被拒绝 [{report_type}],消息大小:{batch_size} 字节"
f"ntfy第 {actual_batch_num}/{total_batches} 批次消息过大被拒绝 [{report_type}],消息大小:{batch_size} 字节"
)
else:
print(
f"ntfy第 {i}/{len(batches)} 批次发送失败 [{report_type}],状态码:{response.status_code}"
f"ntfy第 {actual_batch_num}/{total_batches} 批次发送失败 [{report_type}],状态码:{response.status_code}"
)
try:
error_detail = response.text[:200] # 只显示前200字符的错误信息
@ -3835,20 +3845,20 @@ def send_to_ntfy(
pass
except requests.exceptions.ConnectTimeout:
print(f"ntfy第 {i}/{len(batches)} 批次连接超时 [{report_type}]")
print(f"ntfy第 {actual_batch_num}/{total_batches} 批次连接超时 [{report_type}]")
except requests.exceptions.ReadTimeout:
print(f"ntfy第 {i}/{len(batches)} 批次读取超时 [{report_type}]")
print(f"ntfy第 {actual_batch_num}/{total_batches} 批次读取超时 [{report_type}]")
except requests.exceptions.ConnectionError as e:
print(f"ntfy第 {i}/{len(batches)} 批次连接错误 [{report_type}]{e}")
print(f"ntfy第 {actual_batch_num}/{total_batches} 批次连接错误 [{report_type}]{e}")
except Exception as e:
print(f"ntfy第 {i}/{len(batches)} 批次发送异常 [{report_type}]{e}")
print(f"ntfy第 {actual_batch_num}/{total_batches} 批次发送异常 [{report_type}]{e}")
# 判断整体发送是否成功
if success_count == len(batches):
print(f"ntfy所有 {len(batches)} 批次发送完成 [{report_type}]")
if success_count == total_batches:
print(f"ntfy所有 {total_batches} 批次发送完成 [{report_type}]")
return True
elif success_count > 0:
print(f"ntfy部分发送成功{success_count}/{len(batches)} 批次 [{report_type}]")
print(f"ntfy部分发送成功{success_count}/{total_batches} 批次 [{report_type}]")
return True # 部分成功也视为成功
else:
print(f"ntfy发送完全失败 [{report_type}]")

View File

@ -11,7 +11,7 @@
[![GitHub Stars](https://img.shields.io/github/stars/sansan0/TrendRadar?style=flat-square&logo=github&color=yellow)](https://github.com/sansan0/TrendRadar/stargazers)
[![GitHub Forks](https://img.shields.io/github/forks/sansan0/TrendRadar?style=flat-square&logo=github&color=blue)](https://github.com/sansan0/TrendRadar/network/members)
[![License](https://img.shields.io/badge/license-GPL--3.0-blue.svg?style=flat-square)](LICENSE)
[![Version](https://img.shields.io/badge/version-v2.4.1-green.svg?style=flat-square)](https://github.com/sansan0/TrendRadar)
[![Version](https://img.shields.io/badge/version-v2.4.2-green.svg?style=flat-square)](https://github.com/sansan0/TrendRadar)
[![企业微信通知](https://img.shields.io/badge/企业微信-通知-00D4AA?style=flat-square)](https://work.weixin.qq.com/)
[![Telegram通知](https://img.shields.io/badge/Telegram-通知-00D4AA?style=flat-square)](https://telegram.org/)
@ -464,11 +464,16 @@ GitHub 一键 Fork 即可使用,无需编程基础。
- **小版本更新**:从 v2.x 升级到 v2.y, 用本项目的 `main.py` 代码替换你 fork 仓库中的对应文件
- **大版本升级**:从 v1.x 升级到 v2.y, 建议删除现有 fork 后重新 fork这样更省力且避免配置冲突
### 2025/10/8 - v2.4.1
### 2025/10/8 - v2.4.2
- 修复 ntfy 推送编码问题
- 修复配置文件缺失问题
- 优化 ntfy 推送效果
- 增加 github page 图片分段导出功能
- **更新提示**
- 建议使用【大版本更新】
<details>
<summary><strong>👉 历史更新</strong></summary>

View File

@ -1 +1 @@
2.4.1
2.4.2