summaryrefslogtreecommitdiff
path: root/bin/vcalendar-filter
blob: ff3c4631fe81156b827ea30537757f3117049ba9 (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
#!/home/ANT.AMAZON.COM/crutem/calendar/bin/python3
#
# pip install icalendar
#

import sys
from datetime import datetime
from icalendar import Calendar


def format_name(address):
    if not address:
        return ""

    email = address.split(":")[1]
    if "cn" in address.params:
        return "{} <{}>".format(address.params["cn"], email)

    return email


def format_attendees(attendees):
    if isinstance(attendees, list):
        return ", ".join([format_name(a) for a in attendees])
    return format_name(attendees)


for event in Calendar.from_ical(sys.stdin.read()).walk("vevent"):
    organizer = format_name(event.get("organizer", ""))
    if organizer:
        print(f"Organizer: {organizer}")

    attendees = format_attendees(event.get("attendee"))
    if attendees:
        print(f"Attendees: {attendees}")

    summary = event.get("summary", "")
    if summary:
        print(f"Summary: {summary}")

    time_from = datetime.strftime(
        event.get("dtstart").dt, "%a %d %b %Y %H:%M")
    time_to = datetime.strftime(event.get("dtend").dt, "%H:%M")
    if time_from:
        print(f"When: {time_from}-{time_to}")

    location = event.get("location", "")
    if location:
        print(f"Location: {location}")

    comment = event.get("comment", "")
    if comment:
        print(f"Comment: {comment}")

    description = event.get("description", "")
    if description:
        print(f"Description:\n{description}")

    print("---")