12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- /* Magic Mirror
- * Node Helper: Newsfeed
- *
- * By Michael Teeuw https://michaelteeuw.nl
- * MIT Licensed.
- */
-
- const NodeHelper = require("node_helper");
- const NewsfeedFetcher = require("./newsfeedfetcher.js");
- const Log = require("logger");
-
- module.exports = NodeHelper.create({
- // Override start method.
- start: function () {
- Log.log("Starting node helper for: " + this.name);
- this.fetchers = [];
- },
-
- // Override socketNotificationReceived received.
- socketNotificationReceived: function (notification, payload) {
- if (notification === "ADD_FEED") {
- this.createFetcher(payload.feed, payload.config);
- }
- },
-
- /**
- * Creates a fetcher for a new feed if it doesn't exist yet.
- * Otherwise it reuses the existing one.
- *
- * @param {object} feed The feed object
- * @param {object} config The configuration object
- */
- createFetcher: function (feed, config) {
- const url = feed.url || "";
- const encoding = feed.encoding || "UTF-8";
- const reloadInterval = feed.reloadInterval || config.reloadInterval || 5 * 60 * 1000;
-
- try {
- new URL(url);
- } catch (error) {
- Log.error("Newsfeed Error. Malformed newsfeed url: ", url, error);
- this.sendSocketNotification("NEWSFEED_ERROR", { error_type: "MODULE_ERROR_MALFORMED_URL" });
- return;
- }
-
- let fetcher;
- if (typeof this.fetchers[url] === "undefined") {
- Log.log("Create new newsfetcher for url: " + url + " - Interval: " + reloadInterval);
- fetcher = new NewsfeedFetcher(url, reloadInterval, encoding, config.logFeedWarnings);
-
- fetcher.onReceive(() => {
- this.broadcastFeeds();
- });
-
- fetcher.onError((fetcher, error) => {
- Log.error("Newsfeed Error. Could not fetch newsfeed: ", url, error);
- let error_type = NodeHelper.checkFetchError(error);
- this.sendSocketNotification("NEWSFEED_ERROR", {
- error_type
- });
- });
-
- this.fetchers[url] = fetcher;
- } else {
- Log.log("Use existing newsfetcher for url: " + url);
- fetcher = this.fetchers[url];
- fetcher.setReloadInterval(reloadInterval);
- fetcher.broadcastItems();
- }
-
- fetcher.startFetch();
- },
-
- /**
- * Creates an object with all feed items of the different registered feeds,
- * and broadcasts these using sendSocketNotification.
- */
- broadcastFeeds: function () {
- const feeds = {};
- for (let f in this.fetchers) {
- feeds[f] = this.fetchers[f].items();
- }
- this.sendSocketNotification("NEWS_ITEMS", feeds);
- }
- });
|