feat: download high resolution imgs ()

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
This commit is contained in:
Henrique Dias 2019-11-03 09:10:04 +00:00 committed by GitHub
parent 3688c623f3
commit d271737f07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
const fetch = require('node-fetch')
const path = require('path')
async function getLatestId () {
const raw = await fetch(`https://xkcd.com/info.0.json`)
@ -6,10 +7,26 @@ async function getLatestId () {
return data.num
}
async function getImage (url) {
const ext = path.extname(url)
const url2x = `${path.dirname(url)}/${path.basename(url, ext)}_2x${ext}`
let res = await fetch(url2x)
if (!res.ok) {
res = await fetch(url)
}
if (!res.ok) {
throw new Error('bad image request')
}
return res.buffer()
}
async function getComic (id) {
const raw = await fetch(`https://xkcd.com/${id}/info.0.json`)
const data = await raw.json()
const img = await (await fetch(data.img)).buffer()
const img = await getImage(data.img)
return { data, img }
}