-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlibrarything.js
More file actions
247 lines (200 loc) · 6.54 KB
/
librarything.js
File metadata and controls
247 lines (200 loc) · 6.54 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
import { Extractor } from "./AbstractExtractor.js";
import { collectObject, cleanText, fetchHTML, getCoverData, getFormattedText, addContributor, remapKeys, addMapping } from "../shared/utils.js";
class libraryThingScraper extends Extractor {
get _name() { return "LibraryThing Extractor"; }
needsReload = false;
_sitePatterns = [
/https:\/\/www\.librarything\.com\/+work\/+(\d+)\/*(?:(\d+)|work\/+(\d+))?/,
];
async getDetails() {
const details = await collectObject([
getCover(),
getFacts(),
getCommonKnowledge(),
getTitle(),
getDescription(),
getAuthor(),
]);
const idMatch = document.location.href.match(this._sitePatterns[0]);
const libraryThingId = idMatch[1];
if (libraryThingId) {
details["Mappings"] = addMapping(details["Mappings"] ?? {}, "LibraryThing", libraryThingId);
}
return details;
}
}
function getTitle() {
return {
"Title": cleanText(document.querySelector(`.work_info_area .h1_work_title`)?.textContent ?? "")
};
}
function getDescription() {
const descriptionElm = document.querySelector(`#section_description`);
if (!descriptionElm) return {};
const clonedDescription = descriptionElm.cloneNode(true);
const headerElm = clonedDescription.querySelector(`h2`);
headerElm.remove();
const showMoreElms = clonedDescription.querySelectorAll(`.showlessmore_link`);
for (const showMoreElm of showMoreElms) showMoreElm.remove();
return {
"Description": getFormattedText(clonedDescription)
};
}
function getAuthor() {
const authorElm = document.querySelector(`.h3_work_author`);
if (!authorElm) return {};
const contributors = [];
const authors = authorElm.querySelectorAll(`span.ltil_item`);
for (const author of authors) {
let name = cleanText(author.textContent).replace(/,$/, "");
let role = "Author";
const match = name.match(/(.+) \((.*)\)/);
if (match) {
name = match[1];
role = match[2];
}
addContributor(contributors, name, role);
}
return {
"Contributors": contributors
};
}
const mappingDict = {
"Published": "Publication date",
"Original publication date": "Publication date",
}
function remappings(text) {
if (text in mappingDict) return mappingDict[text];
return text;
}
const nameRemap = remapKeys.bind(undefined, {
"Genres": undefined,
"Canonical title": undefined,
"Epigraph": undefined,
"Dedication": undefined,
"People/Characters": undefined,
"Important places": undefined,
"Important events": undefined,
"First words": undefined,
"Quotations": undefined,
"Last words": undefined,
"Blurbers": undefined,
"Disambiguation notice": undefined,
"Related movies": undefined,
"Alternate titles": "Alt Title",
});
function getFacts() {
const quickFactsElm = document.querySelector(`#section_quickfacts`);
if (!quickFactsElm) return {};
const listElm = quickFactsElm.querySelector("dl");
if (!listElm) return {};
let details = {};
for (let i = 0; i < listElm.children.length; i += 2) {
const titleElm = listElm.children[i];
const valueElm = listElm.children[i + 1];
if (!titleElm || !valueElm) continue;
if (!titleElm.classList.contains("fact")) continue;
let title = cleanText(titleElm.textContent);
let value = cleanText(valueElm.textContent);
title = remappings(title)
if (title === "Publication date" && value.match(/^\d+$/)) {
value = new Date(value, 0);
}
if (title === "LCC" || title === "DDC/MDS") {
value = addMapping(details["Mappings"] ?? {}, title, value)
title = "Mappings"
}
details[title] = value;
}
details = nameRemap(details);
return details;
}
function getCommonKnowledge() {
const commonKnowledgeElm = document.querySelector(`#section_common_knowledge`);
if (!commonKnowledgeElm) return {};
const listElm = commonKnowledgeElm.querySelector("dl");
if (!listElm) return {};
let details = {};
for (let i = 0; i < listElm.children.length; i += 2) {
const titleElm = listElm.children[i];
const valueElm = listElm.children[i + 1];
if (!titleElm || !valueElm) continue;
let title = cleanText(titleElm.textContent);
let value = cleanText(valueElm.textContent);
title = title.replace(/\*$/, "");
title = remappings(title)
if (title === "Publication date" && value.match(/^\d+$/)) {
value = new Date(value, 0);
} else if (title === "Publication date") {
value = new Date(value);
}
if (title === "Canonical DDC/MDS" || title === "Canonical LCC") {
// skip, is in quick facts
continue;
}
details[title] = value;
}
details = nameRemap(details);
if ("Original language" in details && !("Language" in details)) {
details["Language"] = details["Original language"];
delete details["Original language"];
}
return details;
}
async function getCover() {
const covers = new Set();
const coverContainer = document.querySelector(`#lt2_mainimage_container img`); if (coverContainer) {
covers.add(coverContainer.src);
coverContainer.srcset?.split(" ")
?.filter(x => x.includes("http"))
?.map(x => covers.add(x));
}
const bigCovers = await getBigCovers();
if (bigCovers) {
bigCovers.forEach(cover => {
if (cover) covers.add(cover);
});
}
covers.forEach(x => { if (x) covers.add(getHighResImageUrl(x)); });
return getCoverData([...covers]);
}
function getHighResImageUrl(src) {
return src.replace(/\._[^.]+(?=\.)/, "");
}
async function getBigCovers() {
const coverMag = document.querySelector("#covermag");
if (!coverMag) return [];
//covertype, coverid, workID, bookID
const params = coverMag.outerHTML
?.split("event,")[1]
?.split(")")[0]
?.split(",")?.map(x => x.replaceAll("'", "").trim());
if (!params || params.length < 2) return [];
const covertype = params[0];
const coverid = params[1];
const workID = params[2];
const bookID = params[3];
var myArray = coverid.split(':');
var theid = myArray[1];
var url = 'https://www.librarything.com/ajax_coverinfo_lt2.php';
var urlParams = new URLSearchParams({
covertype: covertype,
id: theid,
work: workID,
book: bookID,
workpage: 1
});
const dom = await fetchHTML(`${url}?${urlParams.toString()}`);
// console.log("dom", dom);
if (!dom) return [];
const covers = [];
const coverContainer = dom.querySelector(`#maincover_box img`);
if (coverContainer) {
covers.push(coverContainer.src);
coverContainer.srcset?.split(" ")
?.filter(x => x.includes("http"))
?.map(x => covers.push(x));
}
return covers;
}
export { libraryThingScraper };