diff --git a/src/cloud_util/notion.py b/src/cloud_util/notion.py index f4767b2..0e62e63 100644 --- a/src/cloud_util/notion.py +++ b/src/cloud_util/notion.py @@ -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, + ) diff --git a/src/helper/__init__.py b/src/helper/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/helper/poo_recorder/__init__.py b/src/helper/poo_recorder/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/helper/poo_recorder/old_poo_record.txt b/src/helper/poo_recorder/old_poo_record.txt new file mode 100644 index 0000000..6c6a56f --- /dev/null +++ b/src/helper/poo_recorder/old_poo_record.txt @@ -0,0 +1,40 @@ +12:50 | Thu, 20 Jun 🟢 Healthy +08:22 | Fri, 21 Jun 🟢 Healthy +22:06 | Fri, 21 Jun 🟢 Healthy +13:21 | Sat, 22 Jun 🟢 Healthy +22:20 | Sat, 22 Jun 🟢 Healthy +14:09 | Sun, 23 Jun 🟢 Healthy +08:04 | Mon, 24 Jun 🟢 Healthy +17:39 | Mon, 24 Jun 🟡 Soft +08:12 | Tue, 25 Jun 🟢 Healthy +21:40 | Tue, 25 Jun 🟡 Soft +21:10 | Wed, 26 Jun 🟢 Healthy +14:30 | Thu, 27 Jun 🟢 Healthy +08:00 | Fri, 28 Jun 🟢 Healthy +22:42 | Fri, 28 Jun 🟢 Healthy +12:32 | Sat, 29 Jun 🟢 Healthy +10:40 | Sun, 30 Jun 🟢 Healthy +17:30 | Sun, 30 Jun 🟢 Healthy +14:30 | Mon, 01 Jul 🟢 Healthy +21:31 | Mon, 01 Jul 🟢 Healthy +13:48 | Tue, 02 Jul 🟢 Healthy +09:31 | Wed, 03 Jul 🟢 Healthy +17:55 | Wed, 03 Jul 🟢 Healthy +18:06 | Thu, 04 Jul 🟢 Healthy +12:18 | Thu, 04 Jul 🟢 Healthy +15:03 | Fri, 05 Jul 🟢 Healthy +22:07 | Fri, 05 Jul 🟢 Healthy +16:35 | Sat, 06 Jul 🟢 Healthy +17:05 | Sun, 07 Jul 🟢 Healthy +13:38 | Mon, 08 Jul 🟢 Healthy +22:30 | Tue, 09 Jul 🟢 Healthy +14:15 | Tue, 09 Jul 🟢 Healthy +21:59 | Wed, 10 Jul 🟢 Healthy +17:41 | Thu, 11 Jul 🟢 Healthy +14:06 | Fri, 12 Jul 🟢 Healthy +09:27 | Sat, 13 Jul 🟢 Healthy +22:00 | Sun, 14 Jul 🟢 Healthy +12:20 | Mon, 15 Jul 🟢 Healthy +08:09 | Tue, 16 Jul 🟤 Hard +13:20 | Tue, 16 Jul 🟢 Healthy +08:15 | Wed, 17 Jul 🟤 Hard \ No newline at end of file diff --git a/src/helper/poo_recorder/old_poo_record_importer.py b/src/helper/poo_recorder/old_poo_record_importer.py new file mode 100644 index 0000000..5616e41 --- /dev/null +++ b/src/helper/poo_recorder/old_poo_record_importer.py @@ -0,0 +1,41 @@ +import asyncio +import datetime +from pathlib import Path + +from src.cloud_util.notion import NotionAsync +from src.config import Config + +Config.init() + +notion = NotionAsync(token=Config.get_env("NOTION_TOKEN")) + +current_file_path = Path(__file__).resolve() + +current_dir = str(current_file_path.parent) + + +rows: list[str] = [] + + +async def update_rows() -> None: + header: dict = await notion.get_block_children(block_id=Config.get_env("POO_RECORD_NOTION_TABLE_ID"), page_size=1) + header_id = header["results"][0]["id"] + with Path.open(current_dir + "/old_poo_record.txt") as file: + content = file.read() + rows = content.split("\n") + rows.reverse() + + for row in rows: + t = row[0:5] + date = row[8:19] + formatted_date = datetime.datetime.strptime(date, "%a, %d %b").astimezone().replace(year=2024).strftime("%Y-%m-%d") + status = row[20:] + print(f"{formatted_date} {t} {status}") + await notion.append_table_row_text( + table_id=Config.get_env("POO_RECORD_NOTION_TABLE_ID"), + text_list=[formatted_date, t, status, "0,0"], + after=header_id, + ) + + +asyncio.run(update_rows()) diff --git a/src/recorder/poo.py b/src/recorder/poo.py index 4824000..61d0c47 100644 --- a/src/recorder/poo.py +++ b/src/recorder/poo.py @@ -34,6 +34,7 @@ class PooRecorder: async def _note(self, now: datetime, status: str, latitude: str, longitude: str) -> None: formatted_date = now.strftime("%Y-%m-%d") formatted_time = now.strftime("%H:%M") + status.strip() await self._notion.append_table_row_text(self._table_id, [formatted_date, formatted_time, status, latitude + "," + longitude]) async def record(self, record_detail: RecordField) -> None: