|
9 | 9 | "heading_2": "## ", |
10 | 10 | "heading_3": "### ", |
11 | 11 | } |
12 | | -TEXT_PROPERTY_TYPES = frozenset({"rich_text", "title"}) |
13 | | -NAMED_PROPERTY_TYPES = frozenset({"select", "status"}) |
14 | 12 |
|
15 | 13 |
|
16 | 14 | class NotionExtractor: |
@@ -49,8 +47,6 @@ def _load_data_as_documents(self, notion_obj_id: str, notion_obj_type: str) -> s |
49 | 47 |
|
50 | 48 | def _get_notion_database_data(self, database_id: str) -> str: |
51 | 49 | """Fetch all pages from a Notion database and return as a Markdown table.""" |
52 | | - assert self._notion_access_token is not None, "Notion access token is required" |
53 | | - |
54 | 50 | # Retrieve database metadata |
55 | 51 | database_data = self._client.retrieve_database(database_id=database_id) |
56 | 52 |
|
@@ -100,7 +96,6 @@ def _get_notion_database_data(self, database_id: str) -> str: |
100 | 96 |
|
101 | 97 | def _get_notion_block_data(self, page_id: str) -> str: |
102 | 98 | """Fetch and process Notion block data.""" |
103 | | - assert self._notion_access_token is not None, "Notion access token is required" |
104 | 99 | result_lines_arr = [] |
105 | 100 |
|
106 | 101 | # Retrieve page metadata |
@@ -207,42 +202,46 @@ def _read_table_rows(self, block_id: str) -> str: |
207 | 202 | def _extract_property_value(self, property_value: dict[str, Any]) -> object: |
208 | 203 | """Extract the value of a Notion property.""" |
209 | 204 | column_type = property_value["type"] |
210 | | - if column_type == "multi_select": |
211 | | - return ", ".join(option["name"] for option in property_value[column_type]) |
212 | | - if column_type in TEXT_PROPERTY_TYPES: |
213 | | - return ( |
214 | | - property_value[column_type][0]["plain_text"] |
215 | | - if property_value[column_type] |
216 | | - else "" |
217 | | - ) |
218 | | - if column_type in NAMED_PROPERTY_TYPES: |
219 | | - return ( |
220 | | - property_value[column_type]["name"] |
221 | | - if property_value[column_type] |
222 | | - else "" |
223 | | - ) |
224 | | - if column_type == "number": |
225 | | - return property_value.get("number") |
226 | | - if column_type == "date": |
227 | | - date_data = property_value.get("date", {}) |
228 | | - return ( |
229 | | - {"start": date_data.get("start"), "end": date_data.get("end")} |
230 | | - if date_data |
231 | | - else None |
232 | | - ) |
233 | | - if column_type == "formula": |
234 | | - formula_value = property_value[column_type] |
235 | | - return ( |
236 | | - formula_value.get("number") |
237 | | - if isinstance(formula_value, dict) |
238 | | - and formula_value.get("type") == "number" |
239 | | - else formula_value |
240 | | - ) |
241 | | - if column_type == "created_by": |
242 | | - # Handle created_by type |
243 | | - created_by_data = property_value.get("created_by", {}) |
244 | | - return created_by_data.get("name") if created_by_data else None |
245 | | - return property_value[column_type] |
| 205 | + match column_type: |
| 206 | + case "multi_select": |
| 207 | + value = ", ".join( |
| 208 | + option["name"] for option in property_value[column_type] |
| 209 | + ) |
| 210 | + case "rich_text" | "title": |
| 211 | + value = ( |
| 212 | + property_value[column_type][0]["plain_text"] |
| 213 | + if property_value[column_type] |
| 214 | + else "" |
| 215 | + ) |
| 216 | + case "select" | "status": |
| 217 | + value = ( |
| 218 | + property_value[column_type]["name"] |
| 219 | + if property_value[column_type] |
| 220 | + else "" |
| 221 | + ) |
| 222 | + case "number": |
| 223 | + value = property_value.get("number") |
| 224 | + case "date": |
| 225 | + date_data = property_value.get("date", {}) |
| 226 | + value = ( |
| 227 | + {"start": date_data.get("start"), "end": date_data.get("end")} |
| 228 | + if date_data |
| 229 | + else None |
| 230 | + ) |
| 231 | + case "formula": |
| 232 | + formula_value = property_value[column_type] |
| 233 | + value = ( |
| 234 | + formula_value.get("number") |
| 235 | + if isinstance(formula_value, dict) |
| 236 | + and formula_value.get("type") == "number" |
| 237 | + else formula_value |
| 238 | + ) |
| 239 | + case "created_by": |
| 240 | + created_by_data = property_value.get("created_by", {}) |
| 241 | + value = created_by_data.get("name") if created_by_data else None |
| 242 | + case _: |
| 243 | + value = property_value[column_type] |
| 244 | + return value |
246 | 245 |
|
247 | 246 | def _extract_cell_text(self, cell: list[dict]) -> str: |
248 | 247 | """Extract text content from a table cell.""" |
|
0 commit comments