From 396de4ea0f2ecf1cb20121cd4bc7bcf2d3dec9f2 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 3 Nov 2019 10:07:09 +0000 Subject: [PATCH] fix: don't download comics we know we can't (#7) License: MIT Signed-off-by: Henrique Dias --- bin/index.js | 13 ++++++++++--- lib/html.js | 5 +---- lib/xkcd.js | 8 +++++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/bin/index.js b/bin/index.js index b7c8d64..a7b8d12 100644 --- a/bin/index.js +++ b/bin/index.js @@ -24,11 +24,16 @@ const argv = yargs .argv async function write ({ data, img }, dir, latest) { + const hasImage = img !== null + try { await fs.outputJSON(join(dir, 'info.json'), data, { spaces: '\t' }) - 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, latest)) + await fs.outputFile(join(dir, 'index.html'), comicPage(data, latest, hasImage)) + + if (hasImage) { + await fs.outputFile(join(dir, basename(data.img)), img) + await fs.outputFile(join(dir, `image${extname(data.img)}`), img) + } } catch (err) { await fs.remove(dir) throw err @@ -114,6 +119,8 @@ async function run () { } added = added.sort((a, b) => a.num - b.num) + await fs.remove(join(argv.dir, 'latest')) + await fs.copy(join(argv.dir, latest.toString()), join(argv.dir, 'latest')) await fs.copyFile(join(__dirname, '../node_modules/tachyons/css/tachyons.min.css'), join(argv.dir, 'tachyons.css')) await fs.copyFile(join(__dirname, '../node_modules/tachyons-columns/css/tachyons-columns.min.css'), join(argv.dir, 'tachyons-columns.css')) await fs.outputFile(join(argv.dir, 'index.html'), homePage(added)) diff --git a/lib/html.js b/lib/html.js index a7d8512..12097af 100644 --- a/lib/html.js +++ b/lib/html.js @@ -9,9 +9,7 @@ const classes = { btn: 'dib navy mh2 pa2 bg-light-blue hover-bg-lightest-blue br2 ba bw1 b--navy no-underline' } -const comicPage = ({ alt, title, transcript, num, img }, latest) => { - - return ` +const comicPage = ({ alt, title, transcript, num, img }, latest) => ` ${num} - ${title} @@ -33,7 +31,6 @@ const comicPage = ({ alt, title, transcript, num, img }, latest) => { ${credits} ` -} const homePage = (list) => ` diff --git a/lib/xkcd.js b/lib/xkcd.js index e34ffb3..ab9a732 100644 --- a/lib/xkcd.js +++ b/lib/xkcd.js @@ -26,7 +26,13 @@ async function getImage (url) { async function getComic (id) { const raw = await fetch(`https://xkcd.com/${id}/info.0.json`) const data = await raw.json() - const img = await getImage(data.img) + let img = null + + // Some comics, such as 1608 and 1663, are composed by interactive + // games and cannot be downloaded as images, so we just ignore them. + if (data.img !== 'https://imgs.xkcd.com/comics/') { + img = await getImage(data.img) + } return { data, img } }