Add helper to port notes data, imporve notion
This commit is contained in:
@@ -30,35 +30,53 @@ class NotionAsync:
|
||||
self._client.aclose()
|
||||
self._client = Client(auth=token)
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
async def block_is_table(self, block_id: str) -> bool:
|
||||
block = await self._client.blocks.retrieve(block_id=block_id)
|
||||
block: dict = await self.get_block(block_id=block_id)
|
||||
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"]
|
||||
|
||||
async def append_table_row_text(self, table_id: str, text_list: list[str]) -> None:
|
||||
async def append_table_row_text(self, table_id: str, text_list: list[str], after: str | None = None) -> None:
|
||||
cells: list[RichText] = []
|
||||
for content in text_list:
|
||||
cells.append([asdict(RichTextText(text=Text(content)))]) # noqa: PERF401
|
||||
await self.append_table_row(table_id=table_id, cells=cells)
|
||||
await self.append_table_row(table_id=table_id, cells=cells, after=after)
|
||||
|
||||
async def append_table_row(self, table_id: str, cells: list[RichText]) -> None:
|
||||
async def append_table_row(self, table_id: str, cells: list[RichText], after: str | None = None) -> None:
|
||||
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
|
||||
await self._client.blocks.children.append(
|
||||
block_id=table_id,
|
||||
children=[
|
||||
{
|
||||
"object": "block",
|
||||
"type": "table_row",
|
||||
"table_row": {
|
||||
"cells": cells,
|
||||
},
|
||||
children = [
|
||||
{
|
||||
"object": "block",
|
||||
"type": "table_row",
|
||||
"table_row": {
|
||||
"cells": cells,
|
||||
},
|
||||
],
|
||||
)
|
||||
},
|
||||
]
|
||||
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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user