From 3266a1550e9cd20f18545b2c7bd613e9b716b51c Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 25 Feb 2023 20:56:01 -0500 Subject: [PATCH] Working RSS Feed Manager - Auto updates feeds - Sends updates to specified channels - Needs threading --- controllers/rssController.js | 26 ++++++++++++++++++++++++++ index.js | 6 ++++-- libCore.js | 23 ++++++++++++----------- libStorage.js | 4 ++-- 4 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 controllers/rssController.js diff --git a/controllers/rssController.js b/controllers/rssController.js new file mode 100644 index 0000000..901a72a --- /dev/null +++ b/controllers/rssController.js @@ -0,0 +1,26 @@ +//Will handle updating feeds in all channels + +const { DebugBuilder } = require("../utilities/debugBuilder"); +const log = new DebugBuilder("server", "rssController"); + +const libCore = require("../libCore"); +const libUtils = require("../libUtils"); + +exports.RSSController = class RSSController { + constructor(_client) { + this.client = _client; + } + + async start(){ + log.INFO("Starting RSS Controller"); + while(true){ + await new Promise(resolve => setTimeout(resolve, 10000)); + this.collectLatestPosts(); + } + } + + async collectLatestPosts(){ + log.INFO("Updating sources"); + libCore.updateFeeds(this.client) + } +} \ No newline at end of file diff --git a/index.js b/index.js index a8304e6..78a11bc 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ var http = require('http'); const fs = require('fs'); require('dotenv').config(); const libCore = require("./libCore"); +const { RSSController } = require("./controllers/rssController"); const libUtils = require("./libUtils"); const deployCommands = require("./utilities/deployCommands"); @@ -123,8 +124,9 @@ client.on('ready', () => { log.DEBUG(`Starting HTTP Server`); runHTTPServer(); - log.DEBUG("Loading new posts"); - libCore.updateFeeds(client); + log.DEBUG("Loading new posts"); + const rssManager = new RSSController(client); + rssManager.start(); }); // Setup any additional event handlers diff --git a/libCore.js b/libCore.js index a47f3f1..8840c4d 100644 --- a/libCore.js +++ b/libCore.js @@ -2,7 +2,7 @@ let Parser = require('rss-parser'); const axios = require('axios'); let parser = new Parser(); -const storageHandler = require("./libStorage"); +const { FeedStorage, PostStorage } = require("./libStorage"); const libUtils = require("./libUtils"); const { createHash } = require("crypto"); @@ -19,15 +19,9 @@ const configuration = new Configuration({ const openai = new OpenAIApi(configuration); */ - -// Data Structures -var feeds = {}; -var rssFeedMap = []; -var rssFeedCategories = []; - // Setup Storage handlers -var feedStorage = new storageHandler.feedStorage(); -var postStorage = new storageHandler.postStorage(); +var feedStorage = new FeedStorage(); +var postStorage = new PostStorage(); /** * Adds or updates new source url to configured storage @@ -291,7 +285,10 @@ exports.getChat = async function (_prompt, { _model = "text-davinci-003", _tempe * @constructor */ exports.getSources = function () { - return feeds; + feedStorage.getAllRecords((err, records) => { + if (err) throw err; + return records; + }) } /** @@ -320,7 +317,11 @@ exports.getQuotes = async function (quote_url) { * @constructor */ exports.getCategories = function () { - return rssFeedCategories; + feedStorage.getUniqueByKey("category", (err, results) => { + if (err) throw err; + + return results + }); } exports.addPost = (postObject, callback) => { diff --git a/libStorage.js b/libStorage.js index 8c6d42a..e8f447e 100644 --- a/libStorage.js +++ b/libStorage.js @@ -142,7 +142,7 @@ class Storage { } } -exports.feedStorage = class feedStorage extends Storage { +exports.FeedStorage = class FeedStorage extends Storage { constructor() { super(rssFeedsTable); } @@ -323,7 +323,7 @@ exports.feedStorage = class feedStorage extends Storage { } } -exports.postStorage = class postStorage extends Storage { +exports.PostStorage = class PostStorage extends Storage { constructor() { super(rssPostsTable); }