summaryrefslogtreecommitdiff
path: root/config/services/dav/create-bday-cal.py
blob: 32fade910ee8e13044563317eb900e64ea66c946 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# https://github.com/iBigQ/radicale-birthday-calendar

import fileinput
import json
import os
import random
import uuid
from collections import defaultdict
from datetime import datetime, timedelta
from typing import Iterator, Any

import dateutil.rrule as rrule
import vobject

BIRTHDAY_CALENDAR_COLOR = None
BIRTHDAY_REMINDER_AT_HOUR = True


def parse_date(date_str):
    for date_fmt in ("%Y-%m-%d", "%Y%m%d", "--%m%d"):
        try:
            return datetime.strptime(date_str, date_fmt)
        except ValueError:
            pass
    raise ValueError(f"could not parse date {date_str}")


def get_changed_files() -> dict[str, list[str]]:
    user_modifications = defaultdict(list)
    for changed_file in fileinput.input():
        changed_file = changed_file.rstrip("\n")
        if (
            not changed_file
            or "/.Radicale.cache/" in changed_file
            or not changed_file.count("/") >= 3
        ):
            continue
        root, user, collection, file = changed_file.split("/", maxsplit=3)
        user_modifications[f"{root}/{user}"].append(f"{collection}/{file}")
    return user_modifications


def get_user_collections(user: str) -> dict[str, str]:
    user_collections = {}
    dirpath, dirnames, filenames = next(os.walk(user), ([], [], []))
    for collection in dirnames:
        try:
            with open(f"{user}/{collection}/.Radicale.props", "r") as fp:
                tag = json.load(fp)["tag"]
        except (FileNotFoundError, json.JSONDecodeError, KeyError):
            continue
        else:
            user_collections[collection] = tag
    return user_collections


def get_entries(collection: str) -> Iterator[tuple[str, Any]]:
    dirpath, dirnames, filenames = next(os.walk(collection), ([], [], []))
    for filename in filenames:
        if filename.startswith(".Radicale"):
            continue
        with open(f"{dirpath}/{filename}", "r") as fp:
            for entry in vobject.readComponents(fp.read()):
                yield f"{collection}/{filename}", entry


def get_birthday_calendar(user: str) -> Iterator[tuple[str, Any]]:
    collection = f"{user}/birthdays"
    if not os.path.exists(collection):
        os.makedirs(collection, exist_ok=True)
    fn = f"{collection}/.Radicale.props"
    if not os.path.exists(fn):
        with open(fn, "w") as fp:
            # (Optional) Get calendar color from environment
            if BIRTHDAY_CALENDAR_COLOR:
                # If color was set, strip '#' for consistency
                color = BIRTHDAY_CALENDAR_COLOR.strip("#")
            else:
                # Or alternatively choose something at random
                color = "".join(random.choices("0123456abcdef", k=6))

            data = {
                "C:supported-calendar-component-set": "VEVENT",
                "D:displayname": "Birthdays",
                "C:calendar-description": "[AUTO GENERATED] "
                + "Birthdays from all addressbooks",
                "ICAL:calendar-color": f"#{color}",
                "tag": "VCALENDAR",
            }
            json.dump(data, fp, indent=None)
    yield from get_entries(collection)


def create_birthday(user: str, contact):
    calendar = vobject.iCalendar()

    # Basic event properties
    event = calendar.add("vevent")
    event.add("summary").value = contact.fn.value
    event.add("dtstart").value = parse_date(contact.bday.value).date()
    event.add("dtend").value = event.dtstart.value + timedelta(days=1)
    event.add("uid").value = contact.uid.value

    # (Optional) Set reminder
    if BIRTHDAY_REMINDER_AT_HOUR:
        alarm = event.add("valarm")
        alarm.add("action").value = "DISPLAY"
        alarm.add("description").value = "Reminder"
        alarm.add("trigger").value = timedelta(hours=0)
        alarm.add("uid").value = str(uuid.uuid4())

    # Repeat yearly
    newrule = rrule.rruleset()
    newrule.rrule(rrule.rrule(rrule.YEARLY, dtstart=event.dtstart.value))
    event.rruleset = newrule

    # Write to disk
    collection = f"{user}/birthdays"
    with open(f"{collection}/{contact.uid.value}.ics", "w") as fp:
        fp.write(calendar.serialize())


def main():
    user_to_changes = get_changed_files()
    user_to_collections = {
        user: get_user_collections(user) for user in user_to_changes
    }

    for user, changed_files in user_to_changes.items():
        contacts = {
            contact.uid.value: contact
            for collection, tag in user_to_collections[user].items()
            if tag == "VADDRESSBOOK"
            for _, contact in get_entries(f"{user}/{collection}")
            if hasattr(contact, "bday")
        }
        birthday_entries = {
            user_bday.vevent.uid.value: (fn, user_bday)
            for fn, user_bday in get_birthday_calendar(user)
        }

        # Check existing birthday entries
        for bday_uid, (bday_file, bday_event) in birthday_entries.items():
            # Delete old ones
            if bday_uid not in contacts:
                os.unlink(bday_file)
            # Keep unchanged
            elif not any(
                bday_uid in changed_file for changed_file in changed_files
            ):
                del contacts[bday_uid]

        # Write new and modified contacts
        for contact in contacts.values():
            create_birthday(user, contact)


if __name__ == "__main__":
    main()