myircbot/plugins/news.py
2025-05-29 22:58:53 +02:00

68 lines
2.4 KiB
Python

import feedparser
import time
import logging
import threading
ARCH_NEWS_FEED = "https://archlinux.org/feeds/news/"
LAST_NEWS_TITLE = None
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
class NewsPlugin:
"""Plugin para obtener las últimas noticias de Arch Linux."""
def fetch_latest_news(self):
"""Obtiene la última noticia del feed de Arch Linux."""
try:
feed = feedparser.parse(ARCH_NEWS_FEED)
if not feed.entries:
return " No se encontraron noticias."
latest_news = feed.entries[0]
title = latest_news.title
link = latest_news.link
logging.info(f"[NEWS] Última noticia obtenida: {title} - {link}")
return f" {title} - {link}"
except Exception as e:
logging.error(f" Error al obtener noticias: {e}")
return " Error al obtener noticias."
def announce_news(self, send_message, bot):
"""Monitorea el feed de noticias y envía actualizaciones periódicas."""
global LAST_NEWS_TITLE
while True:
try:
# Verifica si el bot está conectado antes de enviar
if bot.authenticated and bot.joined:
latest_news = self.fetch_latest_news()
if latest_news.startswith(""):
title = latest_news.split(" - ")[0][2:].strip()
if title != LAST_NEWS_TITLE:
LAST_NEWS_TITLE = title
send_message(f"PRIVMSG #testtest :{latest_news}")
time.sleep(600) # Verifica cada 10 minutos
except Exception as e:
logging.error(f" Error en announce_news: {e}")
time.sleep(60) # Espera 1 minuto antes de intentar de nuevo
def run(self, sender=None, *args):
"""Ejecuta el comando .news para mostrar las últimas noticias."""
return self.fetch_latest_news()
def help(self):
"""Muestra ayuda para el comando .news"""
return " Usa `.news` para obtener las últimas noticias de Arch Linux."
# Iniciar el monitoreo de noticias en un hilo separado cuando se carga el plugin
def start_news_monitoring(send_message, bot):
plugin = NewsPlugin()
thread = threading.Thread(target=plugin.announce_news, args=(send_message, bot), daemon=True)
thread.start()
return plugin