以下是一个基于Python的解决方案,使用RSS订阅源+Server酱推送的完整实现:
import sqlite3
import feedparser
import requests
from datetime import datetime
# 配置部分
RSS_FEEDS = [
'https://aitrends.com/feed/',
'https://www.reddit.com/r/MachineLearning/.rss',
'https://news.ycombinator.com/rss',
'https://openai.com/blog/rss/'
]
DB_NAME = 'ai_news.db'
SERVERCHAN_KEY = 'YOUR_SERVERCHAN_SCKEY' # 在Server酱官网注册获取
def init_db():
"""初始化数据库"""
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS articles
(id TEXT PRIMARY KEY, title TEXT, link TEXT, published TIMESTAMP)''')
conn.commit()
conn.close()
def get_existing_ids():
"""获取已存储的文章ID"""
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute("SELECT id FROM articles")
existing_ids = {row[0] for row in c.fetchall()}
conn.close()
return existing_ids
def parse_feeds():
"""解析所有RSS源"""
all_entries = []
for feed_url in RSS_FEEDS:
feed = feedparser.parse(feed_url)
for entry in feed.entries:
# 生成唯一ID(使用链接的MD5或其他唯一标识)
entry_id = entry.get('id', entry.link)
published = datetime(*entry.published_parsed[:6]) if hasattr(entry, 'published_parsed') else datetime.now()
all_entries.append({
'id': entry_id,
'title': entry.title,
'link': entry.link,
'published': published
})
return sorted(all_entries, key=lambda x: x['published'], reverse=True)
def send_wechat(title, content):
"""通过Server酱发送到微信"""
url = f'https://sctapi.ftqq.com/{SERVERCHAN_KEY}.send'
data = {
'title': title[:100], # 标题限制长度
'desp': content # 支持Markdown格式
}
requests.post(url, data=data)
def format_message(articles):
"""格式化消息内容"""
markdown_content = "**最新AI动态** \n更新时间:{}\n\n".format(datetime.now().strftime("%Y-%m-%d %H:%M"))
for idx, article in enumerate(articles[:10], 1): # 取最新10条
markdown_content += f"{idx}. [{article['title']}]({article['link']})\n\n"
return markdown_content
def main():
# 初始化数据库
init_db()
# 获取现有数据
existing_ids = get_existing_ids()
# 解析所有源
new_articles = []
all_entries = parse_feeds()
# 筛选新文章
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
for entry in all_entries:
if entry['id'] not in existing_ids:
# 存储新文章
c.execute("INSERT INTO articles VALUES (?, ?, ?, ?)",
(entry['id'], entry['title'], entry['link'], entry['published']))
new_articles.append(entry)
conn.commit()
conn.close()
# 如果有新文章则发送
if new_articles:
message = format_message(new_articles)
send_wechat("AI动态更新", message)
print(f"已推送{len(new_articles)}条新内容")
else:
print("无新内容")
if __name__ == "__main__":
main()
实现步骤说明:
-
技术栈选择:
- RSS解析:feedparser库
- 数据存储:SQLite
- 微信推送:Server酱(免费版支持每日100条)
- 定时任务:crontab或APScheduler
-
部署步骤:
- 注册Server酱(https://sct.ftqq.com/)获取SCKEY
- 安装依赖:
pip install feedparser requests sqlite3
- 配置RSS源列表(示例包含4个源,可自行添加)
- 设置定时任务(每天9点执行):
0 9 * * * /usr/bin/python3 /path/to/your_script.py
-
功能增强建议:
- 添加自动翻译功能(使用googletrans库)
- 增加去重机制(基于标题相似度)
- 添加异常处理(网络重试机制)
- 支持更多源类型(Twitter API、arXiv API等)
- 添加日志记录
-
扩展功能代码示例(翻译+摘要):
from googletrans import Translator
def translate_content(text, dest='zh-cn'):
"""使用Google翻译API"""
try:
translator = Translator()
return translator.translate(text, dest=dest).text
except:
return text
# 在format_message函数中添加:
translated_title = translate_content(article['title'])
# 在消息中使用translated_title
注意事项:
- Server酱免费版有频率限制,建议合并多条消息发送
- RSS源可能需要处理反爬机制(添加User-Agent)
- 需要保持服务器时钟准确(影响定时任务)
- 建议添加异常处理机制保证长期运行稳定
这个方案可以每天自动收集最新的AI动态,并通过微信推送图文消息。最终效果是在微信中收到类似这样的消息:
最新AI动态 - 2023-08-20
1. OpenAI发布新一代语言模型
2. 谷歌提出新型神经网络架构
3. MIT研究人员开发出...
(每条都是可点击的链接)