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

45 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
from bs4 import BeautifulSoup
import random
import re
class ChistePlugin:
"""Plugin para obtener un chiste aleatorio desde la web."""
URL = "https://chistescortos.yavendras.com/"
def obtener_chiste(self):
"""Obtiene un chiste aleatorio desde la web y lo formatea correctamente."""
try:
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}
response = requests.get(self.URL, headers=headers, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
chistes = soup.find_all("p", class_="description")
if not chistes:
return " No encontré chistes en este momento."
chiste_html = random.choice(chistes)
chiste = chiste_html.decode_contents()
# Reemplazar etiquetas <br> por saltos de línea
chiste = re.sub(r'<br\s*/?>', '\n', chiste).strip()
return f" {chiste}"
except requests.exceptions.RequestException as e:
return f" Error al obtener chiste: {str(e)}"
def run(self, sender, *args):
"""Función principal ejecutada por el bot."""
return self.obtener_chiste()
def help(self):
"""Descripción del plugin para el comando .help"""
return " Usa `.chiste` para obtener un chiste aleatorio."