-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbookshopOrg.js
More file actions
102 lines (85 loc) · 2.95 KB
/
bookshopOrg.js
File metadata and controls
102 lines (85 loc) · 2.95 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
import { addContributor, cleanText, collectObject, getCoverData, getFormattedText, normalizeReadingFormat } from "../shared/utils.js";
import { Extractor } from "./AbstractExtractor.js"
class bookshopScraper extends Extractor {
get _name() { return "bookstore.org Extractor"; }
needsReload = false;
_sitePatterns = [
/https:\/\/bookshop\.org\/p\/books\/(.+)/,
];
async getDetails() {
let details = {};
const ldInfoJsonEl = document.querySelector("script[type='application/ld+json']");
const ldInfo = JSON.parse(ldInfoJsonEl.textContent);
console.log("info", ldInfo);
// cover
const coverData = getCoverData(ldInfo["image"].filter(i => !i.includes("?v=enc-v1")));
// isbn
if (ldInfo["isbn"].length === 13) {
details["ISBN-13"] = ldInfo["isbn"];
} else {
if (ldInfo["isbn"].length === 10) {
details["ISBN-10"] = ldInfo["isbn"];
}
}
// description
const description = document.createElement("div");
description.innerHTML = ldInfo["description"];
details["Description"] = getFormattedText(description);
// date
// details["Publication date"] = ldInfo["datePublished"];
details["Publication date"] = document.querySelector("meta[name='book:release_date']").content;
// title
details["Title"] = ldInfo["name"];
const subtitle = document.querySelector("meta[name='description']").content;
if (subtitle) {
details["Title"] = `${details["Title"]}: ${subtitle}`;
}
// format
details["Edition Format"] = ldInfo["bookFormat"];
details["Reading Format"] = normalizeReadingFormat(ldInfo["bookFormat"]);
// length
if ("numberOfPages" in ldInfo && ldInfo["numberOfPages"] > 0) {
details["Pages"] = ldInfo["numberOfPages"];
}
// author(s)
let contributors = [];
for (const author of ldInfo["author"]) {
if (author && author.name) {
addContributor(contributors, author.name, "Author");
}
}
details["Contributors"] = contributors;
// publisher
details["Publisher"] = ldInfo["publisher"]?.name;
// language (and potentially other fields)
const table = document.querySelector("details table");
for (const row of table.querySelectorAll("tr")) {
const [key, val] = row.children;
const keyname = cleanText(key.textContent);
const value = cleanText(val.textContent);
if (keyname === "Language") {
details["Language"] = value;
continue;
}
if (keyname === "Dimensions") {
if (value === "N/A") continue;
value.split("|").forEach(component => {
if (component.includes(" g")) {
details["Weight"] = component.trim();
return;
}
if (component.includes("mm")) {
details["Dimensions"] = component.trim();
return;
}
});
continue;
}
}
return collectObject([
details,
coverData
]);
}
}
export { bookshopScraper };