-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaldav-daily-digest.py
executable file
·78 lines (63 loc) · 2.57 KB
/
caldav-daily-digest.py
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
#!/usr/bin/python3
"""calendar digest cron script
this logs into a caldav server and tries to get a digest of all of the
events scheduled for that day. naive and hacky but seems to mostly
work? easiest way to run this is from cron.
author: [email protected]
license: apache 2.0"""
from datetime import datetime, timedelta
from os import environ
from urllib.parse import quote_plus
from icalendar import Calendar
import caldav
import pytz
import sys
# you'll probably want to change these:
USERNAME = '[email protected]'
PASSWORD = 'ChAng3M3!'
BASE_URL = 'stbeehive.blahblahblah.foo/caldav/st/principals/individuals/'
# below here you probably will not need to make changes
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
URL = 'https://' + quote_plus(USERNAME) + ":" + quote_plus(PASSWORD) \
+ '@' + BASE_URL + quote_plus(USERNAME) + '/'
NOW = datetime.now()
DAY_START = datetime(NOW.year, NOW.month, NOW.day)
DAY_END = DAY_START + timedelta(hours=24)
if "TZ" in environ:
LOCAL_TZ = pytz.timezone(environ["TZ"])
else:
LOCAL_TZ = pytz.timezone("UTC")
def pretty_print_time(date_time):
"""takes a datetime and normalizes it to local time, prints nicely"""
local_dt = date_time.replace(tzinfo=pytz.utc).astimezone(LOCAL_TZ)
return local_dt.strftime("%I:%M %p")
print("pulling events for %d-%d-%d" % (NOW.year, NOW.month, NOW.day))
CLIENT = caldav.DAVClient(URL)
PRINCIPAL = CLIENT.principal()
CALENDARS = PRINCIPAL.calendars()
if not CALENDARS:
print("No calendars defined for " + USERNAME)
else:
CALENDAR = CALENDARS[0]
EVENTS = CALENDAR.date_search(DAY_START, DAY_END)
if not EVENTS:
print("Nothing on your calendar for today!")
else:
FILTERED_EVENTS = []
for ev in EVENTS:
components = Calendar.from_ical(ev.data).walk('vevent')
if not components:
continue
FILTERED_EVENTS.append(components[0])
FILTERED_EVENTS.sort(key=lambda e: e.decoded('dtstart'))
for event in FILTERED_EVENTS:
summary = event.decoded('summary').decode('utf-8')
description = ""
if event.decoded('description') != b'':
description = event.decoded('description').decode('utf-8')
start = event.decoded('dtstart')
end = event.decoded('dtend')
print(u"%s - %s %s\n%s\n\n" % (pretty_print_time(start),
pretty_print_time(end),
summary,
description))