-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
modules.js
188 lines (156 loc) · 5.7 KB
/
modules.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
// Note: Each check might take up to 5 seconds depending on how many books you have to update.
// Making this value too low might break the program.
const CHECK_EVERY_SECONDS = 3;
const { Client } = require("@notionhq/client");
require("dotenv").config();
if (!(process.env.NOTION_API_KEY && process.env.DATABASE_ID)) {
throw new Error("Please fill in your API key and database ID in repl.it");
}
const notion = new Client({ auth: process.env.NOTION_API_KEY });
const fetch = require("cross-fetch");
let dont_update = [];
const fetchAndUpdate = async () => {
console.log("Current don't-update list is", dont_update)
console.log("Restart server to clear.")
const databaseId = process.env.DATABASE_ID;
const queryResponse = await notion.databases.query({
database_id: databaseId,
page_size: 100,
filter: {
or: [
{
property: "Title",
rich_text: {
contains: ";"
}
}
]
}
});
const relevant_results = queryResponse.results.filter(
(i) => !dont_update.includes(i.id)
);
console.log(
`Checked database, found ${relevant_results.length} items to update.`
);
const all_updated = [];
for (i of relevant_results) {
gbook_query =
i.properties.Title.title[0]
.plain_text /*+ " " + i.properties["Author(s)"].multi_select.map(x => x.name).join(", ")*/;
console.log(gbook_query);
const j = await fetch(
`https://www.googleapis.com/books/v1/volumes?q=${encodeURIComponent(
gbook_query
)}`
).then((r) => r.json());
if (!(j.totalItems > 0)) {
console.log("No results found for " + gbook_query);
return;
}
const book = j.items[0];
let updateOptions = {
page_id: i.id,
properties: {
Title: {
title: [
{
type: "text",
text: {
content:
book.volumeInfo.title ||
i.properties.Title.title[0].plain_text.replace(
";",
""
),
},
},
],
},
"Author(s)": {
multi_select: book.volumeInfo.authors
.filter((x) => x)
.map((x) => ({
name: x.replace(",", ""),
})),
},
"Genre(s)": {
multi_select: (book.volumeInfo.categories || [])
.filter((x) => x)
.map((x) => ({
name: x.replace(",", ""),
})),
},
Description: {
rich_text: [
{
text: {
content:
(book.volumeInfo.description || "").length <
500
? book.volumeInfo.description || ""
: book.volumeInfo.description.substring(
0,
500
) + "...",
},
},
],
},
Link: {
url: book.volumeInfo.previewLink,
},
},
icon: {
emoji: "📖",
},
};
if (book.volumeInfo.imageLinks) {
updateOptions.icon = {
external: {
url: `https://readinglist-cover-proxy.srg.id.au/${book.id}.jpg`,
},
};
updateOptions.cover = {
external: {
url: `https://readinglist-cover-proxy.srg.id.au/${book.id}.jpg`,
},
};
}
if (book.volumeInfo.averageRating) {
updateOptions.properties["Rating"] = {
number: book.volumeInfo.averageRating,
};
}
if (book.volumeInfo.pageCount) {
updateOptions.properties["Pages"] = {
number: book.volumeInfo.pageCount,
};
}
try {
await notion.pages.update(updateOptions);
all_updated.push(i.properties.Title.title[0].plain_text);
} catch (e) {
console.error(`Error on ${i.id}: [${e.status}] ${e.message}`);
if (e.status == 409) {
console.log("Saving conflict, scheduling retry in 3 seconds");
setTimeout(async () => {
try {
console.log(`Retrying ${i.id}`);
await notion.pages.update(updateOptions);
} catch (e) {
console.error(
`Subsequent error while resolving saving conflict on ${i.id}: [${e.status}] ${e.message}`
);
dont_update.push(i.id);
}
}, 3000);
} else {
dont_update.push(i.id);
}
}
console.log("Updated " + i.properties.Title.title[0].plain_text);
}
return all_updated;
};
module.exports.fetchAndUpdate = fetchAndUpdate;