67 lines
2.1 KiB
Python
Executable File
67 lines
2.1 KiB
Python
Executable File
import requests
|
|
from datetime import datetime, timedelta
|
|
from ics import Calendar
|
|
from telegram import Bot
|
|
import asyncio
|
|
from pytz import timezone
|
|
|
|
from dotenv import load_dotenv
|
|
import os
|
|
load_dotenv()
|
|
|
|
WEB_DAV_URL=os.environ.get("CAL_URL")
|
|
TELEGRAM_TOKEN=os.environ.get("TOKEN")
|
|
GROUP_ID=os.environ.get("GROUP_ID")
|
|
|
|
# Función para obtener eventos del día
|
|
def obtener_eventos_del_dia():
|
|
# Descargar el archivo .ics
|
|
response = requests.get(WEB_DAV_URL)
|
|
response.raise_for_status()
|
|
calendario = Calendar(response.text)
|
|
|
|
# Definir inicio y fin del día
|
|
hoy = datetime.now()
|
|
inicio_dia = datetime(hoy.year, hoy.month, hoy.day, 0, 0, 0)
|
|
fin_dia = inicio_dia + timedelta(days=1)
|
|
|
|
eventos_hoy = []
|
|
for event in calendario.events:
|
|
# La librería ics parsea fechas en timezone-aware o naive
|
|
# Convertir a datetime sin timezone para comparación
|
|
start = event.begin.replace(tzinfo=None)
|
|
end = event.end.replace(tzinfo=None)
|
|
if start >= inicio_dia.astimezone(timezone('UTC')) and start < fin_dia.astimezone(timezone('UTC')):
|
|
eventos_hoy.append(event)
|
|
return eventos_hoy
|
|
|
|
# Función para crear mensaje con eventos
|
|
def crear_mensaje_eventos(eventos):
|
|
if not eventos:
|
|
print("No hay eventos para hoy")
|
|
return "No hay eventos programados para hoy."
|
|
mensaje = "Eventos para hoy:\n\n"
|
|
for event in eventos:
|
|
start_time = event.begin.strftime('%H:%M')
|
|
print(event)
|
|
titulo = event.name
|
|
descripcion= event.description
|
|
ubicacion= event.location
|
|
mensaje += f"-- {start_time}: {titulo}\n\n"
|
|
mensaje += f"Lugar: {ubicacion}\n\n"
|
|
mensaje += f"{descripcion}\n\n"
|
|
print(mensaje)
|
|
return mensaje
|
|
|
|
async def main():
|
|
# Obtener eventos del día
|
|
eventos = obtener_eventos_del_dia()
|
|
mensaje = crear_mensaje_eventos(eventos)
|
|
|
|
# Enviar mensaje al grupo de Telegram
|
|
bot = Bot(token=TELEGRAM_TOKEN)
|
|
await bot.send_message(chat_id=GROUP_ID, text=mensaje)
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|