Added hash comparison to duplicate avoidance.
This commit is contained in:
+10
-4
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user