-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetch.js
32 lines (26 loc) · 959 Bytes
/
fetch.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
const fs = require("fs");
const axios = require("axios");
const { loggg } = require("./tools");
async function getContent(website, options) {
const cachePathAndFilename = options.cachePathAndFilename;
// handle missing cache options
if (!options || !cachePathAndFilename) {
loggg(`Missing cache options`);
return (await fetchWebsiteContent(website)).data;
}
// get from cache if cached
if (fs.existsSync(cachePathAndFilename)) {
loggg(`Returning content from cache: ${cachePathAndFilename}`);
return fs.readFileSync(cachePathAndFilename).toString();
}
// download and write to cache
const html = (await fetchWebsiteContent(website)).data;
loggg(`Writing content to cache: ${cachePathAndFilename}`);
fs.writeFileSync(cachePathAndFilename, html);
return html;
}
async function fetchWebsiteContent(website) {
loggg("Fetching content from website!");
return axios.get(website);
}
module.exports = { getContent };