From cc5498e0bbddb1c0f7e31af01ed36a2ea522024c Mon Sep 17 00:00:00 2001 From: Kristof Zerbe Date: Mon, 12 Aug 2024 14:33:05 +0200 Subject: [PATCH] Feed Fix --- .../generators/generator-dynamic-blogroll.js | 40 +++++++++++-------- themes/landscape/layout/blogroll.ejs | 2 + 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/scripts/generators/generator-dynamic-blogroll.js b/scripts/generators/generator-dynamic-blogroll.js index beafb63936..dfdf6e9068 100644 --- a/scripts/generators/generator-dynamic-blogroll.js +++ b/scripts/generators/generator-dynamic-blogroll.js @@ -40,25 +40,31 @@ hexo.extend.generator.register("dynamic-blogroll", async function(locals) { item.feedSize = `${feedSize.toFixed(2)} KB` } // console.log(item.title + ": " + item.feedSize); + // console.log(response.status); - feed2json.fromString(response.data, item.feed, { log: false }, (error, json) => { + if (response.status === 200) { + feed2json.fromString(response.data, item.feed, (error, json) => { - if (!error) { - // json.items.sort((a,b) => a.date_published - b.date_published).reverse(); - json.items.sort((a,b) => _helpers.moment(a.date_published).diff(b.date_published)).reverse(); - - item.feedLength = json.items.length; - - let feedItem = json.items[0]; - item.latest_post = { - "url": feedItem.url, - "title": feedItem.title || "- no title -", - "date_published": feedItem.date_published - }; - } else { - log.error("Parsing feed from " + item.title + " failed"); - } - }); + if (!error) { + // json.items.sort((a,b) => a.date_published - b.date_published).reverse(); + json.items.sort((a,b) => _helpers.moment(a.date_published).diff(b.date_published)).reverse(); + + item.feedLength = json.items.length; + + let feedItem = json.items[0]; + item.latest_post = { + "url": feedItem.url, + "title": feedItem.title || "- no title -", + "date_published": feedItem.date_published + }; + } else { + log.error("Parsing feed from " + item.title + " failed"); + } + }); + } else { + log.error("Fetching feed from " + item.title + " responded with status " + response.status); + item.errorStatus = response.status; + } resolve(); }).catch(err => { log.error("Fetching feed from " + item.title + " failed"); diff --git a/themes/landscape/layout/blogroll.ejs b/themes/landscape/layout/blogroll.ejs index e39b62f977..e85d8f4b01 100644 --- a/themes/landscape/layout/blogroll.ejs +++ b/themes/landscape/layout/blogroll.ejs @@ -40,6 +40,8 @@ <%= new Date(item.latest_post.date_published).toLocaleString('en-GB', { year:"numeric", month:"long", day:"numeric"}) ?? "---" %>: <%= item.latest_post.title %> + <% } else if (item.errorStatus) { %> + Error <%= item.errorStatus %> while fetching the feed. Tryin' again tomorrow... <% } else { %> Failed to read the feed. Tryin' again tomorrow... <% } %>