66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
import os
|
|
import requests
|
|
|
|
# API Keys
|
|
CALENDARIFIC_API_KEY = os.getenv("CALENDARIFIC_API_KEY")
|
|
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://ollama.default.svc.cluster.local:11434/api/generate")
|
|
|
|
def translate_with_ollama(text):
|
|
"""Traduce el texto en inglés a español usando Ollama, eliminando introducciones innecesarias."""
|
|
payload = {
|
|
"model": "gemma2:2b",
|
|
"prompt": f"Traduce al español este texto de manera concisa, sin introducciones: {text}",
|
|
"stream": False
|
|
}
|
|
|
|
try:
|
|
response = requests.post(OLLAMA_URL, json=payload, timeout=30)
|
|
response.raise_for_status()
|
|
translated_text = response.json().get("response", "").strip()
|
|
|
|
# Eliminar frases como "El texto en español sería:"
|
|
if translated_text.lower().startswith("el texto en español sería:"):
|
|
translated_text = translated_text.split(":", 1)[1].strip()
|
|
|
|
return translated_text
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
return f"Error al conectar con Ollama: {e}"
|
|
|
|
|
|
def get_holidays(day, month, country):
|
|
"""Consulta los festivos en Calendarific y los traduce con Ollama."""
|
|
url = f"https://calendarific.com/api/v2/holidays?api_key={CALENDARIFIC_API_KEY}&country={country}&year=2025&month={month}&day={day}"
|
|
|
|
try:
|
|
response = requests.get(url)
|
|
data = response.json()
|
|
|
|
if "error" in data:
|
|
return f"Error en Calendarific: {data['error']}"
|
|
|
|
holidays = data.get("response", {}).get("holidays", [])
|
|
if not holidays:
|
|
return f"No hay festivos en {country} el {day}/{month}."
|
|
|
|
festivos_en = "\n".join([f"- {h['name']} ({h['description']})" for h in holidays])
|
|
|
|
festivos_es = translate_with_ollama(festivos_en)
|
|
|
|
return f"Festivos en {country} el {day}/{month}:\n{festivos_es}"
|
|
|
|
except Exception as e:
|
|
return f"Error al consultar Calendarific: {e}"
|
|
|
|
def run(sender, *args):
|
|
"""Ejecuta la consulta de festivos"""
|
|
if len(args) != 3:
|
|
return "Uso: .fecha <día> <mes> <país (código ISO)>"
|
|
|
|
day, month, country = args
|
|
return get_holidays(day, month, country)
|
|
|
|
def help():
|
|
return "Uso: .fecha <día> <mes> <país (código ISO)> - Consulta si es festivo en ese país."
|
|
|