library/calendar_sync.py aktualisiert
This commit is contained in:
@ -1,6 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from caldav import DAVClient
|
||||
import vobject
|
||||
@ -11,11 +10,11 @@ def fetch_ics_events(url, user=None, password=None):
|
||||
try:
|
||||
response = requests.get(url, auth=(user, password) if user else None)
|
||||
response.raise_for_status()
|
||||
# Alle VEVENTs aus ICS-Datei extrahieren
|
||||
events = []
|
||||
for component in vobject.readComponents(response.text):
|
||||
if hasattr(component, "vevent"):
|
||||
events.append(component.vevent)
|
||||
for calendar in vobject.readComponents(response.text):
|
||||
for component in calendar.components():
|
||||
if component.name == 'VEVENT':
|
||||
events.append(component)
|
||||
return events
|
||||
except Exception as e:
|
||||
raise Exception(f"ICS-Fehler: {str(e)}")
|
||||
@ -52,21 +51,20 @@ def sync_ics_to_caldav(module):
|
||||
module.params['verify_ssl']
|
||||
)
|
||||
|
||||
# Bestehende Events analysieren
|
||||
existing_events = {}
|
||||
for event in target_cal.events():
|
||||
try:
|
||||
for vobj in vobject.readComponents(event.data):
|
||||
if hasattr(vobj, "vevent"):
|
||||
uid = str(vobj.vevent.uid)
|
||||
existing_events[uid] = event
|
||||
for calendar in vobject.readComponents(event.data):
|
||||
for component in calendar.components():
|
||||
if component.name == 'VEVENT':
|
||||
uid = str(component.uid)
|
||||
existing_events[uid] = event
|
||||
except Exception as e:
|
||||
raise Exception(f"Fehler beim Parsen eines bestehenden Events: {str(e)}")
|
||||
|
||||
changed = False
|
||||
results = {'added': [], 'updated': [], 'removed': []}
|
||||
|
||||
# Neue Events hinzufügen oder aktualisieren
|
||||
for vevent in ics_events:
|
||||
uid = str(vevent.uid)
|
||||
ical_data = vevent.serialize()
|
||||
@ -81,7 +79,6 @@ def sync_ics_to_caldav(module):
|
||||
results['updated'].append(uid)
|
||||
changed = True
|
||||
|
||||
# Entfernen von Events, wenn aktiviert
|
||||
if module.params['purge']:
|
||||
current_uids = {str(e.uid) for e in ics_events}
|
||||
for uid in set(existing_events.keys()) - current_uids:
|
||||
@ -111,5 +108,4 @@ def run_module():
|
||||
except Exception as e:
|
||||
module.fail_json(msg=str(e))
|
||||
|
||||
if __name__ == '__main__':
|
||||
run_module()
|
||||
if __name__ == '__
|
||||
|
||||
Reference in New Issue
Block a user