Added hash comparison to duplicate avoidance.

This commit is contained in:
2024-09-11 05:16:58 +02:00
parent 7c6c99c9d1
commit 45123bc630
7 changed files with 305 additions and 35 deletions
+10 -4
View File
@@ -1,7 +1,8 @@
'use strict';
const config = require('config');
const fetch = require('node-fetch');
const bhttp = require('bhttp');
const blake2 = require('blake2');
async function fetchItem(url, attempt, post) {
async function retry(error) {
@@ -17,16 +18,21 @@ async function fetchItem(url, attempt, post) {
}
try {
const res = await fetch(url);
const res = await bhttp.get(url);
if (!res.ok) {
if (!res.statusCode === 200) {
throw new Error(`Response not OK for '${url}', HTTP code '${res.status}'`);
}
console.log(`Fetched '${url}' (${post ? post.permalink : 'no post'})`);
return res.body;
const hash = blake2.createHash('blake2b', { digestLength: 24 });
hash.update(res.body);
const contentHash = hash.digest('hex');
return Object.assign(res.body, { hash: contentHash });
} catch (error) {
console.log(error);
return retry(error);
}
}