Compare commits
2 Commits
234323c766
...
python_ver
| Author | SHA1 | Date | |
|---|---|---|---|
| c814f39810 | |||
| f14b7bf104 |
@@ -35,7 +35,10 @@ class PooRecorder:
|
||||
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])
|
||||
await self._notion.append_table_row_text_after_header(
|
||||
self._table_id,
|
||||
[formatted_date, formatted_time, status, latitude + "," + longitude],
|
||||
)
|
||||
|
||||
async def record(self, record_detail: RecordField) -> None:
|
||||
webhook_id: str = Config.get_env("HOMEASSISTANT_POO_TRIGGER_ID")
|
||||
|
||||
62
src/helper/poo_recorder/poo_record_reverse.py
Normal file
62
src/helper/poo_recorder/poo_record_reverse.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import asyncio
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from src.config import Config
|
||||
from src.util.notion import NotionAsync
|
||||
|
||||
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] = []
|
||||
|
||||
|
||||
@dataclass
|
||||
class Column:
|
||||
id: str
|
||||
date: str
|
||||
time: str
|
||||
status: str
|
||||
location: str
|
||||
|
||||
|
||||
async def reverse_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"]
|
||||
start_cursor = header_id
|
||||
rows: list[Column] = []
|
||||
while start_cursor is not None:
|
||||
children: dict = await notion.get_block_children(block_id=Config.get_env("POO_RECORD_NOTION_TABLE_ID"), start_cursor=start_cursor)
|
||||
for entry in children["results"]:
|
||||
row = Column(
|
||||
id=entry["id"],
|
||||
date=entry["table_row"]["cells"][0][0]["plain_text"],
|
||||
time=entry["table_row"]["cells"][1][0]["plain_text"],
|
||||
status=entry["table_row"]["cells"][2][0]["plain_text"],
|
||||
location=entry["table_row"]["cells"][3][0]["plain_text"],
|
||||
)
|
||||
rows.append(row)
|
||||
start_cursor = children["next_cursor"]
|
||||
rows = rows[1:]
|
||||
for row in rows:
|
||||
print("delete block", row.date, row.time)
|
||||
await notion.delete_block(row.id)
|
||||
await asyncio.sleep(1)
|
||||
|
||||
for row in rows:
|
||||
print("add block", row.date, row.time)
|
||||
await notion.append_table_row_text(
|
||||
table_id=Config.get_env("POO_RECORD_NOTION_TABLE_ID"),
|
||||
text_list=[row.date, row.time, row.status, row.location],
|
||||
after=header_id,
|
||||
)
|
||||
await asyncio.sleep(1)
|
||||
|
||||
|
||||
asyncio.run(reverse_rows())
|
||||
@@ -40,6 +40,9 @@ class NotionAsync:
|
||||
page_size=page_size,
|
||||
)
|
||||
|
||||
async def delete_block(self, block_id: str) -> None:
|
||||
await self._client.blocks.delete(block_id=block_id)
|
||||
|
||||
async def block_is_table(self, block_id: str) -> bool:
|
||||
block: dict = await self.get_block(block_id=block_id)
|
||||
return block["type"] == "table"
|
||||
@@ -48,6 +51,11 @@ class NotionAsync:
|
||||
table = await self._client.blocks.retrieve(block_id=table_id)
|
||||
return table["table"]["table_width"]
|
||||
|
||||
async def append_table_row_text_after_header(self, table_id: str, text_list: list[str]) -> None:
|
||||
header: dict = await self.get_block_children(block_id=table_id, page_size=1)
|
||||
header_id = header["results"][0]["id"]
|
||||
await self.append_table_row_text(table_id=table_id, text_list=text_list, after=header_id)
|
||||
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user