-
Notifications
You must be signed in to change notification settings - Fork 0
/
khal-notify.py
executable file
·42 lines (34 loc) · 1.43 KB
/
khal-notify.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
#!/usr/bin/env python3
"""Notify for all Khal events in advance.
According to the set NOTIFY_MINS (list of minutes in advance to get a
notification).
"""
import datetime
import re
from typing import List, Pattern
from sh import khal, notify_send
NOTIFY_MINS: List[int] = [1, 5, 15, 60, 240]
events: str = str(khal("list", "--notstarted", "today", "today", _tty_out=False))
regex: Pattern = re.compile("^([0-9]{2}:[0-9]{2})-([0-9]{2}:[0-9]{2}) (.*)$")
# regex: Pattern = re.compile("^([0-9]{2}:[0-9]{2} [APMapm]{2})-([0-9]{2}:[0-9]{2} [APMapm]{2}) (.*)$")
now: datetime.time = datetime.datetime.now().time()
for line in events.split("\n"):
matches: List[str] = regex.findall(line)
if len(matches) != 1:
continue
start: datetime.time = datetime.time(*tuple(map(int, matches[0][0].split(":"))))
end: datetime.time = datetime.time(*tuple(map(int, matches[0][1].split(":"))))
if " :: " in matches[0][2]:
title, description = matches[0][2].split(" :: ")
else:
title = matches[0][2]
remaining = datetime.datetime.combine(
datetime.date.min, start
) - datetime.datetime.combine(datetime.date.min, now)
remaining_minutes: int = round(remaining.total_seconds() / 60)
print(remaining_minutes)
if remaining_minutes in NOTIFY_MINS:
notify_send(
f"Upcoming calendar event: {title}",
f"{matches[0][0]}-{matches[0][1]} in {remaining_minutes} mins.",
)