feat: Enhance calendar selection by adding URL normalization and improved error handling

This commit is contained in:
2026-06-08 16:27:00 +02:00
parent 4ba6450c43
commit a4e86e837c
+26 -4
View File
@@ -25,6 +25,20 @@ HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Unraid-Sync/1.0" "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Unraid-Sync/1.0"
} }
def normalize_url(url):
"""Normalize URLs to make matching robust against trailing slash differences."""
return str(url).strip().rstrip("/")
def find_calendar_by_url(calendars, target_url):
"""Find a calendar whose URL matches the configured Baikal calendar URL."""
normalized_target = normalize_url(target_url)
for cal in calendars:
if normalize_url(cal.url) == normalized_target:
return cal
return None
def delete_event(event): def delete_event(event):
"""Helper function to delete a single event.""" """Helper function to delete a single event."""
try: try:
@@ -107,14 +121,22 @@ def sync():
print(f" - {c.url}") print(f" - {c.url}")
return return
else: else:
# Si no hay ID, usar el primero como antes # Si no hay CALENDAR_ID, seleccionar por BAIKAL_URL
if calendars: if not calendars:
calendar = calendars[0]
else:
print("!!! No se encontró ningún calendario en esa URL.") print("!!! No se encontró ningún calendario en esa URL.")
return return
calendar = find_calendar_by_url(calendars, BAIKAL_URL)
if not calendar:
print("!!! Error: No se encontró un calendario que coincida con BAIKAL_URL.")
print(f" BAIKAL_URL configurado: {BAIKAL_URL}")
print(" Calendarios disponibles:")
for c in calendars:
print(f" - {c.url}")
return
print(f"-> Calendario seleccionado: {calendar}") print(f"-> Calendario seleccionado: {calendar}")
print(f"-> URL del calendario seleccionado: {calendar.url}")
# 3. Borrar eventos antiguos (NUEVO) # 3. Borrar eventos antiguos (NUEVO)
delete_all_events(calendar) delete_all_events(calendar)