-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
130 lines (111 loc) · 3.86 KB
/
run.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
1. Download all tasks from Notion
2. Merge in information from Reclaim
- Sync reclaim completion status -> tasks downloaded from Notion
- If due dates postponed in Reclaim/Google Tasks, update tasks downloaded from Notion
3. Optimization
- Fix early and late starts for tasks already in Reclaim and try to solve
- If that doesn't work, unfix and re-optimize. Note that Reclaim tasks were changed
4. Update Reclaim
- If tasks already in Reclaim were changed, update them
- Add tasks not in Reclaim with scheduled early starts within one month from now
5. Update everything Notion
"""
from datetime import datetime, timedelta, time
from feazy.plan import (
optimize_schedule,
download_notion_tasks,
update_notion_tasks,
get_gtasks,
get_gtasks_service,
sync_with_gtasks,
update_gtasks,
)
import pytz
from beautiful_date import *
import logging
from typing import Optional
fmt_date = lambda d: d.astimezone(pytz.timezone("Europe/London")).strftime(
"%m/%d/%Y, %H:%M:%S"
)
def task_optimization(
notion_task_database_id: str,
reclaim_tasklist_id: str,
start_time: Optional[datetime] = None,
deadline: Optional[datetime] = None,
print_task_list=False,
):
logger = logging.getLogger(__name__)
# Download tasks from notion
logger.info("Downloading tasks from Notion")
tasks = download_notion_tasks(notion_task_database_id)
# Start time and deadline
timezone = pytz.timezone("Europe/London")
if start_time is None:
start_time = timezone.localize(datetime.today())
for task in tasks.all_tasks:
if (
task.scheduled_early_start is not None
and task.scheduled_deadline is not None
and not task.completed
):
if (
task.scheduled_early_start < start_time
and task.scheduled_deadline > start_time
):
start_time = task.scheduled_early_start
if deadline is None:
deadline = timezone.localize((Apr / 30 / 2023)[00:00])
logger.info(f"Start Time: {fmt_date(start_time)}")
logger.info(f"Deadline: {fmt_date(deadline)}")
# Merge in Gtasks/Reclaim
logger.info("Syncing Notion with Gtasks")
gservice = get_gtasks_service()
gtasks = get_gtasks(gservice, reclaim_tasklist_id)
tasks = sync_with_gtasks(
service=gservice,
reclaim_list_id=reclaim_tasklist_id,
gtasks=gtasks,
tasks=tasks,
)
# Work Times (9-5 M-F)
work_times = {
i: [time(hour=9, minute=0, second=0), time(hour=14, minute=0, second=0)]
for i in range(5)
}
work_times[5] = [
time(hour=9, minute=0, second=0),
time(hour=12, minute=0, second=0),
]
# Remove completed tasks
for task in tasks.all_tasks:
if task.completed:
tasks.remove_task(task.task_id)
# Optimize schedule
logger.info("Optimizing schedule")
new_tasks = optimize_schedule(
tasks,
work_times=work_times,
start_time=start_time,
deadline=deadline,
block_duration=timedelta(hours=1),
)
if print_task_list:
print(new_tasks)
# Update Gtasks/Reclaim
logger.info("Updating Gtasks")
new_tasks = update_gtasks(new_tasks, gservice, reclaim_tasklist_id)
# Update schedule in notion
update_notion_tasks(new_tasks, use_async=True)
if __name__ == "__main__":
start = datetime.now()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
notion_task_database_id = "89357b5cf7c749d6872a32636375b064"
reclaim_tasklist_id = "Nkw0V2wwWll6QVJ5a0hMUA"
task_optimization(
notion_task_database_id, reclaim_tasklist_id, print_task_list=True
)
end = datetime.now()
delta = (end - start).total_seconds() / 60
logging.info(f"Took {delta:.01f} minutes to run")