Package: cached-notion
CachedNotion 🔧CachedNotionoffers an enhanced, caching-enabled interface for interacting with Notion APIs. While it's a work in progress 🚧, its core functionality is fully operational, ensuring efficient and smooth integration.What's New 🌟Critical Caching Issue Resolved:We've tackled a significant caching problem to ensure more reliable and faster access to your Notion data.Stay tuned for ongoing updates and improvements! 💼Installation 📦CachedNotionis currently available on TestPyPI. You can install it using either pip or Poetry.Using pipTo installCachedNotionusing pip, run the following command:pipinstallcached-notionUsing PoetryFor those using Poetry, you can addCachedNotionto your project as follows:poetryaddcached-notionNew Feature: Notion to Markdown Conversion 📝CachedNotionnow offers an innovative feature to convert Notion pages into Markdown format. This is invaluable for users wanting to document, backup, or share their Notion content in a standardized format.Function Overviewurl_to_md: Converts a Notion URL into a Markdown string. It handles nested content up to a specified depth, making it perfect for diverse documentation needs.id_to_md: A complementary function tourl_to_md, handling individual IDs and managing content depth.Understanding Parametersmax_depth: This parameter inurl_to_mdspecifies the recursive depth for crawling through the Notion content. A value of-1indicates no limit, ensuring a comprehensive conversion that includes all nested elements. Adjust this parameter to control how deep the function traverses through nested pages or blocks.subs: Short for "sub-items," this parameter represents the remaining sub-items that are yet to be processed for further depths. It's crucial for managing and tracking the conversion process across nested levels of content.Usingurl_to_mdQuickly convert any Notion page into Markdown:fromcached_notion.utilsimporturl_to_md# Initialize CachedClientclient=CachedClient(auth=os.environ["NOTION_TOKEN"],cache_delta=24)# Convert a Notion URL to Markdown, specifying the depth of content to includenotion_url="https://www.notion.so/xxx/xxx"markdown_string,remaining_subs=url_to_md(client,notion_url,max_depth=-1)# Markdown string is now ready, along with any remaining sub-items for further processingprint(markdown_string)This function is especially useful for thorough documentation, platform exports, or content backups.Usingid_to_mdid_to_mdcan also be used independently for tailored Markdown generation processes, providing flexibility and control over the depth and content of the conversion.Basic Usage 📖Effortlessly replaceNotionClientwithCachedClientfor an optimized experience:fromcached_notion.cached_clientimportCachedClientfromcached_notion.utilsimportnormalize_url,get_id_with_object_typeimportos# Initialize CachedClient with your Notion token and desired cache settingsclient=CachedClient(auth=os.environ["NOTION_TOKEN"],cache_delta=24)url="https://www.notion.so/xxx/xxx"normalized_url=normalize_url(url)nid,object_type=get_id_with_object_type(normalized_url)# Use CachedClient to interact with Notion by providing the Notion IDpage=client.pages.retrieve(nid)database=client.databases.retrieve(nid)block=client.blocks.retrieve(nid)Utility Functions 🛠️Maximize your productivity with these handy functions:fromcached_notion.cached_clientimportCachedClientfromcached_notion.utilsimportnormalize_url,get_id_with_object_type,retrieve_objectimportosclient=CachedClient(auth=os.environ["NOTION_TOKEN"],cache_delta=24)# Normalize the URL and extract the Notion ID and object typeurl="https://www.notion.so/xxx/xxx"normalized_url=normalize_url(url)nid,object_type=get_id_with_object_type(normalized_url)# Use the retrieve_object utility function to get the object from Notionobj=retrieve_object(client,nid,object_type)# Now you can work with the retrieved objectprint(obj)fromcached_notion.cached_clientimportCachedClientfromcached_notion.utilsimportnormalize_url,get_id_with_object_type,retrieve_all_contentimportosclient=CachedClient(auth=os.environ["NOTION_TOKEN"],cache_delta=24)# Normalize the URL and extract the Notion ID and object typeurl="https://www.notion.so/xxx/xxx"normalized_url=normalize_url(url)nid,object_type=get_id_with_object_type(normalized_url)# Use the retrieve_all_content function to get the full content from Notionfull_content=retrieve_all_content(client,nid,object_type)# Now you can work with the full content retrieved from Notionprint(full_content)Enhanced Caching Strategy 💡Cache Delta Explained:Setcache_deltato manage how often the API calls the Notion API. A positive value uses cached content within the specified hours, reducing API calls. A zero value always fetches fresh content but minimizes API usage when used withretrieve_all_content.