Notion API Master Series 02 - Search

소찬 (Chan)·2022년 7월 18일
0
post-thumbnail

Before using search, click the share button and add which last time you created the bot user.
And then, you can find API Integration from the Bot user.

Come back to python and make the object name as the notion, and we will launch the search module. And use pprint module to look good print.

from dotenv import dotenv_values
from notion_client import Client
from pprint import pprint

config = dotenv_values(".env")
notion_secret = config.get('NOTION_TOKEN_PROBIT2')

notion = Client(auth=notion_secret)
pages = notion.search(filter={"property": "object", "value": "page"})
pprint(pages)

Run the code, and then you can see as below result.
Firstly, we should check the value carefully, id value, and properties value on the result list array.

{'has_more': True,
 'next_cursor': 'e6dc1357-9778-4a2e-afe9-4861cc3b6c02',
 'object': 'list',
 'page_or_database': {},
 'results': [{'archived': False,
              'cover': None,
              'created_by': {'id': '7a43d973-5047-48bb-ab30-c006c009ab7b',
                             'object': 'user'},
              'created_time': '2022-06-07T00:46:00.000Z',
              'icon': {'emoji': '🧳', 'type': 'emoji'}, ...

id value means page_id, and properties value means infomations in that page.

 'id': '16e34dab-509f-4c7e-bb88-f778902dd597',
 'properties': {'title': {'id': 'title',
                          'title': [{'annotations': {'bold': False,
                                                     'code': False,
                                                     'color': 'default',
                                                     'italic': False,
                                                     'strikethrough': False,
                                                     'underline': False},
                          'href': None,
                          'plain_text': '기획서(APP) - '
                                        '출금 페이지 내역',
                          'text': {'content': '기획서(APP) '
                                              '- 출금 페이지 '
                                              '내역',
                                              'link': None},
                          'type': 'text'}],
                'type': 'title'}}

We cannot recognize this page title from only the id value, but we get to know the title from the plain_text value or content value of the text in id's properties value. So if we want to edit specific page, we can find the page with procedure as below.

The way to find a specific page

  1. Use the search(POST)
    module and save the value in the result variable.
  2. Call the title's plain_text value from the result variable and find the page I want.

However, if the page information is more extensive than 100 pages, it is impossible to call search to find the page at once.
One calling has a limit to the calling page. The Default value is 100, which is the maximum. If you want to find many things at once, you can search with page_size=100, or if you're going to find little by little, you may call search with page_size=10 or 20.

The number of items from the full list desired in the response. Maximum: 100

Example with page_size

pages = notion.search(filter={"property": "object", "value": "page"}, page_size=20)

Then the second time, call the search. How can we call the next part?
Do you remember the first part of notion.search, has_more, and next_cursor value?
if has_more is True, it means it has more the result. next_cursor means the first page_id(or database_id) from next result.

'has_more': True,
'next_cursor': 'e6dc1357-9778-4a2e-afe9-4861cc3b6c02',

So if we call again, use While statement until has_more become False.

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

How to bring only title value from properties? We can get a method by knowing the properties structure. The next series lets us know the properties structure.

profile
QA Specialist

0개의 댓글