背景
最近在折腾vim,在~/.vimrc
文件下有很多配置。但建docker容器的时候每次都需要复制一次到~/.vimrc
,未免太麻烦了,所以我在想,如果可以有一个网页,我可以在Dockerfile里获取这个网页的内容,然后自动配置好vim,docker容器开箱即用多爽。而我刚好重度使用notion,notion提供了API可以获取block或者page的内容。
准备工作
官方文档:
Start building with the Notion API
先在下面这个网址创建机器人,也叫integrations,给机器人取个合适的名字:
Notion - The all-in-one workspace for your notes, tasks, wikis, and databases.
设置机器人的权限,复制**NOTION_API_KEY
**,即以secret开头的Key。
在需要获取数据的page里,点右上角share
按钮,再invite
给刚刚创建的机器人。
获取block内容
先想要获取其内容的block链接,打开block菜单,点击Copy link to block
,如:
https://www.notion.so/yym68686/Notion-API-e209ea9434954161945e4765f5293291#bdcbbfec8de24ad2935587b3d46dde32
井号后面的一串数字就是block的ID。
在命令行里输入:
curl 'https://api.notion.com/v1/blocks/bdcbbfec8de24ad2935587b3d46dde32' -o test \
-H 'Authorization: Bearer secret_8DQN9w*********3thq6qdPkKyywqZN' \
-H 'Notion-Version: 2022-02-22'
得到的响应存到test文件里:
{"object":"block","id":"bdcbbfec-8de2-4ad2-9355-87b3d46dde32","created_time":"2022-03-15T16:33:00.000Z","last_edited_time":"2022-03-15T16:45:00.000Z","created_by":{"object":"user","id":"e80cb6ce-f7fc-4d76-bcd9-007af1eadbf5"},"last_edited_by":{"object":"user","id":"e80cb6ce-f7fc-4d76-bcd9-007af1eadbf5"},"has_children":false,"archived":false,"type":"code","code":{"caption":[],"rich_text":[{"type":"text","text":{"content":"curl 'https://api.notion.com/v1/blocks/","link":null},"annotations":{"bold":false,"italic":false,"strikethrough":false,"underline":false,"code":false,"color":"default"},"plain_text":"curl 'https://api.notion.com/v1/blocks/","href":null},{"type":"text","text":{"content":"bdcbbfec8de24ad2935587b3d46dde32","link":null},"annotations":{"bold":false,"italic":false,"strikethrough":false,"underline":false,"code":false,"color":"red"},"plain_text":"bdcbbfec8de24ad2935587b3d46dde32","href":null},{"type":"text","text":{"content":"' -o test \\\n -H 'Authorization: Bearer secret_8DQN9wRRMpDUgwY3St79g5ltrxL3thq6qdPkKyywqZN' \\\n -H 'Notion-Version: 2022-02-22'\n","link":null},"annotations":{"bold":false,"italic":false,"strikethrough":false,"underline":false,"code":false,"color":"default"},"plain_text":"' -o test \\\n -H 'Authorization: Bearer secret_8DQN9wRRMpDUgwY3St79g5ltrxL3thq6qdPkKyywqZN' \\\n -H 'Notion-Version: 2022-02-22'\n","href":null}],"language":"bash"}}
考虑格式化json,保证有python环境,在vim底行模式下输入:
:%!python -m json.tool
文件格式化为:
{
"archived": false,
"code": {
"caption": [],
"language": "bash",
"rich_text": [
{
"annotations": {
"bold": false,
"code": false,
"color": "default",
"italic": false,
"strikethrough": false,
"underline": false
},
"href": null,
"plain_text": "curl 'https://api.notion.com/v1/blocks/bdcbbfec8de24ad2935587b3d46dde32' -o test \\\n -H 'Authorization: Bearer secret_8DQN9wRRM**************L3thq6qdPkKyywqZN' \\\n -H 'Notion-Version: 2022-02-22'\n",
"text": {
"content": "curl 'https://api.notion.com/v1/blocks/bdcbbfec8de24ad2935587b3d46dde32' -o test \\\n -H 'Authorization: Bearer secret_8DQN9***************6qdPkKyywqZN' \\\n -H 'Notion-Version: 2022-02-22'\n",
"link": null
},
"type": "text"
}
]
},
"created_by": {
"id": "e80cb6ce-f7fc-4d76-bcd9-007af1eadbf5",
"object": "user"
},
"created_time": "2022-03-15T16:33:00.000Z",
"has_children": false,
"id": "bdcbbfec-8de2-4ad2-9355-87b3d46dde32",
"last_edited_by": {
"id": "e80cb6ce-f7fc-4d76-bcd9-007af1eadbf5",
"object": "user"
},
"last_edited_time": "2022-03-16T07:59:00.000Z",
"object": "block",
"type": "code"
}
提取plain_text的内容,就是block的内容。利用jq解析json文件,安装jq:
apt install -y jq
利用管道传给jq,再写回test里面:
curl 'https://api.notion.com/v1/blocks/bdcbbfec8de24ad2935587b3d46dde32' \
-H 'Authorization: Bearer secret_8**************L3thq6qdPkKyywqZN' \
-H 'Notion-Version: 2022-02-22' | jq -r '.code.rich_text[].plain_text' > test
最后test里面的内容就跟block内容一样了:
curl 'https://api.notion.com/v1/blocks/bdcbbfec8de24ad2935587b3d46dde32' -o test \
-H 'Authorization: Bearer secret_8********************3thq6qdPkKyywqZN' \
-H 'Notion-Version: 2022-02-22'