Skip to content

Commit

Permalink
ADD : add retrieve block children features
Browse files Browse the repository at this point in the history
  • Loading branch information
minwook-shin committed May 30, 2023
1 parent 983dd6c commit 3deb359
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 1.1.0 (2023-5-30)

### Features

* Add retrieve block children
* https://github.com/minwook-shin/notion-database/issues/16

## 1.0.0 (2023-5-28)

we've implemented all features, change the version rule to the semantic version.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ python setup.py install
* ✅ Retrieve a database
* ✅ Update a database
* ✅ Update database properties
* Blocks
* ✅ Retrieve a block
* ✅ Retrieve block children

## Contributing

Expand Down
43 changes: 43 additions & 0 deletions notion_database/block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Notion API Block
"""
from typing import Dict, List

from notion_database.components.request import Request


class Block: # pylint: disable=too-few-public-methods
"""
Notion API Block class
"""

def __init__(self, integrations_token: str):
"""
init
:param integrations_token: Notion Internal Integration Token
"""
self.properties_list: List[Dict] = []
self.url: str = 'https://api.notion.com/v1/blocks'
self.result: Dict = {}
self.request: Request = Request(self.url, integrations_token=integrations_token)

def retrieve_block(self, block_id: str, is_children: bool = False,
page_size: int = 100, start_cursor: str = None):
"""
Retrieve a block
:param block_id: Identifier for a Notion blocks
:param is_children: Trigger's paginated array of child block objects
:param start_cursor: optional parameter for is_children=True
:param page_size: optional parameter for is_children=True
:return:
"""
if is_children:
param = f"children?page_size={page_size}&start_cursor={start_cursor}"
if start_cursor is None:
param = param.split("&")[0]
self.result = self.request.call_api_get(self.url + "/" + block_id
+ "/" + param)
else:
self.result = self.request.call_api_get(self.url + "/" + block_id)
37 changes: 37 additions & 0 deletions page_content_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import pprint

from block import Block
from notion_database.const.query import Direction, Timestamp
from notion_database.database import Database
from notion_database.search import Search

try:
from dotenv import load_dotenv

load_dotenv()
except ModuleNotFoundError:
pass

NOTION_KEY = os.getenv('NOTION_KEY')

S = Search(integrations_token=NOTION_KEY)
S.search_database(query="", sort={"direction": Direction.ascending, "timestamp": Timestamp.last_edited_time})

for i in S.result:
database_id = i["id"]
D = Database(integrations_token=NOTION_KEY)
D.retrieve_database(database_id=database_id)
D.find_all_page(database_id=database_id, page_size=1)
pprint.pprint(D.result)

B = Block(integrations_token=NOTION_KEY)
for j in D.result["results"]:
B.retrieve_block(block_id=j["id"])
pprint.pprint(B.result)
# B.retrieve_block(block_id=j["id"], is_children=True)
# pprint.pprint(B.result)
B.retrieve_block(block_id=j["id"], is_children=True, page_size=1)
pprint.pprint(B.result)
# B.retrieve_block(block_id=j["id"],is_children=True, page_size=1, start_cursor=B.result["next_cursor"])
# pprint.pprint(B.result)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="notion-database",
version="1.0.0",
version="1.1.0",
author="minwook-shin",
author_email="minwook0106@gmail.com",
description=" Python bindings for Notion Database API",
Expand Down

0 comments on commit 3deb359

Please sign in to comment.