Notion API master series 07 - Sample Code (Summary for 01~06)

소찬 (Chan)·2022년 8월 22일
0
post-thumbnail

I summarized from series 01 to 06 as source code.
What a kind blogger I am. (start a rumor here)

# Initial Code
from dotenv import dotenv_values
from notion_client import Client

config = dotenv_values(".env")
notion_secret = config.get('NOTION_TOKEN')
notion = Client(auth=notion_secret)

# Read every page from Bot and can regulate page_size value from your preference
pages = notion.search(filter={"property": "object", "value": "page"}, page_size=10)
pprint(pages)

cursor_id = pages['next_cursor']
while pages['has_more']:
   pages = notion.search(filter={"property": "object", "value": "page"},
                         start_cursor=cursor_id, page_size=10)
   pprint(pages)
   if pages['has_more']:
      cursor_id = pages['next_cursor']

# Change the content in the block
# Below page_id means page's id, which needs to update from notion.search
page_id = '2c9258e1-43ed-4713-8895-123d42268d2f'
block_list = notion.blocks.children.list(block_id=page_id)
pprint(block_list)

# Bring the plain_text value and change the value that I want from first block, order is [0]
# Bring the id and rich_text info
target_block_id = block_list['results'][0]['id']
rich_text_temp = block_list['results'][0]['paragraph']['rich_text']

# Bring the plain_text value and change the value that I want
text_value = rich_text_temp[0]['plain_text']
text_value = text_value.replace('페퍼로니', '크러스트')

# Changed plain_text and text value to reflect the content
rich_text_temp[0]['plain_text'] = text_value
rich_text_temp[0]['text']['content'] = text_value

# Bind as dictionary type음
rich_text = {}
rich_text['rich_text'] = rich_text_temp

# Update to target_block_id with changed rich_text value
notion.blocks.update(block_id=target_block_id, paragraph=rich_text)
profile
QA Specialist

0개의 댓글