之前用的免费rss推送到telegram服务,api次数限制越来越严,就想着自己写一个推送。
想着用chatgpt写一个,之前写代码问题很多,也没有抱太大的希望能写出什么东西。没想到这玩意儿进步也挺大的,倒是没有什么报错,能跑。当然它写的东西,不是百分百拿来就用,有些内容可以根据自己的情况修改,或者让他修改。
一写简单的代码交给gpt还是可行的。
依赖:
pip install feedparser python-telegram-bot schedule
python脚本:
import feedparser
import schedule
import time
from telegram import Bot
#Telegram Bot Token
bot_token = "xxxxxxxxxx"
#你的用户id或者频道id
chat_id = "xxxxxxxx"
# Store the last fetched RSS entries
last_entries = []
def get_latest_rss_entries(rss_url, num_entries=1):
feed = feedparser.parse(rss_url)
latest_entries = []
for entry in feed.entries[:num_entries]:
title = entry.title
link = entry.link
published = entry.published
summary = entry.summary
latest_entries.append({
"title": title,
"link": link,
"published": published,
"summary": summary
})
return latest_entries
def send_rss_entries_to_telegram(entries):
bot = Bot(token=bot_token)
for entry in entries: #发送内容
message = (
f"{entry['title']}\n"
f"{entry['link']}\n"
# f"Published: {entry['published']}\n"
# f"Summary: {entry['summary']}"
)
bot.send_message(chat_id=chat_id, text=message)
def fetch_and_send_rss():
global last_entries
rss_url = "https://zsxwz.com/feed/" #rss地址,修改成自己的
latest_entries = get_latest_rss_entries(rss_url)
if latest_entries != last_entries:
send_rss_entries_to_telegram(latest_entries)
last_entries = latest_entries
# 每分钟检查一下rss,当然也可以修改成更长时间
schedule.every(1).minutes.do(fetch_and_send_rss)
# Keep the program running
while True:
schedule.run_pending()
time.sleep(1)
上一篇:
总结几个实用工具的管理面板下一篇:
webdav服务搭建