-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathisfdb.js
More file actions
309 lines (260 loc) · 8.93 KB
/
isfdb.js
File metadata and controls
309 lines (260 loc) · 8.93 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { addContributor, cleanText, collectObject, fetchHTML, getCoverData, getFormattedText, logMarian, normalizeReadingFormat, remapKeys } from "../shared/utils.js";
import { Extractor } from "./AbstractExtractor.js"
const editionRegex = /https:\/\/(?:www\.)?isfdb\.org\/cgi-bin\/pl\.cgi\?(\d+)/;
const novelRegex = /https:\/\/(?:www\.)?isfdb\.org\/cgi-bin\/title\.cgi\?(\d+)/;
class isfdbScraper extends Extractor {
get _name() { return "ISFDB Extractor"; }
needsReload = false;
_sitePatterns = [
novelRegex,
editionRegex,
];
async getDetails() {
const url = window.location.href;
if (url.match(editionRegex)) return scrapeEdition();
if (url.match(novelRegex)) return scrapeBook();
throw new Error("Not implemented");
}
normalizeUrl(url) {
if (!url.includes("?")) return super.normalizeUrl(url);
let [domain, id] = url.split("?");
id = id.match(/\d+/)[0];
return `${domain}?${id}`;
}
}
const remappings = remapKeys.bind(undefined, {
"Series Number": "Series Place",
"Date": "Publication date",
"Synopsis": "Description",
"Publication": "Title",
"Format": "Edition Format",
"Current Tags": undefined,
"Webpages": undefined,
"User Rating": undefined,
"Price": undefined,
"Catalog ID": undefined,
// "Notes": undefined,
});
const novelTypes = {
"FANZINE": "Fanzine",
"ANTHOLOGY": "Anthology",
"CHAPBOOK": "Chapbook",
"COLLECTION": "Collection",
"MAGAZINE": "Magazine",
"NONFICTION": "Non-Fiction",
"NOVEL": "Novel",
"OMNIBUS": "Omnibus",
"SHORTFICTION": "Short Fiction",
"POEM": "Poem",
"ESSAY": "Essay",
"REVIEW": "Review",
"INTERVIEW": "Interview",
"SERIAL": "Serial",
"COVERART": "Cover Art",
"INTERIORART": "Interior Art",
}
function scrapeBook(doc = document) {
const contentEl = doc.querySelector("#wrap div.ContentBox:has(.recordID)");
if (contentEl == undefined) throw new Error("Failed to find book element");
const clonedContent = contentEl.cloneNode(true);
const recordEl = clonedContent.querySelector(".recordID");
recordEl.remove();
const notesEl = clonedContent.querySelector(".notes");
if (notesEl) {
notesEl.after(document.createElement("br"));
notesEl.remove();
}
const content = clonedContent.innerHTML.split(/<br>/i);
if (notesEl) content.push.apply(content, notesEl.innerHTML.split(/<br>/i));
clonedContent.querySelectorAll("sup.mouseover").forEach(i => i.innerText = " ")
let details = {};
recordEl.querySelector("b")?.remove();
details["Mappings"] = { "ISFDB Title": [cleanText(recordEl.textContent)] };
const container = document.createElement("div");
for (const con of content) {
container.innerHTML = con;
const labelEl = container.querySelector("b");
if (labelEl == undefined) {
console.log("label is not found", element);
continue;
}
let label = cleanText(labelEl.textContent.replace(":", ""));
labelEl.remove();
let value = cleanText(container.textContent);
if (label === "Date") {
const valSplit = value.split("-");
if (valSplit.length === 3) {
value = new Date(valSplit[0], Math.max(0, valSplit[1] - 1), valSplit[2]);
}
}
if (label === "Author" || label === "Editor") {
value = addContributor(details["Contributors"] ?? [], value, label);
label = "Contributors";
}
if (label === "Authors") {
let contrbutors = details["Contributors"] ?? [];
for (const author of container.querySelectorAll("a")) {
value = addContributor(contrbutors, cleanText(author.textContent), "Author");
}
label = "Contributors";
}
if (label === "Synopsis") {
value = getFormattedText(container);
}
details[label] = value;
}
details = remappings(details);
return details;
}
async function scrapeEdition() {
const contentEl = document.querySelector("#wrap div.ContentBox:has(.recordID)");
if (contentEl == undefined) throw new Error("Failed to find edition element");
const clonedContent = contentEl.cloneNode(true);
const titlesBoxEl = document.querySelector("#wrap div.ContentBox:not(:has(.recordID))");
if (titlesBoxEl == undefined) throw new Error("Failed to find book info element");
const recordEl = clonedContent.querySelector(".recordID");
recordEl.remove();
let details = {};
let mappings = {};
recordEl.querySelector("b")?.remove();
mappings["ISFDB Edition"] = [cleanText(recordEl.textContent)];
clonedContent.querySelectorAll("sup.mouseover").forEach(i => i.innerText = " ")
const coverData = getCoverData(clonedContent.querySelector("img.scan")?.src);
const listElements = clonedContent.querySelectorAll(".pubheader>ul>li")
for (const element of listElements) {
const labelEl = element.querySelector("b");
if (labelEl == undefined) {
console.log("label is not found", element);
continue;
}
let label = cleanText(labelEl.textContent.replace(":", ""));
labelEl.remove();
let value = cleanText(element.textContent);
if (label === "Date") {
const valSplit = value.split("-");
if (valSplit.length === 3) {
value = new Date(valSplit[0], Math.max(0, valSplit[1] - 1), valSplit[2]);
}
}
if (label === "Type") {
if (value in novelTypes) {
value = novelTypes[value];
}
}
if (label === "Author" || label === "Editor") {
value = addContributor(details["contributors"] ?? [], value, label);
label = "contributors";
}
if (label === "Authors") {
let contrbutors = details["contributors"] ?? [];
for (const author of container.querySelectorAll("a")) {
value = addContributor(contrbutors, cleanText(author.textContent), "Author");
}
label = "contributors";
}
if (label === "ISBN") {
const isbns = value.split(" ");
while (isbns.length > 0) {
let isbn = isbns.pop();
if (!isbn) break;
isbn = isbn.replace(/[\[\] ]/g, "");
const length = isbn.replaceAll("-", "").length;
if (length === 10) {
details["ISBN-10"] = isbn;
} else if (length === 13) {
details["ISBN-13"] = isbn;
} else {
logMarian(`WARN: unknown isbn '${isbn}'`)
}
}
continue;
}
if (label === "Notes") {
value = getFormattedText(element);
}
if (label === "Pages") {
// get page primary count
const newValue = value.match(/(\d+(?!]))/g)?.map(Number)?.reduce((a, b) => a + b) ?? value;
if (newValue != value) {
details["Pages original"] = value;
value = newValue;
}
}
if (label === "External IDs") {
element.querySelectorAll("li").forEach(extId => {
const extNameEl = extId.querySelector("abbr");
if (extNameEl == undefined) {
console.log("abbrivation name not found", extId);
return;
}
let extName = cleanText(extNameEl.textContent);
extNameEl.remove();
console.log(extId)
let id = cleanText(extId.textContent.replace(":", ""));
mappings[extName] = mappings[extName] || [];
mappings[extName].push(id);
});
continue;
}
if (label === "Format") {
if (value.startsWith("hc")) {
value = "Hardcover";
} else if (value.startsWith("tp")) {
value = "Trade Paperback";
} else if (value.startsWith("pb")) {
value = "Paperback";
} else if (value.startsWith("digest") && value.includes("magazine")) {
value = "Magazine";
}
}
details[label] = value;
}
details = remappings(details);
details["Reading Format"] = normalizeReadingFormat(details["Edition Format"]);
// get link for main title, either one that matches the book, or the first one
const titlesLi = [...titlesBoxEl.querySelectorAll("li")];
const titleLinks = titlesLi
.map(i => [...i.querySelectorAll("a")])
.flat()
.filter(i => i.href.includes("title.cgi"));
let titleLink = (titleLinks
.filter(i => cleanText(i.textContent) === details["Title"])[0]
?? titleLinks[0]
)?.href;
// if there is more then one book then don't fetch details for only one
if (details["Type"]?.toUpperCase() === "OMNIBUS") titleLink = undefined;
const bookDetailsPromise = new Promise((resolve, reject) => {
if (titleLink == undefined) {
resolve({});
return;
}
fetchHTML(titleLink)
.then((doc) => {
if (doc == undefined) {
reject("No document");
return;
}
resolve(scrapeBook(doc));
})
.catch(reject);
});
details = await collectObject([
bookDetailsPromise,
coverData,
details,
]);
details["Mappings"] = await collectObject([
details["Mappings"],
mappings,
]);
if ("contributors" in details) {
let contrbutors = details["Contributors"] ?? [];
for (const { name, roles } of details["contributors"]) {
addContributor(contrbutors, name, roles);
}
details["Contributors"] = contrbutors;
delete details["contributors"];
}
return details;
}
export { isfdbScraper };