feat: downloading

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
This commit is contained in:
Henrique Dias 2019-04-28 18:59:07 +01:00
parent 953981ef34
commit cd14e6c2f6
2 changed files with 6 additions and 6 deletions

View File

@ -2,7 +2,7 @@
const fs = require('fs-extra')
const yargs = require('yargs')
const { basename, join } = require('path')
const { basename, extname, join } = require('path')
const { getLatestId, getComic } = require('../lib/xkcd')
const { homePage, comicPage } = require('../lib/html')
const { pad, progress } = require('../lib/helpers')
@ -26,8 +26,8 @@ const argv = yargs
async function write ({ data, img }, dir) {
try {
await fs.outputJSON(join(dir, 'info.json'), data, { spaces: '\t' })
const dest = fs.createWriteStream(join(dir, basename(data.img)))
img.body.pipe(dest)
await fs.outputFile(join(dir, basename(data.img)), img)
await fs.outputFile(join(dir, `image.${extname(data.img)}`), img)
await fs.outputFile(join(dir, 'index.html'), comicPage(data))
} catch (err) {
await fs.remove(dir)
@ -68,15 +68,15 @@ async function run () {
let comic = null
const info = {
let info = {
id: i,
title: comic.data.title,
dir: dir,
num: num
}
try {
comic = await getComic(i)
info.title = comic.data.title
await write(comic, dir)
added.push(info)
} catch (err) {

View File

@ -9,7 +9,7 @@ async function getLatestId () {
async function getComic (id) {
const raw = await fetch(`https://xkcd.com/${id}/info.0.json`)
const data = await raw.json()
const img = await fetch(data.img)
const img = await (await fetch(data.img)).buffer()
return { data, img }
}