Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(property-converter): unique_id #2

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions notion_exporter/exporter.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from typing import Optional
import asyncio
import logging
from typing import Optional

from notion_client import AsyncClient as NotionClient, APIResponseError
from notion_client import APIResponseError
from notion_client import AsyncClient as NotionClient
from notion_client import Client
from notion_client.helpers import async_collect_paginated_api
from tenacity import retry, retry_if_exception, stop_after_attempt, wait_exponential

from notion_exporter.block_converter import BlockConverter
from notion_exporter.property_converter import PropertyConverter
from notion_exporter.retry_utils import is_rate_limit_exception, wait_for_retry_after_header, is_unavailable_exception

from notion_exporter.retry_utils import (
is_rate_limit_exception,
is_unavailable_exception,
wait_for_retry_after_header,
)

logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s", datefmt="%m/%d/%Y %I:%M:%S %p")
logger = logging.getLogger(__name__)
Expand All @@ -26,19 +30,22 @@ def __init__(
notion_token: str,
export_child_pages: bool = False,
extract_page_metadata: bool = False,
extract_page_properties: bool = False,
exclude_title_containing: Optional[str] = None,
):
"""
:param notion_token: Notion API token.
:param export_child_pages: Whether to export child pages. Default: True.
:param extract_page_metadata: Whether to extract page metadata. Default: False.
:param exclude_title_containing: If specified, pages with titles containing this string will be excluded.
:param extract_page_properties: Whether to extract page properties. Default: False.

"""
self.notion = NotionClient(auth=notion_token)
self.sync_notion = Client(auth=notion_token)
self.export_child_pages = export_child_pages
self.extract_page_metadata = extract_page_metadata
self.extract_page_properties = extract_page_properties
self.exclude_title_containing = exclude_title_containing
self.block_converter = BlockConverter()
self.property_converter = PropertyConverter(self)
Expand Down Expand Up @@ -293,7 +300,10 @@ async def _get_child_blocks(self, page_id: str) -> list[dict]:
stop=stop_after_attempt(3),
)
async def _get_page_meta(self, page_id: str) -> dict:
page_object = await self.notion.pages.retrieve(page_id)
try:
page_object = await self.notion.pages.retrieve(page_id)
except APIResponseError:
return {"page_id": page_id, "title": "Untitled", "url": "", "created_by": "", "last_edited_by": ""}
created_by, last_edited_by = await asyncio.gather(
self._get_user(page_object["created_by"]["id"]), self._get_user(page_object["last_edited_by"]["id"])
)
Expand Down Expand Up @@ -401,7 +411,7 @@ def _get_page_front_matter(self, page_meta: dict, page_paths: dict, parent_page_
front_matter = ""
if self.extract_page_metadata:
front_matter += "---\n"
front_matter += f"title: {page_meta['title']}\n"
front_matter += f'title: "{page_meta['title']}"\n'
# Add quotation marks to avoid issues with colons in page titles
front_matter += f'path: "{current_page_path}"\n'
front_matter += f"url: {page_meta['url']}\n"
Expand All @@ -411,7 +421,7 @@ def _get_page_front_matter(self, page_meta: dict, page_paths: dict, parent_page_
front_matter += "---\n\n"

# Add properties of database entries as key-value pairs
if "properties" in page_meta:
if self.extract_page_properties and "properties" in page_meta:
for prop_name, prop in page_meta["properties"].items():
front_matter += f"{prop_name}: {prop}\n"
front_matter += "\n"
Expand Down
11 changes: 11 additions & 0 deletions notion_exporter/property_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, notion_exporter: "NotionExporter"):
"status": self.status,
"title": self.title,
"url": self.url,
"unique_id": self.unique_id,
}
self.notion_exporter = notion_exporter

Expand Down Expand Up @@ -206,3 +207,13 @@ def url(property_item: dict) -> str:
"""
url = property_item["url"] if property_item["url"] else ""
return url

@staticmethod
def unique_id(property_item: dict) -> str:
"""
Converts a unique_id property to a Markdown string.
"""
uid = property_item["unique_id"]
prefix = uid.get('prefix', '')
value = uid.get('number', '')
return f"{prefix}-{value}"