-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.js
56 lines (46 loc) · 1.65 KB
/
updater.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const fs = require('fs')
const path = require('path')
const { promisify } = require('util')
const _ = require('lodash')
const fetch = require('node-fetch')
const exportFile = require('./static/export.json')
const exportFilePath = path.resolve(__dirname, 'static', 'export.json')
const exportSlimPath = path.resolve(__dirname, 'static', 'export.slim.json')
const truncateFile = promisify(fs.truncate)
const writeFile = promisify(fs.writeFile)
async function fetchComics () {
const offlineNewestNum = Math.max(1, ...exportFile.map(comic => comic.num))
let onlineNewestNum = await fetch('https://xkcd.com/info.0.json')
.then(res => res.json())
.then(comic => comic.num)
const comicsToFetch = _.range(offlineNewestNum + 1, onlineNewestNum)
const results = await Promise.all(comicsToFetch.map(num => new Promise(resolve =>
fetch(`https://xkcd.com/${num}/info.0.json`)
.then(res => {
if (!res.ok) return resolve(null)
resolve(res.json())
})
.catch(() => {
resolve(null)
})
)))
const data = _.uniqBy(exportFile.concat(results).filter(c => c !== null), 'num')
data.sort((a, b) => a.num - b.num)
const json = JSON.stringify(data)
const slimJson = JSON.stringify(data.map(({ num, img, title, year, day, month }) => ({
id: num,
img,
title,
date: `${year}-${month}-${day}`
})))
try {
await truncateFile(exportFilePath, 0)
await writeFile(exportFilePath, json)
await truncateFile(exportSlimPath, 0)
await writeFile(exportSlimPath, slimJson)
console.log(`Success :) (${results.length} comics exported`)
} catch (e) {
console.log(e)
}
}
fetchComics()