mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-07-01 05:23:25 +00:00
Use es6 notation in newsfeed module
This commit is contained in:
parent
e4f671c898
commit
a70bb517e1
@ -147,8 +147,7 @@ Module.register("newsfeed", {
|
|||||||
* Registers the feeds to be used by the backend.
|
* Registers the feeds to be used by the backend.
|
||||||
*/
|
*/
|
||||||
registerFeeds: function () {
|
registerFeeds: function () {
|
||||||
for (var f in this.config.feeds) {
|
for (let feed of this.config.feeds) {
|
||||||
var feed = this.config.feeds[f];
|
|
||||||
this.sendSocketNotification("ADD_FEED", {
|
this.sendSocketNotification("ADD_FEED", {
|
||||||
feed: feed,
|
feed: feed,
|
||||||
config: this.config
|
config: this.config
|
||||||
@ -162,12 +161,11 @@ Module.register("newsfeed", {
|
|||||||
* @param {object} feeds An object with feeds returned by the node helper.
|
* @param {object} feeds An object with feeds returned by the node helper.
|
||||||
*/
|
*/
|
||||||
generateFeed: function (feeds) {
|
generateFeed: function (feeds) {
|
||||||
var newsItems = [];
|
let newsItems = [];
|
||||||
for (var feed in feeds) {
|
for (let feed in feeds) {
|
||||||
var feedItems = feeds[feed];
|
const feedItems = feeds[feed];
|
||||||
if (this.subscribedToFeed(feed)) {
|
if (this.subscribedToFeed(feed)) {
|
||||||
for (var i in feedItems) {
|
for (let item of feedItems) {
|
||||||
var item = feedItems[i];
|
|
||||||
item.sourceTitle = this.titleForFeed(feed);
|
item.sourceTitle = this.titleForFeed(feed);
|
||||||
if (!(this.config.ignoreOldItems && Date.now() - new Date(item.pubdate) > this.config.ignoreOlderThan)) {
|
if (!(this.config.ignoreOldItems && Date.now() - new Date(item.pubdate) > this.config.ignoreOlderThan)) {
|
||||||
newsItems.push(item);
|
newsItems.push(item);
|
||||||
@ -176,8 +174,8 @@ Module.register("newsfeed", {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
newsItems.sort(function (a, b) {
|
newsItems.sort(function (a, b) {
|
||||||
var dateA = new Date(a.pubdate);
|
const dateA = new Date(a.pubdate);
|
||||||
var dateB = new Date(b.pubdate);
|
const dateB = new Date(b.pubdate);
|
||||||
return dateB - dateA;
|
return dateB - dateA;
|
||||||
});
|
});
|
||||||
if (this.config.maxNewsItems > 0) {
|
if (this.config.maxNewsItems > 0) {
|
||||||
@ -186,8 +184,8 @@ Module.register("newsfeed", {
|
|||||||
|
|
||||||
if (this.config.prohibitedWords.length > 0) {
|
if (this.config.prohibitedWords.length > 0) {
|
||||||
newsItems = newsItems.filter(function (value) {
|
newsItems = newsItems.filter(function (value) {
|
||||||
for (var i = 0; i < this.config.prohibitedWords.length; i++) {
|
for (let word of this.config.prohibitedWords) {
|
||||||
if (value["title"].toLowerCase().indexOf(this.config.prohibitedWords[i].toLowerCase()) > -1) {
|
if (value["title"].toLowerCase().indexOf(word.toLowerCase()) > -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -235,7 +233,7 @@ Module.register("newsfeed", {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// get updated news items and broadcast them
|
// get updated news items and broadcast them
|
||||||
var updatedItems = [];
|
const updatedItems = [];
|
||||||
newsItems.forEach((value) => {
|
newsItems.forEach((value) => {
|
||||||
if (this.newsItems.findIndex((value1) => value1 === value) === -1) {
|
if (this.newsItems.findIndex((value1) => value1 === value) === -1) {
|
||||||
// Add item to updated items list
|
// Add item to updated items list
|
||||||
@ -258,8 +256,7 @@ Module.register("newsfeed", {
|
|||||||
* @returns {boolean} True if it is subscribed, false otherwise
|
* @returns {boolean} True if it is subscribed, false otherwise
|
||||||
*/
|
*/
|
||||||
subscribedToFeed: function (feedUrl) {
|
subscribedToFeed: function (feedUrl) {
|
||||||
for (var f in this.config.feeds) {
|
for (let feed of this.config.feeds) {
|
||||||
var feed = this.config.feeds[f];
|
|
||||||
if (feed.url === feedUrl) {
|
if (feed.url === feedUrl) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -274,8 +271,7 @@ Module.register("newsfeed", {
|
|||||||
* @returns {string} The title of the feed
|
* @returns {string} The title of the feed
|
||||||
*/
|
*/
|
||||||
titleForFeed: function (feedUrl) {
|
titleForFeed: function (feedUrl) {
|
||||||
for (var f in this.config.feeds) {
|
for (let feed of this.config.feeds) {
|
||||||
var feed = this.config.feeds[f];
|
|
||||||
if (feed.url === feedUrl) {
|
if (feed.url === feedUrl) {
|
||||||
return feed.title || "";
|
return feed.title || "";
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,6 @@ const iconv = require("iconv-lite");
|
|||||||
* @class
|
* @class
|
||||||
*/
|
*/
|
||||||
const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings) {
|
const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings) {
|
||||||
const self = this;
|
|
||||||
|
|
||||||
let reloadTimer = null;
|
let reloadTimer = null;
|
||||||
let items = [];
|
let items = [];
|
||||||
|
|
||||||
@ -36,14 +34,14 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
|
|||||||
/**
|
/**
|
||||||
* Request the new items.
|
* Request the new items.
|
||||||
*/
|
*/
|
||||||
const fetchNews = function () {
|
const fetchNews = () => {
|
||||||
clearTimeout(reloadTimer);
|
clearTimeout(reloadTimer);
|
||||||
reloadTimer = null;
|
reloadTimer = null;
|
||||||
items = [];
|
items = [];
|
||||||
|
|
||||||
const parser = new FeedMe();
|
const parser = new FeedMe();
|
||||||
|
|
||||||
parser.on("item", function (item) {
|
parser.on("item", (item) => {
|
||||||
const title = item.title;
|
const title = item.title;
|
||||||
let description = item.description || item.summary || item.content || "";
|
let description = item.description || item.summary || item.content || "";
|
||||||
const pubdate = item.pubdate || item.published || item.updated || item["dc:date"];
|
const pubdate = item.pubdate || item.published || item.updated || item["dc:date"];
|
||||||
@ -68,13 +66,13 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
parser.on("end", function () {
|
parser.on("end", () => {
|
||||||
self.broadcastItems();
|
this.broadcastItems();
|
||||||
scheduleTimer();
|
scheduleTimer();
|
||||||
});
|
});
|
||||||
|
|
||||||
parser.on("error", function (error) {
|
parser.on("error", (error) => {
|
||||||
fetchFailedCallback(self, error);
|
fetchFailedCallback(this, error);
|
||||||
scheduleTimer();
|
scheduleTimer();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -87,7 +85,7 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
|
|||||||
|
|
||||||
fetch(url, { headers: headers })
|
fetch(url, { headers: headers })
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
fetchFailedCallback(self, error);
|
fetchFailedCallback(this, error);
|
||||||
scheduleTimer();
|
scheduleTimer();
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
@ -134,7 +132,7 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Log.info("Newsfeed-Fetcher: Broadcasting " + items.length + " items.");
|
Log.info("Newsfeed-Fetcher: Broadcasting " + items.length + " items.");
|
||||||
itemsReceivedCallback(self);
|
itemsReceivedCallback(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.onReceive = function (callback) {
|
this.onReceive = function (callback) {
|
||||||
|
@ -74,8 +74,8 @@ module.exports = NodeHelper.create({
|
|||||||
* and broadcasts these using sendSocketNotification.
|
* and broadcasts these using sendSocketNotification.
|
||||||
*/
|
*/
|
||||||
broadcastFeeds: function () {
|
broadcastFeeds: function () {
|
||||||
var feeds = {};
|
const feeds = {};
|
||||||
for (var f in this.fetchers) {
|
for (let f in this.fetchers) {
|
||||||
feeds[f] = this.fetchers[f].items();
|
feeds[f] = this.fetchers[f].items();
|
||||||
}
|
}
|
||||||
this.sendSocketNotification("NEWS_ITEMS", feeds);
|
this.sendSocketNotification("NEWS_ITEMS", feeds);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user