library/calendar_sync.py aktualisiert
This commit is contained in:
@ -7,17 +7,13 @@ import requests
|
||||
from urllib.parse import urlparse
|
||||
|
||||
def fetch_ics_events(url, user=None, password=None):
|
||||
"""Lädt Events aus einer ICS-URL"""
|
||||
try:
|
||||
auth = (user, password) if user and password else None
|
||||
response = requests.get(url, auth=auth)
|
||||
response = requests.get(url, auth=(user, password) if user else None)
|
||||
response.raise_for_status()
|
||||
|
||||
ics_data = response.text
|
||||
cal = vobject.readOne(ics_data)
|
||||
cal = vobject.readOne(response.text)
|
||||
return [comp for comp in cal.components() if comp.name == 'VEVENT']
|
||||
except Exception as e:
|
||||
raise Exception(f"ICS-Download fehlgeschlagen: {str(e)}")
|
||||
raise Exception(f"ICS-Fehler: {str(e)}")
|
||||
|
||||
def connect_caldav(url, user=None, password=None, verify_ssl=True):
|
||||
try:
|
||||
@ -40,15 +36,12 @@ def connect_caldav(url, user=None, password=None, verify_ssl=True):
|
||||
raise Exception(f"CalDAV-Fehler: {str(e)}")
|
||||
|
||||
def sync_ics_to_caldav(module):
|
||||
"""Haupt-Synchronisationslogik"""
|
||||
# ICS-Events holen
|
||||
ics_events = fetch_ics_events(
|
||||
module.params['source_url'],
|
||||
module.params['source_user'],
|
||||
module.params['source_password']
|
||||
)
|
||||
|
||||
# CalDAV-Ziel verbinden
|
||||
target_cal = connect_caldav(
|
||||
module.params['target_url'],
|
||||
module.params['target_user'],
|
||||
@ -56,35 +49,32 @@ def sync_ics_to_caldav(module):
|
||||
module.params['verify_ssl']
|
||||
)
|
||||
|
||||
# Existierende Events mappen
|
||||
existing_events = {
|
||||
event.icalendar_component['UID']: event
|
||||
event.icalendar_component.uid.value: event # Korrektur hier
|
||||
for event in target_cal.events()
|
||||
}
|
||||
|
||||
changed = False
|
||||
results = {'added': [], 'updated': [], 'removed': []}
|
||||
|
||||
# Neue/aktualisierte Events syncen
|
||||
for vevent in ics_events:
|
||||
uid = str(vevent['UID'].value)
|
||||
for vevent in ics_events:
|
||||
if not hasattr(vevent, 'uid') or not vevent.uid.value:
|
||||
continue # Events ohne UID überspringen
|
||||
uid = str(vevent.uid.value)
|
||||
ical_data = vevent.serialize()
|
||||
|
||||
if uid not in existing_events:
|
||||
target_cal.add_event(ical_data)
|
||||
results['added'].append(uid)
|
||||
changed = True
|
||||
else:
|
||||
# Nur aktualisieren wenn unterschiedlich
|
||||
if ical_data != existing_events[uid].data:
|
||||
existing_events[uid].data = ical_data
|
||||
existing_events[uid].save()
|
||||
results['updated'].append(uid)
|
||||
changed = True
|
||||
elif ical_data != existing_events[uid].data:
|
||||
existing_events[uid].data = ical_data
|
||||
existing_events[uid].save()
|
||||
results['updated'].append(uid)
|
||||
changed = True
|
||||
|
||||
# Optionale Löschung nicht vorhandener Events
|
||||
if module.params.get('purge'):
|
||||
for uid in set(existing_events.keys()) - {str(e['UID'].value) for e in ics_events}:
|
||||
if module.params['purge']:
|
||||
for uid in set(existing_events.keys()) - {str(e.uid.value) for e in ics_events}:
|
||||
existing_events[uid].delete()
|
||||
results['removed'].append(uid)
|
||||
changed = True
|
||||
|
||||
Reference in New Issue
Block a user