-
Notifications
You must be signed in to change notification settings - Fork 0
/
notion.zsh
executable file
·238 lines (177 loc) · 4.36 KB
/
notion.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/zsh
# ZSH Wrapper for Notion Developers API
# For more info:
# https://developers.notion.com/reference/intro
show_help() {
echo '
Usage:
./notion.sh function [-k/--api-key <notion_api_key>] [-d/--database-id <notion_database_id>] [-p/--page-id <notion_page_id>]
Supported functions up until now:
list_databases
retrieve_database
query_database
retrieve_page
create_page
'
}
# PARSES ARGUMENTS
zparseopts -D -E -F - k:=api_key -api-key:=api_key \
d:=db_id -database-id:=db_id \
p:=pg_id -page-id:=pg_id \
h=is_hp -help=is_hp || exit 1
# remove first -- or -
end_opts=$@[(i)(--|-)]
set -- "${@[0,end_opts-1]}" "${@[end_opts+1,-1]}"
if [[ -z $NOTION_API_KEY ]]; then
NOTION_API_KEY=${${api_key/(-k|--api-key)}}
fi
DATABASE_ID=${${db_id/(-d|--database-id)}}
PAGE_ID=${${pg_id/(-p|--page-id)}}
IS_HELP=$is_hp
API_FUNCTION=$1
if ! [[ -z $IS_HELP ]] || [[ $1 == 'help' ]]; then
show_help
exit 1
fi
echo ''
echo 'NOTION_API_KEY: '$NOTION_API_KEY
echo 'DATABASE_ID: '$DATABASE_ID
echo 'PAGE_ID: '$PAGE_ID
echo 'API_FUNCTION: '$API_FUNCTION
echo ''
# FUNCTIONS
## DATABASES
list_databases() {
curl 'https://api.notion.com/v1/databases' \
-H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
-H 'Notion-Version: 2021-05-13'
}
retrieve_database() {
curl 'https://api.notion.com/v1/databases/'$DATABASE_ID'' \
-H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
-H 'Notion-Version: 2021-05-13'
}
query_database() {
echo 'Function query_database not supported yet.'
}
## PAGES
retrieve_page() {
curl 'https://api.notion.com/v1/pages/'$PAGE_ID'' \
-H 'Notion-Version: 2021-05-13' \
-H 'Authorization: Bearer '"$NOTION_API_KEY"''
}
create_page() {
if ! [[ -z $DATABASE_ID ]]; then
parent_type=database_id
parent_id=$DATABASE_ID
db_column_name=$1
if [[ -z $db_column_name ]]; then
echo '[ERROR] Db column name is needed.'
exit 1
fi
new_page_name=$2
props='"properties": {
"'$db_column_name'": {
"title": [
{
"text": {
"content": "'$new_page_name'"
}
}
]
}
},'
elif ! [[ -z $PAGE_ID ]]; then
parent_type=page_id
parent_id=$PAGE_ID
new_page_name=$1
props='"properties": {
"title": [
{
"text": {
"content": "'$new_page_name'"
}
}
]
},'
else
echo "[ERROR] Needs database or page id."
exit 1
fi
children='{
"object": "block",
"type": "heading_2",
"heading_2": {
"text": [{ "type": "text", "text": { "content": "Lacinato kale" } }]
}
},
{
"object": "block",
"type": "paragraph",
"paragraph": {
"text": [
{
"type": "text",
"text": {
"content": "Lacinato kale is a variety of kale with a long tradition in Italian cuisine, especially that of Tuscany. It is also known as Tuscan kale, Italian kale, dinosaur kale, kale, flat back kale, palm tree kale, or black Tuscan palm.",
"link": { "url": "https://en.wikipedia.org/wiki/Lacinato_kale" }
}
}
]
}
}'
echo ''
echo 'parent_type: '$parent_type
echo 'parent_id: '$parent_id
echo 'db_column_name: '$db_column_name
echo 'new_page_name: '$new_page_name
echo ''
curl 'https://api.notion.com/v1/pages' \
-H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
-H "Content-Type: application/json" \
-H "Notion-Version: 2021-05-13" \
--data '{
"parent": { "'$parent_type'": "'$parent_id'" },
'$props'
"children": [
'$children'
]
}'
}
# TODO
# Implement other functions
# SWITCH CASE
case $API_FUNCTION in
list_databases)
# Usage:
echo 'Entered list_databases on switch \n'
list_databases ${@:2}
;;
retrieve_database)
# Usage:
echo 'Entered retrieve_database on switch \n'
retrieve_database ${@:2}
;;
query_database)
# Usage:
echo 'Entered query_database on switch \n'
query_database ${@:2}
;;
retrieve_page)
# Usage:
echo 'Entered retrieve_page on switch \n'
retrieve_page ${@:2}
;;
create_page)
# Usage:
# If you're creating a page inside a Database, use ./notion.sh create_page <db_column_name> <new_page_name> -d <parent_database_id> [args*]
# If you're creating a page inside another Page, use ./notion.sh create_page <new_page_name> -p <parent_page_id> [args*]
#
# * Check help: -k is always needed
echo 'Entered create_page on switch \n'
create_page ${@:2}
;;
*)
echo 'Command not recognized.'
;;
esac