Notion API master series 08 - Analysis - Search (First)

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

Reference API URL : Search

This series lets us know how to use the method Notion API with examples.
https://developers.notion.com/reference/post-search

Notion API makes the notion object, fills the method name, and uses it.
Write notion.search and fill parameter values in parenthesis().
For example, if we want to bring page information and only bring ten bunches of page, declare like as below.

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

It described filter in Notion API reference document, page_size, or filter in BODY PARAMS.
Input the necessary BODY PARAMS and bring the result with the search method.

  • query
    When supplied, limits which pages are returned by comparing the query to the page title.
    page의 title이 존재할때 title에 매칭되는 문구가 있는지 확인하고 결과를 가져옵니다.
    For example, if you want to find the department, call the command like notion.search(query='department').

note
If you want to search contains space from a query parameter like notion.search(query='customer center'), results are customer keyword pages and center keyword page. So please note that the result doesn't match only the exact customer center keyword.

Then how can I want to bring the information that only contains the customer center title?

pages = notion.search(query='고객센터 게시판', filter={"property": "object", "value": "page"})
find_title = '고객센터 게시판'
for page in pages['result']:
   title_name = ['properties']['이름']['title'][0]['plain_text']
   if title_name == find_title:
      target_id = page['id']
   

Bring title value directly, compare and find exact keyword's page id.
If the result exceeds 100 bunches, check whether the has_more value is True or not and launch the while statement and search it until has_more is True.

pages = notion.search(query='고객센터 게시판', filter={"property": "object", "value": "page"})

has_more = pages['has_more']
if has_more:
   next_cursor = pages['next_cursor']
find_title = '고객센터 게시판'
for page in pages['result']:
   title_name = ['properties']['이름']['title'][0]['plain_text']
   if title_name == find_title:
      target_id = page['id']
while pages[has_more]:
   pages = notion.search(query='고객센터 게시판', filter={"property": "object", "value": "page"},
                         start_cursor=next_cursor)
   has_more = pages['has_more']
   if has_more:
      next_cursor = pages['next_cursor']
   if title_name == find_title:
      target_id = page['id']

If you make the code, then you will find duplicated code. Declare def function, and you can make more clean code.

def page_finder(cursor_id):
   if cursor_id:
      pages = notion.search(filter={"property": "object", "value": "page"},
                                 start_cursor=cursor_id)
      else:
         pages = notion.search(filter={"property": "object", "value": "page"}}
   return pages, pages['next_cursor']

def find_page_id_details(page_name, cursor_id=None):
   pages, cursor_id = page_finder(cursor_id)
   result_page_id = False
     for page in pages['results']:
         page_title = ""
         try:
             page_title = page['properties']['Task']['title'][0]['plain_text']
         except:
             pass
         if page_title == page_name:
             result_page_id = page['id']
             break
  return result_page_id, cursor_id, pages['has_more']
profile
QA Specialist

0개의 댓글