gpt 3.5 写的 rss推送电报频道,python脚本 脚本

zsxwz 23/11.23 15:46 461

gpt3.5写简单的脚本,多数时候也是可以用的。

 

import feedparser
from telegram import Bot, ParseMode
from telegram.error import TelegramError
import schedule
import time

def get_latest_rss(url):
    feed = feedparser.parse(url)
    if feed.entries:
        latest_entry = feed.entries[0]
        return latest_entry.title, latest_entry.link
    else:
        return None, None

def send_to_telegram(bot_token, chat_id, message):
    bot = Bot(token=bot_token)
    try:
        bot.send_message(chat_id=chat_id, text=message, parse_mode=ParseMode.MARKDOWN)
    except TelegramError as e:
        print(f"Error sending message to Telegram: {e}")

def read_last_sent_link(file_path):
    try:
        with open(file_path, 'r') as file:
            return file.read().strip()
    except FileNotFoundError:
        return None

def write_last_sent_link(file_path, link):
    with open(file_path, 'w') as file:
        file.write(link)

def check_and_send(rss_url, telegram_bot_token, telegram_chat_id, last_sent_link_file):
    last_sent_link = read_last_sent_link(last_sent_link_file)
    latest_title, latest_link = get_latest_rss(rss_url)

    if latest_link and latest_link != last_sent_link:
        message = f"*{latest_title}*\n[Read more]({latest_link})"
        send_to_telegram(telegram_bot_token, telegram_chat_id, message)
        write_last_sent_link(last_sent_link_file, latest_link)
    else:
        print("No new content to send.")

if __name__ == "__main__":
    rss_url = "Rss地址"
    telegram_bot_token = "机器人bot token"
    telegram_chat_id = "频道id-10xxxx"
    last_sent_link_file = "last_sent_link.txt"

    # 初始检查一次
    check_and_send(rss_url, telegram_bot_token, telegram_chat_id, last_sent_link_file)

    # 设置定时任务,每分钟检查一次
    schedule.every(1).minutes.do(check_and_send, rss_url, telegram_bot_token, telegram_chat_id, last_sent_link_file)

    while True:
        schedule.run_pending()
        time.sleep(1)

 

需要的pip依赖:

pip install schedule python-telegram-bot feedparser

 


上一篇:多播放源自动采集在线影视cms系统
下一篇:电报解除双向联系人限制 申述视频教程
最新回复 (0)
    • 姿势论坛—姿势小王子
      2
返回
发新帖
友情链接
免责声明:本站部分资源来源于网络,如有侵权请发邮件(mail@zsxwz.com)告知我们,我们将会在24小时内处理。