Skip to content

Instantly share code, notes, and snippets.

@shinyaa31
Last active April 3, 2022 06:12
Show Gist options
  • Save shinyaa31/ea91f32ad2cff3bedda79eb3849b1d34 to your computer and use it in GitHub Desktop.
Save shinyaa31/ea91f32ad2cff3bedda79eb3849b1d34 to your computer and use it in GitHub Desktop.
Notionワークスペース配下の更新履歴作成用:Notion APIを使って必要なデータをJSON形式で取得&最終更新日時で降順ソートするスクリプト
#/bin/sh
NOTION_API_TOKEN='secret_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
#######################################################
## ワークスペース直下のページ(最上位階層ページ).
#######################################################
curl -X POST 'https://api.notion.com/v1/search' \
-H "Notion-Version: 2021-05-13" \
-H 'Authorization: Bearer '$NOTION_API_TOKEN'' \
-H 'Content-Type: application/json' \
--data '{
"sort":{
"direction":"descending",
"timestamp":"last_edited_time"
}
}' | \
jq -c '.results[] | select (.parent.type == "workspace")' | \
jq -c '{ object: .object, last_edited_time: .last_edited_time, id: .id, page_title: .properties.title.title[0].plain_text } ' > update-histories-notion.json
#######################################################
## ワークスペース直下のページ(最上位階層以外のページ).
#######################################################
curl -X POST 'https://api.notion.com/v1/search' \
-H "Notion-Version: 2021-05-13" \
-H 'Authorization: Bearer '$NOTION_API_TOKEN'' \
-H 'Content-Type: application/json' \
--data '{
"sort":{
"direction":"descending",
"timestamp":"last_edited_time"
}
}' |\
jq -c '.results[] | select (.object == "page" and .parent.type == "page_id")' | \
jq -c '{ object: .object, last_edited_time: .last_edited_time, id: .id, page_title: .properties.title.title[0].plain_text } ' >> update-histories-notion.json
#######################################################
## ワークスペース直下のページ(テーブルページ).
#######################################################
curl -X POST 'https://api.notion.com/v1/search' \
-H "Notion-Version: 2021-05-13" \
-H 'Authorization: Bearer '$NOTION_API_TOKEN'' \
-H 'Content-Type: application/json' \
--data '{
"sort":{
"direction":"descending",
"timestamp":"last_edited_time"
}
}' | \
jq -c '.results[] | select (.object == "database")' | \
jq -c '{ object: .object, last_edited_time: .last_edited_time, id: .id, page_title: .title[0].plain_text } ' >> update-histories-notion.json
#######################################################
## ワークスペース配下にあるテーブル関連ページ(議事録).
#######################################################
curl -X POST 'https://api.notion.com/v1/search' \
-H "Notion-Version: 2021-05-13" \
-H 'Authorization: Bearer '$NOTION_API_TOKEN'' \
-H 'Content-Type: application/json' \
--data '{
"sort":{
"direction":"descending",
"timestamp":"last_edited_time"
}
}' |\
jq -c '. | select (.object == "list")' | \
jq '.results[] | select (.object == "page" and .properties["会議名"] !=null)' | \
jq -c '{ object: .object, last_edited_time: .last_edited_time, id: .id, page_title: .properties["会議名"].title[].plain_text } ' >> update-histories-notion.json
#######################################################
## ワークスペース配下にあるテーブル関連ページ(タイトル).
#######################################################
curl -X POST 'https://api.notion.com/v1/search' \
-H "Notion-Version: 2021-05-13" \
-H 'Authorization: Bearer '$NOTION_API_TOKEN'' \
-H 'Content-Type: application/json' \
--data '{
"sort":{
"direction":"descending",
"timestamp":"last_edited_time"
}
}' |\
jq -c '. | select (.object == "list")' | \
jq '.results[] | select (.object == "page" and .properties["タイトル"] !=null)' | \
jq -c '{ object: .object, last_edited_time: .last_edited_time, id: .id, page_title: .properties["タイトル"].title[].plain_text } ' >> update-histories-notion.json
#######################################################
## マージした履歴情報を更新日時で降順ソート.
#######################################################
jq -c '. | sort_by(.last_edited_time) | reverse | .[]' --slurp update-histories-notion.json > update-histories-notion-sorted.json
#######################################################
## ソート済みjsonファイルを使ってPythonでHTMLを整形.
#######################################################
python read-notion-json.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment