mirror of
https://github.com/georgemandis/bubo-rss.git
synced 2024-11-12 23:14:22 +00:00
Updated Bubo to v1.0.1
This commit is contained in:
parent
1cd365e257
commit
a5ff96e449
60
src/index.js
60
src/index.js
@ -1,9 +1,13 @@
|
|||||||
/**
|
/*
|
||||||
* 🦉 Bubo RSS Reader
|
* 🦉 Bubo RSS Reader
|
||||||
* ====
|
* ====
|
||||||
* Dead, dead simple feed reader that renders an HTML
|
* Dead simple feed reader that renders an HTML
|
||||||
* page with links to content from feeds organized by site
|
* page with links to content from feeds organized by site
|
||||||
*
|
*
|
||||||
|
* Code: https://github.com/georgemandis/bubo-rss
|
||||||
|
* Copyright (c) 2019 George Mandis (https://george.mand.is)
|
||||||
|
* Version: 1.0.1 (11/14/2021)
|
||||||
|
* Licensed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const fetch = require("node-fetch");
|
const fetch = require("node-fetch");
|
||||||
@ -15,14 +19,17 @@ const env = nunjucks.configure({ autoescape: true });
|
|||||||
|
|
||||||
const feeds = require("./feeds.json");
|
const feeds = require("./feeds.json");
|
||||||
|
|
||||||
env.addFilter("formatDate", function(dateString) {
|
/**
|
||||||
|
* Global filters for my Nunjucks templates
|
||||||
|
*/
|
||||||
|
env.addFilter("formatDate", function (dateString) {
|
||||||
const formattedDate = new Date(dateString).toLocaleDateString()
|
const formattedDate = new Date(dateString).toLocaleDateString()
|
||||||
return formattedDate !== 'Invalid Date' ? formattedDate : dateString;
|
return formattedDate !== 'Invalid Date' ? formattedDate : dateString;
|
||||||
});
|
});
|
||||||
|
|
||||||
env.addGlobal('now', (new Date()).toUTCString() );
|
env.addGlobal('now', (new Date()).toUTCString());
|
||||||
|
|
||||||
// parse XML or JSON feeds
|
// parse RSS/XML or JSON feeds
|
||||||
function parseFeed(response) {
|
function parseFeed(response) {
|
||||||
const contentType = response.headers.get("content-type")
|
const contentType = response.headers.get("content-type")
|
||||||
? response.headers.get("content-type").split(";")[0]
|
? response.headers.get("content-type").split(";")[0]
|
||||||
@ -44,13 +51,44 @@ function parseFeed(response) {
|
|||||||
|
|
||||||
const jsonFeed = [contentType]
|
const jsonFeed = [contentType]
|
||||||
.map(item =>
|
.map(item =>
|
||||||
["application/json"].includes(item) ? response.json() : false
|
["application/json", "application/feed+json"].includes(item) ? response.json() : false
|
||||||
)
|
)
|
||||||
.filter(_ => _)[0];
|
.filter(_ => _)[0];
|
||||||
|
|
||||||
return rssFeed || jsonFeed || false;
|
return rssFeed || jsonFeed || false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
There's a little inconcistency with how feeds report certain things like
|
||||||
|
title, links and timestamps. These helpers try to normalize that bit and
|
||||||
|
provide an order-of-operations list of properties to look for.
|
||||||
|
|
||||||
|
Note: these are tightly-coupled to the template and a personal preference.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const getLink = (obj) => {
|
||||||
|
const link_values = ["link", "url", "guid", "home_page_url"];
|
||||||
|
const keys = Object.keys(obj);
|
||||||
|
const link_property = link_values.find(link_value => keys.includes(link_value));
|
||||||
|
return obj[link_property];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// fallback to URL for the title if not present (coupled to my template)
|
||||||
|
const getTitle = (obj) => {
|
||||||
|
const title_values = ["title", "url", "link"]; // fallback to url/link as title if omitted
|
||||||
|
const keys = Object.keys(obj);
|
||||||
|
const title_property = title_values.find(title_value => keys.includes(title_value));
|
||||||
|
return obj[title_property];
|
||||||
|
}
|
||||||
|
|
||||||
|
// More dependable way to get timestamps
|
||||||
|
const getTimestamp = (obj) => {
|
||||||
|
const timestamp = new Date(obj.pubDate || obj.isoDate || obj.date || obj.date_published).getTime();
|
||||||
|
return isNaN(timestamp) ? (obj.pubDate || obj.isoDate || obj.date || obj.date_published) : timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch the feeds and build the object for our template
|
||||||
(async () => {
|
(async () => {
|
||||||
const contentFromAllFeeds = {};
|
const contentFromAllFeeds = {};
|
||||||
const errors = [];
|
const errors = [];
|
||||||
@ -66,13 +104,15 @@ function parseFeed(response) {
|
|||||||
typeof body === "string" ? await parser.parseString(body) : body;
|
typeof body === "string" ? await parser.parseString(body) : body;
|
||||||
|
|
||||||
contents.feed = feeds[group][index];
|
contents.feed = feeds[group][index];
|
||||||
contents.title = contents.title ? contents.title : contents.link;
|
contents.title = getTitle(contents);
|
||||||
|
contents.link = getLink(contents);
|
||||||
contentFromAllFeeds[group].push(contents);
|
contentFromAllFeeds[group].push(contents);
|
||||||
|
|
||||||
// try to normalize date attribute naming
|
// try to normalize date attribute naming
|
||||||
contents.items.forEach(item => {
|
contents?.items?.forEach(item => {
|
||||||
const timestamp = new Date(item.pubDate || item.isoDate || item.date).getTime();
|
item.timestamp = getTimestamp(item);
|
||||||
item.timestamp = isNaN(timestamp) ? (item.pubDate || item.isoDate || item.date) : timestamp;
|
item.title = getTitle(item);
|
||||||
|
item.link = getLink(item);
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user