]> git.lizzy.rs Git - horimiya-dl.git/blob - init.js
f1428cdc4c6bc906650d0865c7c200632a2b5b68
[horimiya-dl.git] / init.js
1 // GPLv3 License
2 const fs = require("fs")
3 const download = require("download")
4 const cliProgress = require("cli-progress")
5 const unzipper = require("unzipper")
6 const pdfkit = require("pdfkit")
7
8 const url = "http://images.mangafreak.net/downloads/Horimiya_"
9 const chapters = 124
10
11 const bar = new cliProgress.SingleBar({}, cliProgress.Presets.legacy)
12 bar.start(chapters, 0, { speed: "N/A" })
13
14 const doc = new pdfkit({autoFirstPage: false})
15 doc.pipe(fs.createWriteStream("Horimiya.pdf"))
16
17 fs.promises.rm("pages", {recursive: true, force: true})
18         .then(_ => fs.promises.mkdir("pages"))
19         .then(_ => Promise.all(new Array(chapters)
20                 .fill(null)
21                 .map((_, i) => i + 1)
22                 .map(n => download(url + n)
23                         .pipe(unzipper.Extract({ path: "pages" }))
24                         .promise()
25                         .then(_ => bar.increment())     
26                 )
27         ))
28         .then(_ => bar.stop())
29         .then(_ => fs.promises.readdir("pages"))
30         .then(files => files
31                 .sort(new Intl.Collator(undefined, { numeric: true }).compare)
32                 .map(file => "pages/" + file)
33                 .map(file => doc.openImage(file))
34                 .forEach(img => doc.addPage({size: [img.width, img.height]}).image(img, 0, 0))
35         )
36         .then(_ => doc.end())