-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
468 lines (384 loc) · 15.8 KB
/
index.js
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import PromptSync from "prompt-sync";
import fetch from "node-fetch";
import { parseStringPromise as parseString } from "xml2js";
import SVGtoPDF from "svg-to-pdfkit";
import PDFDocument from "pdfkit";
import PDFMerger from "pdf-merger-js";
import fs from "fs";
import path from "path";
import aesjs from "aes-js";
import forge from "node-forge";
import yargs from "yargs";
const argv = yargs(process.argv.slice(2))
.option("username", {
alias: "u",
type: "string",
description: "Username(email)",
})
.option("password", {
alias: "p",
type: "string",
description: "Password",
})
.option("isbn", {
alias: "i",
type: "string",
description: "ISBN",
})
.option("booktab-isbn", {
alias: "b",
type: "string",
description: "Overwrite the booktab ISBN (which is different from the normal ISBN)",
})
.help()
.alias("help", "h")
.argv;
const prompt = PromptSync({ sigint: true });
PDFDocument.prototype.addSVG = function (svg, x, y, options) {
return SVGtoPDF(this, svg, x, y, options), this;
};
async function decryptFile(encryptionKey, encryptedData) {
const aesCtr = new aesjs.ModeOfOperation.cbc(Buffer.from(encryptionKey, "utf8"), Buffer.from(encryptionKey, "utf8"));
let decryptedBytes = aesCtr.decrypt(Buffer.from(encryptedData, "base64"));
for(let i=16;i>0;i--){
if (decryptedBytes.slice(decryptedBytes.length-i).every(e=>e==i)) {
decryptedBytes = decryptedBytes.slice(0, decryptedBytes.length-i);
break;
}
}
const decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
return decryptedText;
}
async function downloadKitabooBook(bookReaderUrl) {
bookReaderUrl = new URL(bookReaderUrl.hash.substring(1), 'https://webreader.zanichelli.it');
let bookID = bookReaderUrl.searchParams.get('bookID');
let usertoken = bookReaderUrl.searchParams.get('usertoken');
console.log("Exchangin usertoken...");
usertoken = await fetch(`https://microservices.kitaboo.eu/v1/zanichelli/user/123/pc/validateUserToken?usertoken=${encodeURIComponent(usertoken)}`).then(res => res.json()).then(res => res.userToken).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
console.log("Fetching book details...");
let bookDetails = await fetch(`https://zanichelliservices.kitaboo.eu/DistributionServices/services/api/reader/distribution/123/pc/book/details?bookID=${bookID}`, {
headers: { usertoken },
}).then((res) => res.json()).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
let ebookID = bookDetails.bookList[0].book.ebookID;
console.log("Obtaining encryption encryption key..."); // yeah, that's not a typo
let downloadBook = await fetch(`https://webreader.zanichelli.it/downloadapi/auth/contentserver/book/123234234/HTML5/${bookID}/downloadBook?state=online`, {
headers: { usertoken },
}).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
let readerCookies = downloadBook.headers.raw()['set-cookie'].map((cookie) => cookie.split(';')[0]).join('; ');
downloadBook = await downloadBook.json();
let rawPrivateKey = downloadBook.privateKey;
let jwtToken = downloadBook.jwtToken; // note how jwt = json web token, so what you are saying is json web token token... gg
console.log("Fetching encrypted encryption key...")
let encryptedEncryptionKey = await fetch(`https://webreader.zanichelli.it/${ebookID}/html5/${ebookID}/OPS/enc_resource.key`, {
headers: { authorization: jwtToken, cookie: readerCookies },
}).then((res) => res.text()).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
console.log("Processing...");
console.log("Decrypting encryption key...");
let privateKey = "-----BEGIN RSA PRIVATE KEY-----\n";
privateKey += rawPrivateKey.match(/.{1,64}/g).join('\n');
privateKey += "\n-----END RSA PRIVATE KEY-----";
let key = forge.pki.privateKeyFromPem(privateKey);
let encryptionKey = key.decrypt(forge.util.decode64(encryptedEncryptionKey));
console.log("Fetching book content...");
let content = await fetch(`https://webreader.zanichelli.it/${ebookID}/html5/${ebookID}/OPS/content.opf`, { headers: { cookie: readerCookies } }).then((res) => res.text()).then(parseString).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
// mention in content.metadata of render type, could be usefull in the future if other formats get added
if (content.Error) {
console.log("Error: ", ...content.Error.Code, ...content.Error.Message);
process.exit(1);
}
let title = content.package.metadata[0]["dc:title"][0];
let items = {};
for (let item of content.package.manifest[0].item) {
if (['image/svg+xml', 'image/png', 'image/jpeg'].includes(item.$['media-type'])) items[item.$.id] = item.$.href;
}
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream(title.replace(/[^a-z0-9]/gi, '_') + '.pdf'));
for (let [i, itemref] of content.package.spine[0].itemref.entries()) {
console.log(`Downloading ${itemref.$.idref}`);
if (items[`images${itemref.$.idref}svgz`] !== undefined) {
let svg = null;
while (!svg) {
const abortController = new AbortController();
const promise = fetch(
`https://webreader.zanichelli.it/${ebookID}/html5/${ebookID}/OPS/${items[`images${itemref.$.idref}svgz`]}`,
{ headers: { cookie: readerCookies }, controller: abortController.signal }
).then(async (res) => {
return decryptFile(encryptionKey, await res.text());
});
const timeoutId = setTimeout(() => abortController.abort(), 10000);
svg = await promise;
clearTimeout(timeoutId);
}
doc.addSVG(svg, 0, 0, { preserveAspectRatio: "xMinYMin meet" });
} else if (items[`images${itemref.$.idref}png`] !== undefined) {
let png = null;
while (!png) {
const abortController = new AbortController();
const promise = fetch(
`https://webreader.zanichelli.it/${ebookID}/html5/${ebookID}/OPS/${items[`images${itemref.$.idref}png`]}`,
{ headers: { cookie: readerCookies }, controller: abortController.signal }
).then(async (res) => {
return decryptFile(encryptionKey, await res.text());
});
const timeoutId = setTimeout(() => abortController.abort(), 10000);
png = await promise;
clearTimeout(timeoutId);
}
doc.image(png, 0, 0, {fit: [doc.page.width, doc.page.height], align: 'center', valign: 'center'});
} else if (items[`images${itemref.$.idref}jpg`] !== undefined) {
let jpeg = null;
while (!jpeg) {
const abortController = new AbortController();
const promise = fetch(
`https://webreader.zanichelli.it/${ebookID}/html5/${ebookID}/OPS/${items[`images${itemref.$.idref}jpg`]}`,
{ headers: { cookie: readerCookies }, controller: abortController.signal }
).then(async (res) => {
return decryptFile(encryptionKey, await res.body());
});
const timeoutId = setTimeout(() => abortController.abort(), 5000)
jpeg = await promise;
clearTimeout(timeoutId);
}
doc.image(jpeg, 0, 0, {fit: [doc.page.width, doc.page.height], align: 'center', valign: 'center'});
} else {
console.log(`Unable to find suitable format for ${itemref.$.idref}`);
}
if (i < content.package.spine[0].itemref.length - 1) doc.addPage();
}
doc.end();
console.log("Done! You'll find the PDF in the directory of the script");
}
async function downloadBookTabBook(redirectUrl, cookie) { // bookReaderUrl,
//let idOpera = bookReaderUrl.searchParams.get('idOpera');
let isbn = redirectUrl.split('/');
isbn = argv["booktab-isbn"] || isbn[isbn.length - 1];
console.log("Accessing booktab...");
let bookTabSession = await fetch('https://web-booktab.zanichelli.it/api/v1/sessions_web', {
method: 'POST',
headers: { cookie },
}).then((res) => res.json()).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
cookie += `; booktab_token=${bookTabSession.session}`;
/*console.log("Fetching available books...");
let bookTabBooks = await fetch('https://web-booktab.zanichelli.it/api/v5/metadata', {
headers: { cookie },
}).then((res) => res.json()).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
let candidates = bookTabBooks.books.filter((book) => book.idBookZ == idOpera);
console.log("Available books:"); // why is there a second list of books? I don't know, no idea, it just goes to show how shitty zanichelli's software is
console.table(candidates, ['title', 'idBookZ', 'isbn']);*/ // probably no longer needed
while (!isbn)
isbn = prompt("ISBN: ");
console.log("Fetching book details...");
let spine = await fetch(`https://web-booktab.zanichelli.it/api/v1/resources_web/${isbn}/spine.xml`, {
headers: { cookie },
}).then(async (res) => {
if (res.status == 404) {
return fetch(`https://web-booktab.zanichelli.it/api/v1/resources_web/${isbn}/volume.xml`, {
headers: { cookie },
}).then((res) => {
if (res.status == 404) {
console.log('Looks like this is not a downloadable book, try another one.');
process.exit(1);
}
return res.text()
});
}
return res.text();
}).then(parseString).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
const title = (spine.spine || spine.config.volume[0].settings[0]).volumetitle[0].trim().replace(/[^a-z0-9]/gi, '_');
console.log("Downloading book...");
const units = (spine.spine ? spine.spine.unit : spine.config.volume[0].units[0].unit).map((unit) => {
if (unit.$.features == 'flash') return null;
return unit.$.btbid;
}).filter((unit) => unit != null);
let pdfMerger = new PDFMerger();
let isXps = false;
for(let i = 0; i < units.length; i++) {
console.log(`Downloading unit ${i + 1} of ${units.length}`)
const unit = units[i];
let config = await fetch(`https://web-booktab.zanichelli.it/api/v1/resources_web/${isbn}/${unit}/config.xml`, {
headers: { cookie },
}).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
if (config.status != 200) continue;
config = await config.text().then(parseString);
let pdfUrl = config.unit.content[0];
if (config.unit.filesMap) {
pdfUrl = config.unit.filesMap[0].entry.find((file) => file.$.key == config.unit.content[0] + '.pdf')._;
}
if (isXps) {
let xps = await fetch(`https://web-booktab.zanichelli.it/api/v1/resources_web/${isbn}/${unit}/${config.unit.content[0]}.xod`, {
headers: { cookie },
}).then((res) => res.buffer()).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
await fs.promises.writeFile(path.join("xps_" + title, `${i}_${unit}.xps`), xps);
continue;
}
let pdf = await fetch(`https://web-booktab.zanichelli.it/api/v1/resources_web/${isbn}/${unit}/${pdfUrl}.pdf`, {
headers: { cookie },
}).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
if (pdf.status == 404) {
isXps = true;
i = 0; // restart the loop
console.log("DETECTED XPS FORMAT, DOWNLOADING INDIVIDUAL UNITS...");
await fs.promises.mkdir("xps_" + title, { recursive: true }); // adding prefix to gitignore
continue;
}
pdf = await pdf.buffer();
await pdfMerger.add(pdf);
}
if (isXps) {
console.log("Done! You'll find the XPS files in the directory of the script");
console.log("Instructions:");
console.log("1. A folder with the name of the book will be created, containing all the units in XPS format");
console.log("2. Navigate to https://xpstopdf.com/ and convert the XPS files to PDF format");
console.log("3. Merge the PDF files using https://www.ilovepdf.com/merge_pdf");
console.log("If anyone would like to contribute a script to automate this process, feel free to do so");
} else {
console.log("Saving...");
await pdfMerger.save(title + '.pdf');
console.log("Done! You'll find the PDF in the directory of the script");
}
}
(async () => {
let {username, password} = argv;
while (!username)
username = prompt("Username(email): ");
while (!password)
password = prompt("Password: ");
console.log("Logging in...");
let token = await fetch("https://idp.zanichelli.it/v4/login/", {
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded",
},
body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`,
}).then((res) => res.json()).then((res) => res.token).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
let cookie = `token=${token}`;
let loginCookies = await fetch("https://my.zanichelli.it/?loginMode=myZanichelli", {
headers: { cookie },
}).then((res) => res.headers.raw()['set-cookie'].map((cookie) => cookie.split(';')[0])).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
let dashboardCookies = {};
for (let loginCookie of loginCookies) {
let [key, value] = loginCookie.split('=');
dashboardCookies[key] = value;
}
console.log("Fetching available books...");
/*await fetch('https://api-catalogo.zanichelli.it/v3/dashboard/init', {
headers: { 'myz-token': dashboardCookies['myz_token'] },
}).then(res => res.text()).then(console.log).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});*/ // keeping this, perhaps it's needed in the future
await fetch('https://api-catalogo.zanichelli.it/v3/dashboard/user', {
headers: { 'myz-token': dashboardCookies['myz_token'] },
}).then(res => res.json()).then((res) => {
console.log(`Logged in as: ${res.firstName} ${res.lastName}`)
}).catch((err) => {
console.log("Error: ", err);
process.exit(1);
}); // we don't really care about the response, but apparently it's required to access the book list
let books = {};
let page = 1;
let notATeacher = false;
while (true) {
let response = await fetch(`https://api-catalogo.zanichelli.it/v3/dashboard/search?sort%5Bfield%5D=year_date&sort%5Bdirection%5D=desc&searchString&pageNumber=${page}&rows=100`, {
headers: { 'myz-token': dashboardCookies['myz_token'] },
}).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
if (response.status == 403) {
notATeacher = true;
break;
}
response = await response.json();
if (response.data.pagination.pages == 0) {
console.log("No books found");
process.exit(0);
}
for (let license of response.data.licenses) {
if (license.volume.ereader_url == '') continue;
books[license.volume.isbn] = {
title: license.volume.opera.title,
ereader_url: license.volume.ereader_url,
isbns: license.volume.isbns,
}
}
if (response.data.pagination.pages == page) break;
page++;
}
if (notATeacher) {
let request = await fetch('https://api-catalogo.zanichelli.it/v3/dashboard/licenses/real', {
headers: { 'myz-token': dashboardCookies['myz_token'] },
}).then((res) => res.json()).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
for (let license of request.realLicenses) {
if (license.volume.ereader_url == '') continue;
books[license.volume.isbn] = {
title: license.volume.opera.title,
ereader_url: license.volume.ereader_url,
isbns: license.volume.isbns
}
}
}
console.log("Available books:");
console.table(books, ['title']);
let isbn = argv.isbn;
while (!isbn)
isbn = prompt("ISBN: ");
console.log("Detecting reader...");
let bookReaderUrl = await fetch(books[isbn].ereader_url, {
headers: { cookie },
redirect: 'manual',
}).then((res) => res.headers.get('location')).catch((err) => {
console.log("Error: ", err);
process.exit(1);
});
bookReaderUrl = new URL(bookReaderUrl);
if (bookReaderUrl.host == 'web-booktab.zanichelli.it') {
console.log("BookTab book detected");
await downloadBookTabBook(books[isbn].ereader_url, cookie);
} else {
console.log("Kitaboo book detected");
await downloadKitabooBook(bookReaderUrl);
}
})();