-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
269 lines (222 loc) · 9.29 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
const prompt = require('prompt-sync')({sigint: true});
const fetch = require('node-fetch');
const msgpack = require('msgpack-lite');
const aesjs = require('aes-js');
const PDFDocument = require('pdf-lib').PDFDocument;
const fs = require('fs');
const sanitize = require("sanitize-filename");
const yargs = require('yargs/yargs');
const md5 = require('md5');
const { spawn } = require('child_process');
const path = require('path');
const argv = yargs(process.argv.slice(2))
.option('site', {
describe: 'The site to download from, currently either bsmart or digibook24',
type: 'string',
default: null
})
.option('siteUrl', {
describe: 'This overwrites the base url for the site, useful in case a new platform is added',
type: 'string',
default: null
})
.option('cookie', {
describe: 'Input "_bsw_session_v1_production" cookie',
type: 'string',
default: null
})
.option('bookId', {
describe: 'Book id',
type: 'string',
default: null
})
.option('downloadOnly', {
describe: 'Downloads the pages as individual pdfs and will provide a command that can be used to merge them with pdftk',
type: 'boolean',
default: false
})
.option('pdftk', {
describe: 'Downloads the pages as individual pdfs and merges them with pdftk',
type: 'boolean',
default: false
})
.option('pdftkPath', {
describe: 'Path to pdftk executable',
type: 'string',
default: 'pdftk'
})
.option('checkMd5', {
describe: 'Checks the md5 hash of the downloaded pages',
type: 'boolean',
default: false
})
.option('output', {
describe: 'Output filename',
type: 'string',
default: null
})
.option('resources', {
describe: 'Download resources of the book instrad of the book it self',
type: 'boolean',
default: false
})
.help()
.argv;
let key = new Uint8Array([30, 0, 184, 152, 115, 19, 157, 33, 4, 237, 80, 26, 139, 248, 104, 155]);
async function decryptFile(file) {
return new Promise(async (resolve, reject) => {
try {
let header = msgpack.decode(file.slice(0, 256));
let firstPart = file.slice(256, header.start);
let secondPart = new Uint8Array(file.slice(header.start));
var aesCbc = new aesjs.ModeOfOperation.cbc(key, firstPart.slice(0, 16));
var decryptedFirstPart = aesCbc.decrypt(firstPart.slice(16));
for(let i=16;i>0;i--){
if (decryptedFirstPart.slice(decryptedFirstPart.length-i).every(e=>e==i)) {
decryptedFirstPart = decryptedFirstPart.slice(0, decryptedFirstPart.length-i);
break;
}
}
let result = new Uint8Array(decryptedFirstPart.length + secondPart.length);
result.set(decryptedFirstPart);
result.set(secondPart, decryptedFirstPart.length);
resolve(result);
} catch (e) {
reject({e, file})
}
});
}
(async () => {
if (argv.downloadOnly && argv.pdftk) {
console.log("Can't use --download-only and --pdftk at the same time");
return;
}
if ((argv.downloadOnly || argv.pdftk) && !fs.existsSync('temp')) {
fs.mkdirSync('temp');
}
if ((argv.downloadOnly || argv.pdftk) && fs.readdirSync('temp').length > 0) {
console.log("Files already in temp folder, please manually delete them if you want to download a new book");
return;
}
let baseSite = argv.siteUrl;
if (!baseSite) {
let platform = argv.site;
while (!platform) {
platform = prompt('Input site (bsmart or digibook24):');
if (platform != 'bsmart' && platform != 'digibook24') {
platform = null;
console.log('Invalid site');
}
}
baseSite = platform == 'bsmart' ? 'www.bsmart.it' : 'web.digibook24.com';
}
let cookie = argv.cookie;
while (!cookie) {
cookie = prompt('Input "_bsw_session_v1_production" cookie:');
}
let user = await fetch(`https://${baseSite}/api/v5/user`, {headers: {cookie:'_bsw_session_v1_production='+cookie}});
if (user.status != 200) {
console.log("Bad cookie");
return;
}
user = await user.json();
let headers = {"auth_token": user.auth_token};
let books = await fetch(`https://${baseSite}/api/v6/books?page_thumb_size=medium&per_page=25000`, {headers}).then(res => res.json());
let preactivations = await fetch(`https://${baseSite}/api/v5/books/preactivations`, {headers}).then(res => res.json());
preactivations.forEach(preactivation => {
if (preactivation.no_bsmart === false) {
books.push(...preactivation.books);
}
});
if (books.length == 0) {
console.log('No books in your library!');
} else {
console.log("Book list:");
console.table(books.map(book => ({ id: book.id, title: book.title })))
}
let bookId = argv.bookId;
while (!bookId) {
bookId = prompt(`Please input book id${(books.length == 0 ? " manually" : "")}:`);
}
let book = await fetch(`https://${baseSite}/api/v6/books/by_book_id/${bookId}`, {headers});
if (book.status != 200) {
console.log("Invalid book id");
return;
}
book = await book.json();
let info = [];
let page = 1;
while (true) {
//console.log(page);
let tempInfo = await fetch(`https://${baseSite}/api/v5/books/${book.id}/${book.current_edition.revision}/resources?per_page=500&page=${page}`, {headers}).then(res => res.json());
info = info.concat(tempInfo);
if (tempInfo.length < 500) break;
page++;
}
const outputPdf = await PDFDocument.create();
const writeAwaitng = [];
const filenames = [];
const outputname = argv.outputFilename || sanitize(book.id + " - " + book.title);
let assets = info.map(e=>e.assets).flat();
if (argv.resources) {
assets = assets.filter(e=>e.use == "launch_file");
if (!fs.existsSync(outputname)) {
fs.mkdirSync(outputname);
}
console.log("Downloading resources");
} else {
assets = assets.filter(e=>e.use == "page_pdf");
console.log("Downloading pages");
}
for (let i = 0; i<assets.length; i++) {
console.log(`Progress ${(i/assets.length*100).toFixed(2)}% (${i+1}/${assets.length})`);
let asset = assets[i];
if (argv.resources) {
let resourceData = await fetch(asset.url).then(res => res.buffer());
if (asset.encrypted !== false) resourceData = await decryptFile(resourceData).catch((e) => {console.log("Error Downloading resource", e, i, asset)}); // some resources don't say they aren't encrypted but they are
if (argv.checkMd5 && md5(resourceData) != asset.url) console.log("Missmatching md5 hash", i, asset.url)
let filename = path.basename(asset.filename);
writeAwaitng.push(fs.promises.writeFile(`${outputname}/${filename}`, resourceData, (e)=>{}));
} else {
let pageData = await fetch(asset.url).then(res => res.buffer());
pageData = await decryptFile(pageData).catch((e) => {console.log("Error Downloading page", e, i, asset)});
if (argv.checkMd5 && md5(pageData) != asset.url) console.log("Missmatching md5 hash", i, asset.url)
if (argv.downloadOnly || argv.pdftk) {
let filename = path.basename(asset[i].filename, '.pdf');
writeAwaitng.push(fs.promises.writeFile(`temp/${filename}.pdf`, pageData, (e)=>{}));
filenames.push(`temp/${filename}.pdf`);
} else {
const page = await PDFDocument.load(pageData);
const [firstDonorPage] = await outputPdf.copyPages(page, [0]);
outputPdf.addPage(firstDonorPage);
}
}
}
await Promise.all(writeAwaitng);
if (argv.resources) {
// do nothing
} if (!argv.downloadOnly && !argv.pdftk) await fs.promises.writeFile(outputname + ".pdf", await outputPdf.save());
else {
let pdftkCommand = `${argv.pdftkPath} ${filenames.join(' ')} cat output "${outputname}.pdf"`;
console.log("Run this command to merge the pages with pdftk:");
console.log(pdftkCommand);
if (argv.pdftk) {
console.log("Merging pages with pdftk");
let pdftk = spawn(argv.pdftkPath, filenames.concat(['cat', 'output', outputname + ".pdf"]));
pdftk.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
pdftk.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
pdftk.on('close', (code) => {
console.log(`child process exited with code ${code}`);
console.log("Done");
});
}
}
console.log("Done");
})();
/*(async ()=> {
fs.writeFile("test.pdf", await downloadAndDecryptFile("https://s3-eu-west-1.amazonaws.com/dea.bsmart.it/0/9/092662be7c3701b1c596057205fc8a7e"), (e)=>{});
})()*/