-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-with-cache.js
49 lines (42 loc) · 1.44 KB
/
fetch-with-cache.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
const fetch = require('node-fetch');
const moment = require('moment');
const md5 = require('md5');
const fs = require('fs');
const childProcess = require('child_process');
function makeCacheFilename(url) {
const key = `${url}${moment().format('YYYY-MM-DD_HH')}`
return `cache/${md5(key)}`;
}
async function getHtml(url) {
const filename = `${makeCacheFilename(url)}.html`;
if (fs.existsSync(filename)) {
// console.debug(`Cache hit ${filename} => ${url}`);
return String(fs.readFileSync(filename));
}
// childProcess.execSync(`curl -L "${url}" -o ${filename}`);
// return String(fs.readFileSync(filename));
const response = await fetch(url);
const data = await response.text();
fs.writeFile(filename, data, err => {
if (err) {
console.error(`Could not write cache file ${filename} for ${url}`);
}
});
return data;
}
async function getJson(url) {
const filename = `${makeCacheFilename(url)}.json`;
if (fs.existsSync(filename)) {
// console.debug(`Cache hit ${filename} => ${url}`);
return JSON.parse(fs.readFileSync(filename));
}
const response = await fetch(url);
const data = await response.json();
fs.writeFile(filename, JSON.stringify(data, null, 2), err => {
if (err) {
console.error(`Could not write cache file ${filename} for ${url}`);
}
});
return data;
}
module.exports = { getHtml, getJson }