-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathkobo.js
More file actions
186 lines (162 loc) · 6.54 KB
/
kobo.js
File metadata and controls
186 lines (162 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
import { Extractor } from "./AbstractExtractor.js";
import { addContributor, getCoverData, logMarian, cleanText, normalizeReadingFormat, collectObject } from "../shared/utils.js";
class koboScraper extends Extractor {
get _name() { return "Kobo Extractor"; }
_sitePatterns = [
/^https?:\/\/(www\.)?kobo\.[a-z]{2,10}\/[a-z]{2,5}\/[a-z]{2,5}\/[a-z]{1,5}book\/[0-9a-z\-]+/,
];
async getDetails() {
const bookDetails = {};
// image and imageScore
const imggrab = document.querySelector('.item-image .image-actions img.cover-image');
const coverData = getCoverData(imggrab?.src); //, getHighResImageUrl(imggrab?.src)]) -- this function is not doing anything -- skipping
// Source ID
// const sourceId = getKoboIdFromUrl(window.location.href);
// if (sourceId) bookDetails["Mappings"] = { "Kobo": [sourceId] };
// Title
getKoboBookTitle(bookDetails);
// Series name and number
getKoboSeries(bookDetails);
// Contributors
extractKoboContributors(bookDetails);
//get format and length
getKoboFormatInfo(bookDetails, window.location.href)
// get extra block of info - isbn, language, etc.
extraKoboInfo(bookDetails);
// Description
extractKoboDescription(bookDetails);
// logMarian("Kobo extraction complete:", bookDetails);
return collectObject([
coverData,
bookDetails
]);
}
}
// seems like a thing we do
function getHighResImageUrl(src) {
// return src.replace(/\/compressed\.photo\./, '/');
return src
}
function getKoboIdFromUrl(url) {
const regex = /book\/([^/?]+)/i;
const match = url.match(regex);
return match ? match[1] : null;
}
function extractKoboContributors(bookDetails) {
const contributors = [];
const authorray = document.querySelectorAll('.about .authors .visible-contributors a');
if (authorray) {
for (let c = 0; c < authorray.length; c++) {
let authorName = authorray[c].textContent;
addContributor(contributors, authorName, "Author");
}
}
const narratoray = document.querySelectorAll('.about .narrators .visible-contributors a');
if (narratoray) {
for (let c = 0; c < narratoray.length; c++) {
let narName = narratoray[c].textContent;
addContributor(contributors, narName, "Narrator");
}
}
if (contributors.length) {
bookDetails["Contributors"] = contributors;
}
}
function getKoboSeries(bookDetails) {
if (document.querySelector('.product-sequence-field a')) {
let seriesInfoName = document.querySelector('.product-sequence-field a');
let name = seriesInfoName.textContent.trim();
bookDetails['Series'] = name;
let seriesPlace = document.querySelector('.sequenced-name-prefix');
if (seriesPlace.textContent.match(/\d+/) > 0) {
let seriesNum = seriesPlace.textContent.trim();
let number = seriesNum.match(/\d+/);
bookDetails['Series Place'] = number[0];
}
}
}
function getKoboBookTitle(bookDetails) {
const h1 = document.querySelector('.title-widget h1');
const rawTitle = cleanText(h1?.childNodes[0]?.textContent);
rawTitle ? bookDetails["Title"] = rawTitle : null;
}
function getKoboFormatInfo(bookDetails, url) {
const formax = /audiobook/i;
if (url.match(formax)) {
bookDetails['Reading Format'] = 'Audiobook';
let audio = document.querySelectorAll(".metadata-field.metadata-time .metadata");
let audioLength = Math.abs(audio[0].textContent);
let hours = Math.floor(audioLength);
let mins = Math.round(((audioLength - hours) * 60));
if (audioLength < 1) {
bookDetails['Listening Length'] = [mins + " minutes"];
} else {
bookDetails['Listening Length'] = [hours + " hours ", mins + " minutes"];
}
} else {
bookDetails['Reading Format'] = 'Ebook';
let pageCount = document.querySelector('.stat-desc strong');
if (pageCount) {
bookDetails['Pages'] = pageCount.textContent;
}
}
// Normalize reading format just in case
bookDetails['Reading Format'] = normalizeReadingFormat(bookDetails['Reading Format']);
// TODO: see if edition format can be extracted (see download options)
}
function joinContent(elements) {
return Array.from(elements).map(item => cleanText(item.textContent)).join("\n");
}
function extractKoboDescription(bookDetails) {
const descriptionEl = document.querySelectorAll('#synopsis div[data-full-synopsis] p');
const fallbackDescriptionEl = document.querySelectorAll('.synopsis-description p');
let description = null;
if (descriptionEl) {
description = joinContent(descriptionEl);
} else if (fallbackDescriptionEl) {
description = joinContent(fallbackDescriptionEl);
}
bookDetails["Description"] = description;
}
function extraKoboInfo(bookDetails) {
const extraMetadata = document.querySelectorAll(".bookitem-secondary-metadata ul li");
const extrainfo = [];
for (let lindex = 0; lindex < extraMetadata.length; lindex++) {
let mytext = extraMetadata[lindex].textContent;
if (lindex == 0) {
extrainfo['Publisher'] = mytext.trim;
} else {
let [a, b] = mytext.split(':');
extrainfo[a.trim()] = b.trim();
}
}
for (let label in extrainfo) {
if (!label) break;
switch (label) {
case 'Release Date':
bookDetails['Publication date'] = extrainfo[label];
break;
case 'Imprint':
// if the imprint and publisher are the same, use the publisher; otherwise use the imprint
// left in case we need to revisit to include both
if (extrainfo['Publisher'] == extrainfo[label]) {
bookDetails['Publisher'] = extrainfo['Publisher'];
} else {
bookDetails['Publisher'] = extrainfo[label];
}
break;
case 'Book ID':
if (extrainfo[label].length == 13) {
bookDetails['ISBN-13'] = extrainfo[label];
}
else if (extrainfo[label].length == 10) {
bookDetails['ISBN-10'] = extrainfo[label];
}
break;
case 'Language':
bookDetails['Language'] = extrainfo[label];
break;
}
}
}
export { koboScraper };