Last active
January 20, 2022 20:33
-
-
Save jspricke/10c51a496496d409072d7bb943df85d0 to your computer and use it in GitHub Desktop.
.local/share/task/hooks/on-add/on-modify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from datetime import datetime, time, timezone | |
from json import dumps, loads | |
from os.path import expanduser | |
from sys import stdin, stdout | |
from zoneinfo import ZoneInfo | |
def parse_due(dues: str) -> datetime: | |
due = datetime.strptime(dues, "%Y%m%dT%H%M%SZ") | |
return due.replace(tzinfo=timezone.utc).astimezone(ZoneInfo("localtime")) | |
def fix_due(due: datetime) -> str: | |
due = datetime.combine(due.date(), time.min) | |
return due.astimezone(timezone.utc).strftime("%Y%m%dT%H%M%SZ") | |
def remind_remove(uuid: str) -> None: | |
with open(expanduser("~/.remind/todo.rem"), encoding="utf-8") as rem: | |
reminders = rem.readlines() | |
for num, line in enumerate(reminders): | |
if uuid in line: | |
del reminders[num] | |
del reminders[num] | |
break | |
with open(expanduser("~/.remind/todo.rem"), "w", encoding="utf-8") as rem: | |
rem.writelines(reminders) | |
def remind_add(task: dict[str, str], due: datetime) -> None: | |
if task["status"] != "pending": | |
return | |
uuid = task["uuid"] | |
date = due.strftime("%b %d %Y") | |
until = due.strftime("'%Y-%m-%d'") | |
description = task["description"] | |
line = f"# task: {uuid}\nREM {date} *1 UNTIL [ max({until}, realtoday()) ] MSG {description} (TODO)\n" | |
with open(expanduser("~/.remind/todo.rem"), "a", encoding="utf-8") as rem: | |
rem.write(line) | |
def main() -> None: | |
tasks = stdin.readline() | |
new = stdin.readline() | |
if new: | |
task = loads(tasks) | |
if "due" in task: | |
remind_remove(task["uuid"]) | |
tasks = new | |
task = loads(tasks) | |
if "due" in task: | |
due = parse_due(task["due"]) | |
task["due"] = fix_due(due) | |
remind_add(task, due) | |
stdout.write(dumps(task, ensure_ascii=False)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment