JDRssDownloader/FeedUpdater.js

29 lines
818 B
JavaScript
Raw Normal View History

2022-06-08 12:28:30 +01:00
const fs = require("fs");
const Parser = require("rss-parser");
2022-06-08 22:10:37 +01:00
const lodash = require('lodash');
2022-06-08 12:28:30 +01:00
2022-06-09 10:29:05 +01:00
async function feedUpdater() {
2022-06-09 10:06:22 +01:00
// Make a new RSS Parser
const parser = new Parser();
2022-06-08 12:28:30 +01:00
2022-06-09 10:06:22 +01:00
// Get all the items in the RSS feed
const feed = await parser.parseURL(JSON.parse(fs.readFileSync('config.json')).RSSFeed);
2022-06-08 12:28:30 +01:00
2022-06-09 10:06:22 +01:00
let items = [];
2022-06-08 12:28:30 +01:00
2022-06-11 18:46:24 +01:00
if (fs.existsSync('./feedCache.json')) {
items = JSON.parse(fs.readFileSync('./feedCache.json'))
2022-06-09 10:06:22 +01:00
}
// Compare existing cache and new items and merge differences
let updatedArray = lodash.unionBy(feed.items, items, 'title');
2022-06-08 22:10:37 +01:00
2022-06-09 10:06:22 +01:00
// Save the file
log.info(updatedArray.length + ' items in file cache')
2022-06-11 18:46:24 +01:00
fs.writeFileSync('./feedCache.json', JSON.stringify(updatedArray));
2022-06-09 10:06:22 +01:00
2022-06-09 20:23:36 +01:00
2022-06-09 10:29:05 +01:00
}
module.exports = {
feedUpdater
}