45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
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."
|
||
|