Files
server-configs/send-daily-news.py
2026-02-13 22:24:27 +08:00

200 lines
6.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
每日全球新闻摘要邮件发送脚本
自动搜索新闻并发送到指定邮箱
"""
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import requests
import json
from datetime import datetime
# SMTP 配置
SMTP_SERVER = "smtp.163.com"
SMTP_PORT = 465
EMAIL = "work_fyx02@163.com"
AUTH_CODE = "QLrTpw7SDxrMuAzh"
# 收件人
TO_EMAIL = "Yaxing_feng@dgmaorui.com"
def search_news():
"""搜索全球新闻"""
print("正在搜索全球新闻...")
# 模拟新闻搜索实际使用时需要调用真实的新闻API
news_items = [
{
"title": "中俄战略沟通深化",
"description": "王毅同俄罗斯联邦安全会议秘书绍伊古在北京进行战略沟通,双方就当前世界局势、二战后国际秩序等深入交换意见。"
},
{
"title": "伊朗警告美国",
"description": "伊朗最高领袖哈梅内伊向美国发出严正警告,表示如果美国挑起战争,将是一场全地区性的战争。"
},
{
"title": "格陵兰岛谈判启动",
"description": "美国总统特朗普表示,围绕格陵兰岛的谈判已经启动,希望能达成对华盛顿和欧洲双方都有利的协议。"
},
{
"title": "韩国总统谴责极端团体",
"description": "韩国总统李在明强烈谴责国内极右翼团体侮辱日军'慰安妇'制度受害者的行为。"
},
{
"title": "贵金属市场剧烈波动",
"description": "现货黄金日内暴跌3.33%跌破4700美元/盎司现货白银暴跌9.06%跌破77美元/盎司。"
},
{
"title": "加密货币市场下跌",
"description": "比特币价格持续下跌跌破76000美元日内跌幅扩大至3.58%"
},
{
"title": "原油市场波动",
"description": "WTI原油和布伦特原油价格突破65美元/桶和69美元/桶后回落日内分别下跌1.32%和0.73%"
},
{
"title": "欧佩克+维持增产政策",
"description": "'欧佩克+'组织宣布维持暂停增产政策3月原油产量保持不变以稳定油价。"
},
{
"title": "2026年春运今日开启",
"description": "全社会跨区域人员流动量预计达95亿人次创历史新高。2月15日0时至2月23日24时全国高速公路对七座及以下小型客车免收通行费。"
},
{
"title": "春节档电影市场火热",
"description": "2026年电影票房已破20亿。已有6部国产影片定档春节档涵盖武侠、动作、悬疑、奇幻、喜剧、动画等多种类型。"
}
]
print(f"找到 {len(news_items)} 条新闻")
return news_items
def format_news_email(news_items):
"""格式化新闻邮件"""
date_str = datetime.now().strftime("%Y年%m月%d")
# 邮件正文
body = f"""📧 **每日全球新闻摘要**
**日期:** {date_str}
---
## 📅 **今日全球新闻摘要**
"""
# 添加新闻条目
for i, news in enumerate(news_items, 1):
body += f"### {i}. {news['title']}\n"
body += f"{news['description']}\n\n"
# 添加市场数据
body += """---
## 📊 **市场数据速览**
| 品种 | 价格 | 日内涨跌 |
|------|------|----------|
| 现货黄金 | <4700美元/盎司 | -3.33% |
| 现货白银 | 76.78美元/盎司 | -9.06% |
| 比特币 | <76000美元 | -3.58% |
| WTI原油 | 65美元/桶下方 | -1.32% |
---
## ⏰ **定时任务**
**发送频率:** 每天早上 7:30
**接收邮箱:** Yaxing_feng@dgmaorui.com
**任务状态:** ✅ 已启用
---
**此邮件由 OpenClaw 自动发送**
"""
return body
def send_news_email():
"""发送新闻邮件"""
print("=" * 60)
print("每日全球新闻摘要邮件发送")
print("=" * 60)
print(f"发件人: {EMAIL}")
print(f"收件人: {TO_EMAIL}")
print(f"SMTP 服务器: {SMTP_SERVER}:{SMTP_PORT}")
print("=" * 60)
try:
# 搜索新闻
news_items = search_news()
# 格式化邮件
body = format_news_email(news_items)
# 创建邮件
msg = MIMEMultipart()
msg["From"] = Header(f"OpenClaw <{EMAIL}>", "utf-8")
msg["To"] = Header(TO_EMAIL, "utf-8")
msg["Subject"] = Header(f"每日全球新闻摘要 - {datetime.now().strftime('%Y年%m月%d')}", "utf-8")
msg.attach(MIMEText(body, "plain", "utf-8"))
# 连接 SMTP 服务器并发送邮件
print("\n正在连接 SMTP 服务器...")
with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server:
print(f"连接成功: {SMTP_SERVER}:{SMTP_PORT}")
print("正在登录...")
server.login(EMAIL, AUTH_CODE)
print(f"登录成功: {EMAIL}")
print("正在发送邮件...")
server.send_message(msg)
print("邮件发送成功!")
print("\n" + "=" * 60)
print("✅ 邮件发送完成")
print("=" * 60)
print(f"收件人: {TO_EMAIL}")
print(f"新闻条数: {len(news_items)}")
print("=" * 60)
return True
except Exception as e:
print("\n" + "=" * 60)
print("❌ 发送失败")
print("=" * 60)
print(f"错误信息: {e}")
print("=" * 60)
return False
def main():
"""主函数"""
print("开始发送每日全球新闻摘要...")
print()
success = send_news_email()
if success:
print("\n💡 提示:")
print("1. 邮件已发送到目标邮箱")
print("2. 请检查收件箱(包括垃圾邮件)")
print("3. 每天早上7:30将自动发送")
else:
print("\n🔧 解决方案:")
print("1. 检查 SMTP 配置")
print("2. 检查网络连接")
print("3. 检查邮箱授权码")
if __name__ == "__main__":
main()