Synchronize calendar events to a shadow calendar where the event summary is veiled, i.e. replaced by start and end date/time only.
This script is intended to be called periodically, e.g. by Cron, without any options. It can be run from any machine, not necessarily the Nextcloud server. Sync tokens are used to efficiently handle the synchronization.
The password for Nextcloud is stored in the script file! As a minimal precaution it is advisable to create an app password for this access.
Currently, only the event summary is replaced; any other data is copied from the original event. This can easily be changed but I do not have any appropriate use case at hand.
A persistent directory should exist with write-permissions for the user running this script; e.g. /var/lib/shadowCalDAV/ (-> configuration variable RUN_DIR). It is used to store a sync token in one file per calendar.
Permissions: this file should be accessible by its owner only (file permissions 700)
User: a dedicated user with network access is advisable.
Python module caldav in version >=11
Install it via PIP for user running this script by
$ sudo -u USER python3 -m pip install caldav
CALDAV_URL, CALDAV_USER, CALDAV_PASSWORD for CalDAV (Nextcloud) server.
TIMEZONE should be specified as None in order to use local instead of universal time.
CALENDARS is a list of pairs of "internal" and "external" (= shadow) calendars.
TREAT_ALL_EVENTS can be used to override the sync token.
RESET_SHADOW_CALENDAR cannot be implemented yet. It should allow to purge all events from the shadow calendar and do a fresh sync.
The summary will be replaced by a string in the form of
<BUSY_TEXT><StartDateTime*><FROM_TO_SEPARATOR><EndDateTime*>
BUSY_TEXT, FROM_TO_SEPARATOR are constant strings; add whitespace if desired.
DATE_FORMAT, DATETIME_FORMAT are format codes in strftime syntax.
-
Sanity check about access permissions of this script file.
-
Connect to CalDAV root ("principal") on CalDAV (Nextcloud) server.
For each calendar pair of internal and external calendar:
-
Get sync token from file if possible.
-
Load all new events from internal calendar as per sync token or all events.
For each such event:
-
Copy event to external calendar, replacing its summary by start/end date/time while keeping its UID. If an event with that UID already exists update it appropriately.
-
In case of deleted objects, load all events from external calendar and delete the superfluous ones.
For each calendar pair:
-
Store new sync token.