-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodonotion.py
64 lines (52 loc) · 1.86 KB
/
todonotion.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
from notion_client import Client
import logging
logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO)
class TodoNotion:
fill_text = {True: "X", False: ""}
todo_text = "[{}]"
def __init__(self, token, page_id):
"""
token: token to access the integration
page_id: page to be used as the python 'interpreter'
"""
self.notion = Client(auth=token)
self.page_id = page_id
def page_children(self):
# a page is a block in notion
children_object = self.notion.blocks.children.list(block_id=self.page_id)
children = children_object.get("results", [])
return children
def list_tasks(self):
"""Prints all the found todos in the page"""
children = self.page_children()
text = ""
for child in children:
if child.get("type") == "to_do":
is_check = child["to_do"]["checked"]
plain_text = child["to_do"]["text"][0]["plain_text"]
task = (
self.todo_text.format(self.fill_text[is_check]) + f" {plain_text}"
)
text += task + "\n"
print(text)
def add_new_task(self, task):
"""appends the result and the prompt"""
children = [
{
"object": "block",
"type": "to_do",
"to_do": {
"checked": False,
"text": [
{
"type": "text",
"text": {
"content": task,
},
},
],
},
}
]
self.notion.blocks.children.append(block_id=self.page_id, children=children)
logging.info(f"'{task}' added to the page")