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