-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathamazon.js
More file actions
307 lines (256 loc) · 9.7 KB
/
amazon.js
File metadata and controls
307 lines (256 loc) · 9.7 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
import { Extractor } from "./AbstractExtractor.js"
import { logMarian, getFormattedText, getCoverData, addContributor, cleanText, normalizeReadingFormat, collectObject } from '../shared/utils.js';
const bookSeriesRegex = /^Book (\d+) of \d+$/i;
const includedLabels = new Set([
'Contributors',
'Publisher',
'Publication date',
'Program Type',
'Language',
'Print length',
'Listening Length',
'ISBN-10',
'ISBN-13',
'ASIN',
'Series',
'Series Place'
]);
class amazonScraper extends Extractor {
get _name() { return "Amazon Extractor"; }
needsReload = false;
_sitePatterns = [
/https:\/\/www\.amazon\..*?\/(?:dp|gp\/product)\/.*?(B[\dA-Z]{9}|\d{9}(?:X|\d))/,
/https:\/\/www\.amazon\.[a-z.]+\/(?:gp\/product|dp|[^/]+\/dp)\/[A-Z0-9]{10}/,
/https:\/\/www\.amazon\.[a-z.]+\/[^/]+\/dp\/[A-Z0-9]{10}/,
/https:\/\/www\.amazon\.[a-z.]+\/-\/[a-z]+\/[^/]+\/dp\/[A-Z0-9]{10}/, // for paths with language segments
];
async getDetails() {
const coverData = getCover();
const bookDetails = getDetailBullets();
const audibleDetails = getAudibleDetails();
const contributors = extractAmazonContributors();
bookDetails["Edition Format"] = getSelectedFormat() || '';
bookDetails["Title"] = document.querySelector('#productTitle')?.innerText.trim();
bookDetails["Description"] = getBookDescription() || '';
bookDetails["Contributors"] = contributors;
// TODO: get the goodreads id for the novel
bookDetails['Reading Format'] = normalizeReadingFormat(bookDetails["Edition Format"]);
if (bookDetails['Reading Format'] === 'Ebook') {
// Normalize `Kindle Edition` to to `Kindle` like it is on amazon.com
bookDetails["Edition Format"] = "Kindle";
}
// combined publisher date
const pubDate = bookDetails["Publisher"]?.match(/^(?<pub>[^(;]+?)(?:; (?<edition>[\w ]+))? \((?<date>\d{1,2} \w+ \d{4})\)$/);
if (pubDate != undefined) {
bookDetails["Publisher"] = pubDate.groups["pub"].trim();
bookDetails["Publication date"] = pubDate.groups["date"];
if (pubDate.groups["edition"]) {
bookDetails["Edition Information"] = pubDate.groups["edition"].trim();
}
}
// Fill in Edition Info from Version or Edition
const version = bookDetails['Version'] || audibleDetails['Version'];
const edition = bookDetails["Edition"];
if (!!version && !!edition) { // if both edition and version are present mix them
bookDetails["Edition Information"] = `${edition}; ${version}`;
} else { // otherwise pick one or leave it undefined if neither exist
bookDetails["Edition Information"] = edition || version;
}
// If the isbn10 is the isbn13 and is in the ASIN
const isbn10 = bookDetails["ISBN-10"]?.replace("-", "");
const isbn13 = bookDetails["ISBN-13"]?.replace("-", "");
const asin = bookDetails["ASIN"];
if (
isbn10 != null &&
isbn13 != null &&
asin != null &&
asin.length === 10 &&
isbn10.length === 13 &&
isbn10 === isbn13 &&
!/[a-z]/i.test(asin)
) {
bookDetails["ISBN-10"] = asin;
}
const mergedDetails = await collectObject([
bookDetails,
audibleDetails,
coverData,
]);
delete mergedDetails.Edition;
delete mergedDetails.Version;
// logMarian("details", mergedDetails);
return mergedDetails;
}
}
async function getCover() {
const imgEl = document.querySelector("#landingImage, #imgTagWrapperId img"); // same element
const imgEl2 = document.querySelector("#imgBlkFront");
const imgEl3 = document.querySelector("#ebooksImgBlkFront");
const imgAudible = document.querySelector('#audibleProductImage img');
const covers = new Set();
[imgEl, imgEl2, imgEl3, imgAudible].forEach(img => {
if (!img) return;
if (img) covers.add(img.src);
const dataset = img.dataset;
if (dataset) {
if (dataset.oldHires) covers.add(dataset.oldHires);
// add highest res dynamic
try {
const dynamicImage = JSON.parse(dataset.aDynamicImage);
const largest = Object.entries(dynamicImage).reduce((acc, [url, [height, width]]) => {
const currentScore = width * height;
return currentScore > acc.score ? { url, score: currentScore } : acc;
}, { url: null, score: 0 }).url;
if (largest) covers.add(largest);
} catch (err) {
logMarian('Error parsing dynamic image data:', err);
}
}
});
// get original image
covers.forEach((value) => value && covers.add(getHighResImageUrl(value)));
const coverList = Array.from(covers)
.filter((x) => !x.includes("01RmK+J4pJL.gif")); // filter out no image image
console.log(coverList)
const coverRes = await getCoverData(coverList);
if (coverRes.imgScore === 0) return {}
return coverRes;
}
function getHighResImageUrl(src) {
return src.replace(/\._[^.]+(?=\.)/, '');
}
function getDetailBullets() {
const bullets = document.querySelectorAll('#detailBullets_feature_div li');
const details = {};
bullets.forEach(li => {
// Identify the edition labels, skip if not found
const labelSpan = li.querySelector('span.a-text-bold');
if (!labelSpan) return;
// Clean up label text
let label = cleanText(labelSpan.textContent.replace(':', ''));
// Fetch and clean the value of the detail
const valueSpan = labelSpan.nextElementSibling;
let value = cleanText(valueSpan?.textContent);
// Handle book series special case
const match = bookSeriesRegex.exec(label) || bookSeriesRegex.exec(value);
if (match) {
details['Series'] = value;
details['Series Place'] = match[1];
return;
}
if ((label === 'Edition' || label === 'Version') && value) {
details[label] = value;
if (label === 'Edition' && !details['Edition Format']) {
details['Edition Format'] = value;
}
return;
}
// Print debug info for labels not included
// skip if not included in the list
if (!includedLabels.has(label)) {
// logMarian(`Label not currently included: "${label}"`);
return;
}
// Final check that both label and value are present
if (!label || !value) return;
// Rename "Print length" to "Pages" and extract number only
if (label === 'Print length') {
label = 'Pages';
const pageMatch = value.match(/\d+/);
value = pageMatch ? pageMatch[0] : value;
}
details[label] = value;
});
// Double check book series
const series = document.querySelector("div[data-feature-name='seriesBulletWidget'] a")
if (!details["Series"] && series != undefined) {
const match = series.textContent.trim().match(/Book (\d+) of \d+: (.+)/i);
if (match) {
details['Series'] = match[2];
details['Series Place'] = match[1];
}
}
return details;
}
function getAudibleDetails() {
const table = document.querySelector('#audibleProductDetails table');
if (!table) return {};
const details = {};
const rows = table.querySelectorAll('tr');
rows.forEach(row => {
const label = row.querySelector('th span')?.textContent?.trim();
const value = row.querySelector('td')?.innerText?.trim();
const match = bookSeriesRegex.exec(label) || bookSeriesRegex.exec(value);
// Handle book series special case
if (match) {
details['Series'] = value;
details['Series Place'] = match[1];
return;
}
if ((label === 'Edition' || label === 'Version') && value) {
details[label] = value;
if (label === 'Edition' && !details['Edition Format']) {
details['Edition Format'] = value;
}
return;
}
// Match any Audible.<TLD> Release Date
if (/^Audible\.[^\s]+ Release Date$/i.test(label)) {
details['Publication date'] = value;
} else if (label === 'Audible.com Release Date') {
details['Publication date'] = value;
} else if (label === 'Program Type') {
details['Reading Format'] = value;
details['Edition Format'] = "Audible";
} else if (label === 'Listening Length') {
const timeMatch = value.match(/(\d+)\s*hours?\s*(?:and)?\s*(\d+)?\s*minutes?/i);
if (timeMatch) {
const arr = [];
if (timeMatch[1]) arr.push(`${timeMatch[1]} hours`);
if (timeMatch[2]) arr.push(`${timeMatch[2]} minutes`);
details['Listening Length'] = arr;
} else {
details['Listening Length'] = value;
}
} else if (label && value && includedLabels.has(label)) {
details[label] = value;
}
});
return details;
}
function getBookDescription() {
const container = document.querySelector('#bookDescription_feature_div .a-expander-content');
if (!container) return '';
return getFormattedText(container);
}
function getSelectedFormat() {
const selected = document.querySelector('#tmmSwatches .swatchElement.selected .slot-title span[aria-label]');
if (selected) {
return selected.getAttribute('aria-label')?.replace(' Format:', '').trim();
}
return null;
}
function extractAmazonContributors() {
const contributors = [];
const authorSpans = document.querySelectorAll('#bylineInfo .author');
authorSpans.forEach(span => {
const name = span.querySelector('a')?.innerText.trim();
const roleText = span.querySelector('.contribution span')?.innerText.trim();
let roles = [];
if (roleText) {
// e.g., "(Author)", "(Illustrator)", "(Author, Narrator)"
const roleMatch = roleText.match(/\(([^)]+)\)/);
if (roleMatch) {
// Split by comma and trim each role
roles = roleMatch[1].split(',').map(r => r.trim());
}
} else {
roles.push("Contributor"); // fallback if role is missing
}
// Ignore if any role is Publisher
if (roles.includes("Publisher")) return;
if (name) addContributor(contributors, name, roles);
});
return contributors;
}
export { amazonScraper };