]> git.friedersdorff.com Git - max/remindme.git/commitdiff
Implement cron based repeated reminders
authorMaximilian Friedersdorff <max@friedersdorff.com>
Sun, 22 Dec 2019 20:52:58 +0000 (20:52 +0000)
committerMaximilian Friedersdorff <max@friedersdorff.com>
Sun, 22 Dec 2019 20:52:58 +0000 (20:52 +0000)
remindme.py

index 3454eab37722b653f10f81cad9574bd33af84bfe..7bea3f0ac72737034a6b4a2bddda6e014b8bce90 100644 (file)
@@ -1,4 +1,5 @@
 import os
+import sys
 from smtplib import SMTP_SSL
 import email
 import datetime
@@ -6,6 +7,7 @@ import datetime
 from imapclient import IMAPClient, SEEN
 from dateutil.parser import parse
 from cron_descriptor import get_description
+from croniter import croniter
 
 
 IMAP_USER = os.environ["REMINDME_IMAP_USER"]
@@ -17,7 +19,6 @@ SMTP_HOST = os.environ.get("REMINDME_IMAP_HOST", IMAP_HOST)
 
 USAGE = ""
 
-
 __iclient = None
 
 
@@ -68,10 +69,21 @@ def ack_reminder(uid, reminder):
     return make_reply(reminder, f"Received Reminder: {reminder['subject']}", reply_body)
 
 
-def remind(reminder):
+def remind(last_time, start_time, reminder):
     f_line = reminder.get_payload().strip().splitlines()[0]
     if f_line.lower().startswith("repeat"):
-        return False, None
+        cron_payload = f_line[6:].strip()
+        it = croniter(cron_payload, last_time)
+        next_time = it.get_next(datetime.datetime)
+        if next_time > last_time and next_time <= start_time:
+            return (
+                True,
+                make_reply(
+                    reminder, f"Re: {reminder['subject']} now", reminder.get_payload()
+                ),
+            )
+        else:
+            return False, None
     else:
         try:
             parsed_time = parse(f_line, fuzzy=True)
@@ -92,7 +104,7 @@ def remind(reminder):
             )
 
 
-def main():
+def main(last_time, start_time):
     msgs = iclient().search(["ALL"])
     new_rems, old_rems, metas, to_delete, to_send = [], [], [], [], []
     for uid, data in iclient().fetch(msgs, ["BODY.PEEK[]", "FLAGS"]).items():
@@ -120,7 +132,7 @@ def main():
         to_send.append(ack_reminder(uid, reminder))
 
     for uid, reminder in old_rems + new_rems:
-        done, reply = remind(reminder)
+        done, reply = remind(last_time, start_time, reminder)
         if reply:
             to_send.append(reply)
         if done:
@@ -137,4 +149,9 @@ def main():
 
 
 if __name__ == "__main__":
-    main()
+    last_time = datetime.datetime.fromtimestamp(
+        float(sys.argv[1]), datetime.timezone.utc
+    )
+    start_time = datetime.datetime.now(datetime.timezone.utc)
+    main(last_time, start_time)
+    print(start_time.timestamp())