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