don't retrieve data from internet
This commit is contained in:
parent
904a0978bf
commit
79e5331c3c
1 changed files with 12 additions and 27 deletions
|
@ -1,31 +1,16 @@
|
||||||
const fetch = require('node-fetch');
|
fs = require('fs');
|
||||||
|
|
||||||
// URL of the repo
|
// Retrieve the FAQ file
|
||||||
const repoURL = 'https://git.kennel.ml/api/v1/repos/Anri/Constnium/contents/FAQ.md';
|
const get = () => {
|
||||||
|
return new Promise(function(ok) {
|
||||||
// Store the data from the repo
|
fs.readFile('./FAQ.md', 'utf8', (err, data) => {
|
||||||
let data = null;
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
// Store last time data as been pulled from the repo
|
ok('# Erreur 404\nImpossible de trouver la FAQ.');
|
||||||
let timestamp = 0;
|
}
|
||||||
|
ok(data);
|
||||||
// Retrieve the data from the repo
|
});
|
||||||
async function get() {
|
});
|
||||||
const now = Date.now();
|
|
||||||
// If data is older than one day, refresh it
|
|
||||||
if (Math.ceil(Math.abs(now - timestamp) / (1000 * 3600 * 24)) > 2 || data === null) {
|
|
||||||
await fetch(repoURL)
|
|
||||||
.then(res => res.json().then(json => {
|
|
||||||
// eslint-disable-next-line no-undef
|
|
||||||
data = Buffer.from(json.content, json.encoding).toString('utf8');
|
|
||||||
timestamp = now;
|
|
||||||
}))
|
|
||||||
.catch(() => {
|
|
||||||
data = "# Constnium";
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
Loading…
Reference in a new issue