calcurse_reminders.py (2109B)
1 #!/usr/bin/env python3 2 import subprocess 3 import time 4 import sys 5 6 warn = int(sys.argv[1]) 7 8 def get(command): 9 return subprocess.check_output(command).decode("utf-8") 10 11 def convert(t): 12 # convert set time into a calculate- able time 13 return [int(n) for n in t.split(":")] 14 15 def calc_diff(t_curr, t_event): 16 # calculate time span 17 diff_hr = (t_event[0] - t_curr[0])*60 18 diff_m = t_event[1] - t_curr[1] 19 return diff_hr + diff_m 20 21 def cleanup(done, newlist): 22 # clean up "done" -lists 23 for item in done: 24 if not item in newlist: 25 done.remove(item) 26 return done 27 28 def show_time(event, s = ""): 29 # show scheduled event 30 hrs = str(event[0][0]); mnts = str(event[0][1]) 31 mnts = "0"+mnts if len(mnts) != 2 else mnts 32 subprocess.call(["notify-send", "--urgency=critical", s, "At "+hrs+":"+mnts+" - "+event[1]]) 33 34 startups = []; times = [] 35 36 while True: 37 currtime = convert(time.strftime("%H:%M")) 38 events = [l.strip() for l in get(["calcurse", "-a"]).splitlines()][1:] 39 # arrange event data: 40 groups = []; sub = [] 41 for l in events: 42 if l.startswith("* "): 43 groups.append([l.replace("* ", "")]) 44 elif l.startswith("- "): 45 sub.append(convert(l.split("->")[0].replace("-", "").strip())) 46 else: 47 groups.append(sub+[l]) 48 sub = [] 49 # run notifications 50 for item in groups: 51 # on startup: 52 if not item in startups: 53 # all- day events 54 if len(item) == 1: 55 subprocess.call(["notify-send", "--urgency=critical", "Today - "+item[0]]) 56 # time- specific events 57 elif len(item) == 2: 58 show_time(item, "Appointment") 59 startups.append(item) 60 # as a reminder: 61 if all([len(item) == 2, not item in times]): 62 span = calc_diff(currtime, item[0]) 63 if span <= warn: 64 show_time(item, "[Reminder]") 65 times.append(item) 66 # clean up events 67 startups = cleanup(startups, groups); times = cleanup(times, groups) 68 time.sleep(30)