-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_kindle_notebook_to_notion.php
143 lines (127 loc) · 3.34 KB
/
import_kindle_notebook_to_notion.php
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
<?php
/**
*
* import_kindle_notebook_to_notion.php
*
* Author:
* GitHub: @happy-se-life
*
* Usage:
* $ php import_kindle_notebook_to_notion.php notebook.html
*
* This software is released under the MIT License, see LICENSE.
*
*/
require('config.php');
/**
* Return array of an block item.
*/
function getArrayBlock($type, $content) {
return [
"object" => "block",
"type" => "$type",
"$type" => [
"text" => [
[
"type" => "text",
"text" => [ "content" => "$content" ]
]
]
]
];
}
/**
* Create post data.
*/
function createPostData($html) {
if (!is_file($html)) {
echo "Please specify the correct file.\n";
exit(1);
}
// Read html
$doc = new DOMDocument();
$doc->loadHTMLFile($html);
$elements = $doc->getElementsByTagName('div');
$children = [];
$bookTitle = [];
$authors = [];
// Create children array contained in post_data
foreach ($elements as $elm) {
$text = trim($elm->nodeValue);
switch ($elm->getAttribute('class')) {
case "bookTitle" :
$bookTitle = $text;
break;
case "authors" :
$authors = $text;
break;
case "citation" :
if (strlen($text) != 0) {
$children[] = getArrayBlock("paragraph", $text);
}
break;
case "sectionHeading" :
$children[] = getArrayBlock("heading_2", $text);
break;
case "noteHeading" :
$children[] = getArrayBlock("heading_3", $text);
break;
case "noteText" :
$children[] = getArrayBlock("paragraph", $text);
break;
default :
break;
}
}
// Create post data
$post_data = [
"parent" => [ "database_id" => MY_NOTION_DATABASE_ID ],
"properties" => [
"Name" => [
"title" => [
[
"text" => [
"content" => "$bookTitle"
]
]
]
],
"Authors" => [
"rich_text" => [
[
"text" => [
"content" => "$authors"
]
]
]
]
],
"children" => $children,
];
return $post_data;
}
/**
* Import to notion.
*/
function import($html) {
$post_data = createPostData($html);
$header = [
"Authorization: Bearer " . MY_NOTION_TOKEN,
"Content-Type: application/json",
"Notion-Version: " . NOTION_API_VERSION,
];
$curl = curl_init( NOTION_API_ENDPOINT );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data));
// Post
$result = curl_exec($curl);
if ($result) {
echo "The import process was successful.\n";
} else {
echo "Import process failed.\n";
}
return;
}
import($argv[1]);