horoszkop.py V1.1
from sopel import plugin
import requests
from bs4 import BeautifulSoup
import time
csillagjegyek = {
"vizonto": "♒",
"bika": "♉",
"bak": "♑",
"kos": "♈",
"ikrek": "♊",
"rak": "♋",
"oroszlan": "♌",
"szuz": "♍",
"merleg": "♎",
"skorpio": "♏",
"nyilas": "♐",
"halak": "⛎",
}
@plugin.rule(r'\[(\S+)\]!horoszkop ?(.*)')
@plugin.rule(r'\[(\S+)\]! horoszkop ?(.*)')
@plugin.command('horoszkop')
@plugin.example('!horoszkop bika', '!horoszkop szuz napi', '!horoszkop kos hétvégi')
def horoszkop(bot, trigger):
args = trigger.group(2).split() if trigger.group(2) else []
if not args:
bot.say("Helytelen, probáld meg így: !horoszkop bika napi, !horoszkop bika heti, !horoszkop bika havi vagy !horoszkop bika hétvégi.")
return
csillagjegy = args[0].lower()
tipus = args[1].lower() if len(args) > 1 else "napi" # Alapértelmezett típus: napi
# Kezeljük a különböző írásmódokat a csillagjegyek esetén
if csillagjegy in ["szüz", "szűz", "szúz"]:
csillagjegy = "szuz" # Átírjuk a szótárhoz illeszkedő formára
if csillagjegy in ["vizöntő", "vizöntö", "vizőntő", "vizőntö"]:
csillagjegy = "vizonto" # Átírjuk a szótárhoz illeszkedő formára
elif csillagjegy == "rák":
csillagjegy = "rak" # Átírjuk a szótárhoz illeszkedő formára
elif csillagjegy == "oroszlán":
csillagjegy = "oroszlan"
elif csillagjegy == "mérleg":
csillagjegy = "merleg"
elif csillagjegy == "kós":
csillagjegy = "kos"
if csillagjegy not in csillagjegyek:
bot.say("Ismeretlen csillagjegy. Használhatók: " + ", ".join(csillagjegyek.keys()))
return
# Kezeljük a különböző írásmódokat a típusok esetén
if tipus not in ["napi", "heti", "havi", "hetvegi", "hétvégi"]:
bot.say(f"Ismeretlen típus '{tipus}'. Alapértelmezett napi horoszkóp lesz lekérve.")
tipus = "napi"
elif tipus == "hétvégi":
tipus = "hetvegi" # Átírjuk a szótárhoz illeszkedő formára
horoszkop_data = get_horoszkop(csillagjegy, tipus)
if horoszkop_data:
chunks = split_into_chunks(horoszkop_data)
for chunk in chunks:
bot.say(f"*{csillagjegy.capitalize()}* (*{csillagjegyek.get(csillagjegy, '')}*) *{tipus.capitalize()}*: _{chunk}_")
time.sleep(1) # Wait for 1 second before sending the next message
else:
bot.say("Nem sikerült lekérni a horoszkópot. Próbáld újra később.")
def split_into_chunks(text, max_length=350):
chunks = []
current_chunk = ""
sentences = text.split(". ")
for sentence in sentences:
if len(current_chunk) + len(sentence) <= max_length:
current_chunk += (sentence + ". ")
else:
chunks.append(current_chunk.strip())
current_chunk = sentence + ". "
if current_chunk:
chunks.append(current_chunk.strip())
return chunks
def get_horoszkop(csillagjegy, tipus):
url = f"https://nlc.hu/horoszkop/{csillagjegy}/"
try:
response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
titles = {
"napi": "Napi horoszkóp",
"heti": "Heti horoszkóp",
"havi": "Havi horoszkóp",
"hetvegi": "Hétvégi horoszkóp"
}
title = titles.get(tipus)
if not title:
return None
horoscope_section = soup.find("h2", text=title)
if horoscope_section:
horoscope_content = horoscope_section.find_next("span").get_text(strip=True)
return horoscope_content
except requests.RequestException as e:
print(f"Error fetching horoscope: {e}")
return None