This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·91 lines (70 loc) · 2.21 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
#!/usr/bin/env node
// librairies
import cliProgress from 'cli-progress';
import Epub from 'epub-gen';
// utils
import options from './utils/cli.js';
import uploadEpub from './utils/remarkable.js';
import { getBookSkeleton, getChapter } from './utils/scraper.js';
const progress = new cliProgress.SingleBar({
format: 'Downloading [{bar}] {percentage}% | ETA: {eta}s | {value}/{total}',
hideCursor: true,
}, cliProgress.Presets.shades_classic);
function sleep(seconds) {
return new Promise((resolve) => {
setTimeout(resolve, 1000 * seconds);
});
};
(async () => {
await run(options);
})();
async function run(options) {
const skeleton = await getBookSkeleton(options.url, options.selector);
console.log(`----- ${skeleton.name} ----- ${skeleton.chapters.length} chapters`);
// retrieve urls from skeleton chapter list
const limit = options.limit || skeleton.chapters.length;
const urls = skeleton.chapters
.slice(0, limit)
.map(chapter => new URL(chapter.url));
progress.start(urls.length, 0);
const chapters = [];
if (options.delay === null) {
// asynchronous download
await Promise.all(urls.map(async url => {
chapters.push(await getChapter(url));
progress.increment();
}));
} else {
// synchronous download
for (const url of urls) {
chapters.push(await getChapter(url));
progress.increment();
// wait before downloading next chapter
await sleep(options.delay);
}
}
progress.stop();
console.log(`+ Creating book...`);
const book = {
name: options.name || skeleton.name,
chapters,
};
const filename = `${book.name}.epub`;
const epubOptions = {
title: book.name,
author: options.author,
publisher: 'Unknown',
content: chapters.map(chapter => ({ title: chapter.name, data: chapter.content })),
};
await new Epub(epubOptions, filename).promise;
console.log(`+ Book saved to ${filename}`);
// upload book
if (options.upload) {
try {
await uploadEpub(book.name, filename, options.tokenFile);
console.log('Book uploaded.');
} catch (err) {
console.log('Could not upload book, book name may already exists. Try to specify a custom name with --name option.', err);
}
}
}