-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice2.py
51 lines (40 loc) · 1.63 KB
/
service2.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
# Use this code if you want to create a systemd service.
# creating a daemon as in daemon.py will not work with systemd
# this works. If it stops working, the problem is related to something else.
import time
import subprocess
import datetime
import json
import os
script_dir = os.path.dirname(os.path.abspath(__file__))
reminders_path = os.path.join(script_dir, 'reminders.json')
def send_notif(event, days):
"""see man notify-send for more options"""
subprocess.run(["notify-send", '-u', 'normal','-t', '10000', f"{event} is in {days} days"])
# check if we need to remind the user of an upcoming event
# events = {"xmas": {"date": "2023-12-25", "reminders": 10}}
# the problem with this code is that it will continue issuing notifications every 10secs for the same events
# it would be better if it only did it once or every so often until the user takes an action
# I am thinking of deleting the reminder from the dictionary once the notification is issued.
while True:
try:
with open(reminders_path, 'r') as f:
rem = f.read()
except Exception as e:
print('No reminders yet')
print(e)
exit(1)
# parse date
reminders = json.loads(rem)
for r in reminders.items():
print(r)
event_date = datetime.datetime.strptime(r[1]["date"], "%Y-%m-%d").date()
today = datetime.date.today()
reminder = r[1]["reminders"]
delta = event_date - today
# check if we need to issue a reminder
if delta.days == reminder:
event_name = r[0].upper()
send_notif(event_name, delta.days)
time.sleep(5)
time.sleep(20)