Added include and exclude source arguments. Improved stream fetch failure handling and improved logging details.

This commit is contained in:
2024-09-11 05:16:55 +02:00
parent 0a2fac5f1f
commit e01979a757
8 changed files with 45 additions and 15 deletions
+13 -6
View File
@@ -1,22 +1,29 @@
'use strict';
const config = require('config');
const fetch = require('node-fetch');
function fetchItem(url, attempt) {
function fetchItem(url, attempt, post) {
function retry(error) {
console.log(error);
console.log('\x1b[31m%s\x1b[0m', `Failed to fetch '${url}': ${error.message} (${post.permalink})`);
if(attempt < 3) {
if(attempt < config.fetch.retries) {
console.log('Retrying...');
return fetchItem(url, ++attempt);
return fetchItem(url, ++attempt, post);
}
return null;
};
return fetch(url).then(res => {
return res.ok ? res : Promise.reject(`Failed to 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}'`);
console.log(`Fetched '${url}' (${post.permalink})`);
return res.body;
}).catch(retry);