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")
8 const url = "http://images.mangafreak.net/downloads/Horimiya_"
11 const bar = new cliProgress.SingleBar({}, cliProgress.Presets.legacy)
12 bar.start(chapters, 0, { speed: "N/A" })
14 const doc = new pdfkit({autoFirstPage: false})
15 doc.pipe(fs.createWriteStream("Horimiya.pdf"))
17 fs.promises.rm("pages", {recursive: true, force: true})
18 .then(_ => fs.promises.mkdir("pages"))
19 .then(_ => Promise.all(new Array(chapters)
22 .map(n => download(url + n)
23 .pipe(unzipper.Extract({ path: "pages" }))
25 .then(_ => bar.increment())
28 .then(_ => bar.stop())
29 .then(_ => fs.promises.readdir("pages"))
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))