From: Maximilian Friedersdorff Date: Sun, 22 Dec 2019 20:52:58 +0000 (+0000) Subject: Implement cron based repeated reminders X-Git-Url: https://git.friedersdorff.com/?a=commitdiff_plain;h=3eef533fbe1e2e7fc476d92fd6ce31404d77940d;p=max%2Fremindme.git Implement cron based repeated reminders --- diff --git a/remindme.py b/remindme.py index 3454eab..7bea3f0 100644 --- a/remindme.py +++ b/remindme.py @@ -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())