-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbookmark2notion.js
188 lines (177 loc) · 4.79 KB
/
bookmark2notion.js
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
const { config } = require('dotenv');
const { login } = require('masto');
const { Client } = require('@notionhq/client');
const { JSDOM } = require('jsdom');
const findIdx = require('lodash/findIndex');
config();
const URL_RE =
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;
const notion = new Client({
auth: process.env.NOTION_TOKEN,
});
const dbID = process.env.BOOKMARK_DATABASE_ID;
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
function dudu2props(item) {
let id = item.id; //ID
let source = item.uri;
let dom = new JSDOM(item.content.replace(/<br\s*\/>/g, '\n'));
let links = dom.window.document.querySelectorAll('a');
links.forEach((li) => {
let de = li.classList.contains('hashtag') ? ' ' : '\n';
li.innerHTML = de + li.textContent + de;
});
let content = dom.window.document.body.textContent;
let lines = content.split('\n');
content = [];
lines.forEach((line) => {
if (!URL_RE.test(line)) {
if (
content.length === 0 ||
(content.length > 0 && content[content.length - 1].type === 'url') ||
(content.length > 0 &&
content[content.length - 1].type === 'text' &&
(content[content.length - 1].text + line).length > 2000) // one block of paragraph max length 2000
) {
content.push({
type: 'text',
text: line,
});
} else {
content[content.length - 1].text += '\n' + line;
}
} else {
content.push({
type: 'url',
text: line,
});
}
});
return {
properties: {
Name: {
title: [
{
text: {
content: item.spoilerText || '@' + item.account.username,
},
},
],
},
Source: {
url: source,
},
ID: {
rich_text: [{
type: 'text', // not using number as the ID is very big number
text: {
content: id,
},
}],
},
},
children: [
{
object: 'block',
type: 'paragraph',
paragraph: {
text: content.map((i) => {
if (i.type === 'text') {
return {
type: 'text',
text: {
content: i.text,
},
};
} else if (i.type === 'url') {
return {
type: 'text',
text: {
content: '\n' + i.text + '\n',
link: {
url: i.text,
},
},
};
}
}),
},
},
],
};
}
async function main() {
const masto = await login({
url: process.env.MASTO_URL,
accessToken: process.env.MASTO_TOKEN,
});
let sorted = await notion.databases.query({
database_id: dbID,
sorts: [
{
property: 'Created',
direction: 'descending',
},
],
page_size: 1,
});
let last = sorted.results?.[0].properties.ID.rich_text[0].plain_text || null;
let bookmarks = [];
let gen = await masto.bookmarks.getIterator({ limit: 50 });
if (last) {
let idx = null;
while (idx === -1 || idx === null) {
let { value } = await gen.next();
idx = findIdx(value, (v) => v.id === last);
if (idx > 0) { // no need to fetch more
bookmarks.push(...value.slice(0, idx));
console.log('Fetched', bookmarks.length, 'bookmarks.');
break;
} else if (idx === 0) { // no new item
break;
} // else: index == -1, need to fetch more
await sleep('500');
}
if (bookmarks.length) {
console.log('New', bookmarks.length, 'bookmarks to be inserted.');
} else {
console.log('No new bookmarks.');
return;
}
} else { // no last means the notion db is blank now, need to fetch all bookmarks
let done = false;
while (!done) {
let res = await gen.next();
done = res.done;
if (done) {
break;
}
if (res.value) {
bookmarks.push(...res.value);
console.log('Fetched', bookmarks.length, 'bookmarks.');
await sleep('500');
}
}
}
bookmarks = bookmarks.reverse(); // order in mastodon API is reversed by time, last in, last out
for (let i = 0; i < bookmarks.length; i++) {
const item = bookmarks[i];
try {
const postData = {
parent: {
database_id: dbID,
},
...dudu2props(item),
};
const response = await notion.pages.create(postData);
await sleep('200');
if (response && response.id) {
console.log(item.url, ' page created (id=', item.id, ')');
}
} catch (error) {
console.warn('Failed to create the page for ', item.url, '(id=', item.id, ')');
}
}
}
main();