2024-08-16 16:57:25 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from dataclasses import asdict, dataclass, field
|
|
|
|
|
|
|
|
|
|
from notion_client import AsyncClient as Client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class Text:
|
|
|
|
|
content: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class RichText:
|
|
|
|
|
type: str
|
|
|
|
|
href: str | None = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class RichTextText(RichText):
|
|
|
|
|
type: str = "text"
|
|
|
|
|
text: Text = field(default_factory=lambda: Text(content=""))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NotionAsync:
|
|
|
|
|
def __init__(self, token: str) -> None:
|
|
|
|
|
self._client = Client(auth=token)
|
|
|
|
|
|
|
|
|
|
def update_token(self, token: str) -> None:
|
|
|
|
|
self._client.aclose()
|
|
|
|
|
self._client = Client(auth=token)
|
|
|
|
|
|
2024-08-26 16:45:36 +02:00
|
|
|
async def get_block(self, block_id: str) -> dict:
|
|
|
|
|
return await self._client.blocks.retrieve(block_id=block_id)
|
|
|
|
|
|
|
|
|
|
async def get_block_children(self, block_id: str, start_cursor: str | None = None, page_size: int = 100) -> dict:
|
|
|
|
|
return await self._client.blocks.children.list(
|
|
|
|
|
block_id=block_id,
|
|
|
|
|
start_cursor=start_cursor,
|
|
|
|
|
page_size=page_size,
|
|
|
|
|
)
|
|
|
|
|
|
2024-08-16 16:57:25 +02:00
|
|
|
async def block_is_table(self, block_id: str) -> bool:
|
2024-08-26 16:45:36 +02:00
|
|
|
block: dict = await self.get_block(block_id=block_id)
|
2024-08-16 16:57:25 +02:00
|
|
|
return block["type"] == "table"
|
|
|
|
|
|
|
|
|
|
async def get_table_width(self, table_id: str) -> int:
|
|
|
|
|
table = await self._client.blocks.retrieve(block_id=table_id)
|
|
|
|
|
return table["table"]["table_width"]
|
|
|
|
|
|
2024-08-26 16:45:36 +02:00
|
|
|
async def append_table_row_text(self, table_id: str, text_list: list[str], after: str | None = None) -> None:
|
2024-08-16 16:57:25 +02:00
|
|
|
cells: list[RichText] = []
|
|
|
|
|
for content in text_list:
|
|
|
|
|
cells.append([asdict(RichTextText(text=Text(content)))]) # noqa: PERF401
|
2024-08-26 16:45:36 +02:00
|
|
|
await self.append_table_row(table_id=table_id, cells=cells, after=after)
|
2024-08-16 16:57:25 +02:00
|
|
|
|
2024-08-26 16:45:36 +02:00
|
|
|
async def append_table_row(self, table_id: str, cells: list[RichText], after: str | None = None) -> None:
|
2024-08-16 16:57:25 +02:00
|
|
|
if not await self.block_is_table(table_id):
|
|
|
|
|
return
|
|
|
|
|
table_width = await self.get_table_width(table_id=table_id)
|
|
|
|
|
if table_width != len(cells):
|
|
|
|
|
return
|
2024-08-26 16:45:36 +02:00
|
|
|
children = [
|
|
|
|
|
{
|
|
|
|
|
"object": "block",
|
|
|
|
|
"type": "table_row",
|
|
|
|
|
"table_row": {
|
|
|
|
|
"cells": cells,
|
2024-08-16 16:57:25 +02:00
|
|
|
},
|
2024-08-26 16:45:36 +02:00
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
if after is None:
|
|
|
|
|
await self._client.blocks.children.append(
|
|
|
|
|
block_id=table_id,
|
|
|
|
|
children=children,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
await self._client.blocks.children.append(
|
|
|
|
|
block_id=table_id,
|
|
|
|
|
children=children,
|
|
|
|
|
after=after,
|
|
|
|
|
)
|