Added support for fetching content directly from host. Improved pattern interpolation. Refactored content modules.

This commit is contained in:
2024-09-11 05:16:57 +02:00
parent 20cb522689
commit b9a7e4b83a
26 changed files with 572 additions and 440 deletions
+15 -13
View File
@@ -3,30 +3,32 @@
const config = require('config');
const fetch = require('node-fetch');
function fetchItem(url, attempt, post) {
function retry(error) {
console.log('\x1b[31m%s\x1b[0m', `Failed to fetch '${url}': ${error.message} (${post.permalink})`);
async function fetchItem(url, attempt, post) {
async function retry(error) {
console.log('\x1b[31m%s\x1b[0m', `Failed to fetch '${url}': ${error.message} (${post ? post.permalink : 'no post'})`);
if(attempt < config.fetch.retries) {
if (attempt < config.fetch.retries) {
console.log('Retrying...');
return fetchItem(url, ++attempt, post);
return fetchItem(url, attempt + 1, post);
}
return null;
};
}
return fetch(url).then(res => {
if(!res.ok) {
try {
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Response not OK for '${url}', HTTP code '${res.status}'`);
}
return res;
}).then(res => {
console.log(`Fetched '${url}' (${post.permalink})`);
console.log(`Fetched '${url}' (${post ? post.permalink : 'no post'})`);
return res.body;
}).catch(retry);
};
} catch (error) {
return retry(error);
}
}
module.exports = fetchItem;