Run prettier over ALL files once

No other changes done in this commit
This commit is contained in:
Veeck
2020-05-11 22:22:32 +02:00
parent 3a5a29efc0
commit abb5dc5739
160 changed files with 2369 additions and 2210 deletions

View File

@@ -1,5 +1,6 @@
# Module: News Feed
The `newsfeed ` module is one of the default modules of the MagicMirror.
This module displays news headlines based on an RSS feed. Scrolling through news headlines happens time-based (````updateInterval````), but can also be controlled by sending news feed specific notifications to the module.
The `newsfeed` module is one of the default modules of the MagicMirror.
This module displays news headlines based on an RSS feed. Scrolling through news headlines happens time-based (`updateInterval`), but can also be controlled by sending news feed specific notifications to the module.
For configuration options, please check the [MagicMirror² documentation](https://docs.magicmirror.builders/modules/newsfeed.html).

View File

@@ -17,7 +17,7 @@ var iconv = require("iconv-lite");
* attribute logFeedWarnings boolean - Log warnings when there is an error parsing a news article.
*/
var Fetcher = function(url, reloadInterval, encoding, logFeedWarnings) {
var Fetcher = function (url, reloadInterval, encoding, logFeedWarnings) {
var self = this;
if (reloadInterval < 1000) {
reloadInterval = 1000;
@@ -26,8 +26,8 @@ var Fetcher = function(url, reloadInterval, encoding, logFeedWarnings) {
var reloadTimer = null;
var items = [];
var fetchFailedCallback = function() {};
var itemsReceivedCallback = function() {};
var fetchFailedCallback = function () {};
var itemsReceivedCallback = function () {};
/* private methods */
@@ -35,32 +35,29 @@ var Fetcher = function(url, reloadInterval, encoding, logFeedWarnings) {
* Request the new items.
*/
var fetchNews = function() {
var fetchNews = function () {
clearTimeout(reloadTimer);
reloadTimer = null;
items = [];
var parser = new FeedMe();
parser.on("item", function(item) {
parser.on("item", function (item) {
var title = item.title;
var description = item.description || item.summary || item.content || "";
var pubdate = item.pubdate || item.published || item.updated || item["dc:date"];
var url = item.url || item.link || "";
if (title && pubdate) {
var regex = /(<([^>]+)>)/ig;
var regex = /(<([^>]+)>)/gi;
description = description.toString().replace(regex, "");
items.push({
title: title,
description: description,
pubdate: pubdate,
url: url,
url: url
});
} else if (logFeedWarnings) {
console.log("Can't parse feed item:");
console.log(item);
@@ -70,39 +67,37 @@ var Fetcher = function(url, reloadInterval, encoding, logFeedWarnings) {
}
});
parser.on("end", function() {
parser.on("end", function () {
//console.log("end parsing - " + url);
self.broadcastItems();
scheduleTimer();
});
parser.on("error", function(error) {
parser.on("error", function (error) {
fetchFailedCallback(self, error);
scheduleTimer();
});
var nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
var headers = {"User-Agent": "Mozilla/5.0 (Node.js "+ nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)",
"Cache-Control": "max-age=0, no-cache, no-store, must-revalidate",
"Pragma": "no-cache"};
var headers = { "User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)", "Cache-Control": "max-age=0, no-cache, no-store, must-revalidate", Pragma: "no-cache" };
request({uri: url, encoding: null, headers: headers})
.on("error", function(error) {
request({ uri: url, encoding: null, headers: headers })
.on("error", function (error) {
fetchFailedCallback(self, error);
scheduleTimer();
})
.pipe(iconv.decodeStream(encoding)).pipe(parser);
.pipe(iconv.decodeStream(encoding))
.pipe(parser);
};
/* scheduleTimer()
* Schedule the timer for the next update.
*/
var scheduleTimer = function() {
var scheduleTimer = function () {
//console.log('Schedule update timer.');
clearTimeout(reloadTimer);
reloadTimer = setTimeout(function() {
reloadTimer = setTimeout(function () {
fetchNews();
}, reloadInterval);
};
@@ -114,7 +109,7 @@ var Fetcher = function(url, reloadInterval, encoding, logFeedWarnings) {
*
* attribute interval number - Interval for the update in milliseconds.
*/
this.setReloadInterval = function(interval) {
this.setReloadInterval = function (interval) {
if (interval > 1000 && interval < reloadInterval) {
reloadInterval = interval;
}
@@ -123,14 +118,14 @@ var Fetcher = function(url, reloadInterval, encoding, logFeedWarnings) {
/* startFetch()
* Initiate fetchNews();
*/
this.startFetch = function() {
this.startFetch = function () {
fetchNews();
};
/* broadcastItems()
* Broadcast the existing items.
*/
this.broadcastItems = function() {
this.broadcastItems = function () {
if (items.length <= 0) {
//console.log('No items to broadcast yet.');
return;
@@ -139,19 +134,19 @@ var Fetcher = function(url, reloadInterval, encoding, logFeedWarnings) {
itemsReceivedCallback(self);
};
this.onReceive = function(callback) {
this.onReceive = function (callback) {
itemsReceivedCallback = callback;
};
this.onError = function(callback) {
this.onError = function (callback) {
fetchFailedCallback = callback;
};
this.url = function() {
this.url = function () {
return url;
};
this.items = function() {
this.items = function () {
return items;
};
};

View File

@@ -4,8 +4,7 @@
* By Michael Teeuw https://michaelteeuw.nl
* MIT Licensed.
*/
Module.register("newsfeed",{
Module.register("newsfeed", {
// Default module config.
defaults: {
feeds: [
@@ -41,12 +40,12 @@ Module.register("newsfeed",{
},
// Define required scripts.
getScripts: function() {
getScripts: function () {
return ["moment.js"];
},
// Define required translations.
getTranslations: function() {
getTranslations: function () {
// The translations for the default modules are defined in the core translation files.
// Therefor we can just return false. Otherwise we should have returned a dictionary.
// If you're trying to build your own module including translations, check out the documentation.
@@ -54,7 +53,7 @@ Module.register("newsfeed",{
},
// Define start sequence.
start: function() {
start: function () {
Log.info("Starting module: " + this.name);
// Set locale.
@@ -71,7 +70,7 @@ Module.register("newsfeed",{
},
// Override socket notification handler.
socketNotificationReceived: function(notification, payload) {
socketNotificationReceived: function (notification, payload) {
if (notification === "NEWS_ITEMS") {
this.generateFeed(payload);
@@ -84,7 +83,7 @@ Module.register("newsfeed",{
},
// Override dom generator.
getDom: function() {
getDom: function () {
var wrapper = document.createElement("div");
if (this.config.feedUrl) {
@@ -98,7 +97,6 @@ Module.register("newsfeed",{
}
if (this.newsItems.length > 0) {
// this.config.showFullArticle is a run-time configuration, triggered by optional notifications
if (!this.config.showFullArticle && (this.config.showSourceTitle || this.config.showPublishDate)) {
var sourceAndTimestamp = document.createElement("div");
@@ -113,7 +111,7 @@ Module.register("newsfeed",{
if (this.config.showPublishDate) {
sourceAndTimestamp.innerHTML += moment(new Date(this.newsItems[this.activeItem].pubdate)).fromNow();
}
if (this.config.showSourceTitle && this.newsItems[this.activeItem].sourceTitle !== "" || this.config.showPublishDate) {
if ((this.config.showSourceTitle && this.newsItems[this.activeItem].sourceTitle !== "") || this.config.showPublishDate) {
sourceAndTimestamp.innerHTML += ":";
}
@@ -123,47 +121,42 @@ Module.register("newsfeed",{
//Remove selected tags from the beginning of rss feed items (title or description)
if (this.config.removeStartTags === "title" || this.config.removeStartTags === "both") {
for (let f=0; f<this.config.startTags.length;f++) {
if (this.newsItems[this.activeItem].title.slice(0,this.config.startTags[f].length) === this.config.startTags[f]) {
this.newsItems[this.activeItem].title = this.newsItems[this.activeItem].title.slice(this.config.startTags[f].length,this.newsItems[this.activeItem].title.length);
for (let f = 0; f < this.config.startTags.length; f++) {
if (this.newsItems[this.activeItem].title.slice(0, this.config.startTags[f].length) === this.config.startTags[f]) {
this.newsItems[this.activeItem].title = this.newsItems[this.activeItem].title.slice(this.config.startTags[f].length, this.newsItems[this.activeItem].title.length);
}
}
}
if (this.config.removeStartTags === "description" || this.config.removeStartTags === "both") {
if (this.isShowingDescription) {
for (let f=0; f<this.config.startTags.length;f++) {
if (this.newsItems[this.activeItem].description.slice(0,this.config.startTags[f].length) === this.config.startTags[f]) {
this.newsItems[this.activeItem].description = this.newsItems[this.activeItem].description.slice(this.config.startTags[f].length,this.newsItems[this.activeItem].description.length);
for (let f = 0; f < this.config.startTags.length; f++) {
if (this.newsItems[this.activeItem].description.slice(0, this.config.startTags[f].length) === this.config.startTags[f]) {
this.newsItems[this.activeItem].description = this.newsItems[this.activeItem].description.slice(this.config.startTags[f].length, this.newsItems[this.activeItem].description.length);
}
}
}
}
//Remove selected tags from the end of rss feed items (title or description)
if (this.config.removeEndTags) {
for (let f=0; f<this.config.endTags.length;f++) {
if (this.newsItems[this.activeItem].title.slice(-this.config.endTags[f].length)===this.config.endTags[f]) {
this.newsItems[this.activeItem].title = this.newsItems[this.activeItem].title.slice(0,-this.config.endTags[f].length);
for (let f = 0; f < this.config.endTags.length; f++) {
if (this.newsItems[this.activeItem].title.slice(-this.config.endTags[f].length) === this.config.endTags[f]) {
this.newsItems[this.activeItem].title = this.newsItems[this.activeItem].title.slice(0, -this.config.endTags[f].length);
}
}
if (this.isShowingDescription) {
for (let f=0; f<this.config.endTags.length;f++) {
if (this.newsItems[this.activeItem].description.slice(-this.config.endTags[f].length)===this.config.endTags[f]) {
this.newsItems[this.activeItem].description = this.newsItems[this.activeItem].description.slice(0,-this.config.endTags[f].length);
for (let f = 0; f < this.config.endTags.length; f++) {
if (this.newsItems[this.activeItem].description.slice(-this.config.endTags[f].length) === this.config.endTags[f]) {
this.newsItems[this.activeItem].description = this.newsItems[this.activeItem].description.slice(0, -this.config.endTags[f].length);
}
}
}
}
if(!this.config.showFullArticle){
if (!this.config.showFullArticle) {
var title = document.createElement("div");
title.className = "newsfeed-title bright medium light" + (!this.config.wrapTitle ? " no-wrap" : "");
title.innerHTML = this.newsItems[this.activeItem].title;
@@ -174,7 +167,7 @@ Module.register("newsfeed",{
var description = document.createElement("div");
description.className = "newsfeed-desc small light" + (!this.config.wrapDescription ? " no-wrap" : "");
var txtDesc = this.newsItems[this.activeItem].description;
description.innerHTML = (this.config.truncDescription ? (txtDesc.length > this.config.lengthDescription ? txtDesc.substring(0, this.config.lengthDescription) + "..." : txtDesc) : txtDesc);
description.innerHTML = this.config.truncDescription ? (txtDesc.length > this.config.lengthDescription ? txtDesc.substring(0, this.config.lengthDescription) + "..." : txtDesc) : txtDesc;
wrapper.appendChild(description);
}
@@ -196,7 +189,6 @@ Module.register("newsfeed",{
if (this.config.hideLoading) {
this.show();
}
} else {
if (this.config.hideLoading) {
this.hide();
@@ -209,14 +201,14 @@ Module.register("newsfeed",{
return wrapper;
},
getActiveItemURL: function() {
getActiveItemURL: function () {
return typeof this.newsItems[this.activeItem].url === "string" ? this.newsItems[this.activeItem].url : this.newsItems[this.activeItem].url.href;
},
/* registerFeeds()
* registers the feeds to be used by the backend.
*/
registerFeeds: function() {
registerFeeds: function () {
for (var f in this.config.feeds) {
var feed = this.config.feeds[f];
this.sendSocketNotification("ADD_FEED", {
@@ -231,7 +223,7 @@ Module.register("newsfeed",{
*
* attribute feeds object - An object with feeds returned by the node helper.
*/
generateFeed: function(feeds) {
generateFeed: function (feeds) {
var newsItems = [];
for (var feed in feeds) {
var feedItems = feeds[feed];
@@ -239,24 +231,24 @@ Module.register("newsfeed",{
for (var i in feedItems) {
var item = feedItems[i];
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.sort(function(a,b) {
newsItems.sort(function (a, b) {
var dateA = new Date(a.pubdate);
var dateB = new Date(b.pubdate);
return dateB - dateA;
});
if(this.config.maxNewsItems > 0) {
if (this.config.maxNewsItems > 0) {
newsItems = newsItems.slice(0, this.config.maxNewsItems);
}
if(this.config.prohibitedWords.length > 0) {
newsItems = newsItems.filter(function(value){
for (var i=0; i < this.config.prohibitedWords.length; i++) {
if (this.config.prohibitedWords.length > 0) {
newsItems = newsItems.filter(function (value) {
for (var i = 0; i < this.config.prohibitedWords.length; i++) {
if (value["title"].toLowerCase().indexOf(this.config.prohibitedWords[i].toLowerCase()) > -1) {
return false;
}
@@ -267,8 +259,8 @@ Module.register("newsfeed",{
// get updated news items and broadcast them
var updatedItems = [];
newsItems.forEach(value => {
if (this.newsItems.findIndex(value1 => value1 === value) === -1) {
newsItems.forEach((value) => {
if (this.newsItems.findIndex((value1) => value1 === value) === -1) {
// Add item to updated items list
updatedItems.push(value);
}
@@ -276,7 +268,7 @@ Module.register("newsfeed",{
// check if updated items exist, if so and if we should broadcast these updates, then lets do so
if (this.config.broadcastNewsUpdates && updatedItems.length > 0) {
this.sendNotification("NEWS_FEED_UPDATE", {items: updatedItems});
this.sendNotification("NEWS_FEED_UPDATE", { items: updatedItems });
}
this.newsItems = newsItems;
@@ -289,7 +281,7 @@ Module.register("newsfeed",{
*
* returns bool
*/
subscribedToFeed: function(feedUrl) {
subscribedToFeed: function (feedUrl) {
for (var f in this.config.feeds) {
var feed = this.config.feeds[f];
if (feed.url === feedUrl) {
@@ -306,7 +298,7 @@ Module.register("newsfeed",{
*
* returns string
*/
titleForFeed: function(feedUrl) {
titleForFeed: function (feedUrl) {
for (var f in this.config.feeds) {
var feed = this.config.feeds[f];
if (feed.url === feedUrl) {
@@ -319,23 +311,23 @@ Module.register("newsfeed",{
/* scheduleUpdateInterval()
* Schedule visual update.
*/
scheduleUpdateInterval: function() {
scheduleUpdateInterval: function () {
var self = this;
self.updateDom(self.config.animationSpeed);
// Broadcast NewsFeed if needed
if (self.config.broadcastNewsFeeds) {
self.sendNotification("NEWS_FEED", {items: self.newsItems});
self.sendNotification("NEWS_FEED", { items: self.newsItems });
}
this.timer = setInterval(function() {
this.timer = setInterval(function () {
self.activeItem++;
self.updateDom(self.config.animationSpeed);
// Broadcast NewsFeed if needed
if (self.config.broadcastNewsFeeds) {
self.sendNotification("NEWS_FEED", {items: self.newsItems});
self.sendNotification("NEWS_FEED", { items: self.newsItems });
}
}, this.config.updateInterval);
},
@@ -347,25 +339,25 @@ Module.register("newsfeed",{
*
* return string - Capitalized output string.
*/
capitalizeFirstLetter: function(string) {
capitalizeFirstLetter: function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
resetDescrOrFullArticleAndTimer: function() {
resetDescrOrFullArticleAndTimer: function () {
this.isShowingDescription = this.config.showDescription;
this.config.showFullArticle = false;
this.scrollPosition = 0;
// reset bottom bar alignment
document.getElementsByClassName("region bottom bar")[0].style.bottom = "0";
document.getElementsByClassName("region bottom bar")[0].style.top = "inherit";
if(!this.timer){
if (!this.timer) {
this.scheduleUpdateInterval();
}
},
notificationReceived: function(notification, payload, sender) {
notificationReceived: function (notification, payload, sender) {
var before = this.activeItem;
if(notification === "ARTICLE_NEXT"){
if (notification === "ARTICLE_NEXT") {
this.activeItem++;
if (this.activeItem >= this.newsItems.length) {
this.activeItem = 0;
@@ -373,7 +365,7 @@ Module.register("newsfeed",{
this.resetDescrOrFullArticleAndTimer();
Log.info(this.name + " - going from article #" + before + " to #" + this.activeItem + " (of " + this.newsItems.length + ")");
this.updateDom(100);
} else if(notification === "ARTICLE_PREVIOUS"){
} else if (notification === "ARTICLE_PREVIOUS") {
this.activeItem--;
if (this.activeItem < 0) {
this.activeItem = this.newsItems.length - 1;
@@ -383,51 +375,50 @@ Module.register("newsfeed",{
this.updateDom(100);
}
// if "more details" is received the first time: show article summary, on second time show full article
else if(notification === "ARTICLE_MORE_DETAILS"){
else if (notification === "ARTICLE_MORE_DETAILS") {
// full article is already showing, so scrolling down
if(this.config.showFullArticle === true){
if (this.config.showFullArticle === true) {
this.scrollPosition += this.config.scrollLength;
window.scrollTo(0, this.scrollPosition);
Log.info(this.name + " - scrolling down");
Log.info(this.name + " - ARTICLE_MORE_DETAILS, scroll position: " + this.config.scrollLength);
}
else {
} else {
this.showFullArticle();
}
} else if(notification === "ARTICLE_SCROLL_UP"){
if(this.config.showFullArticle === true){
} else if (notification === "ARTICLE_SCROLL_UP") {
if (this.config.showFullArticle === true) {
this.scrollPosition -= this.config.scrollLength;
window.scrollTo(0, this.scrollPosition);
Log.info(this.name + " - scrolling up");
Log.info(this.name + " - ARTICLE_SCROLL_UP, scroll position: " + this.config.scrollLength);
}
} else if(notification === "ARTICLE_LESS_DETAILS"){
} else if (notification === "ARTICLE_LESS_DETAILS") {
this.resetDescrOrFullArticleAndTimer();
Log.info(this.name + " - showing only article titles again");
this.updateDom(100);
} else if (notification === "ARTICLE_TOGGLE_FULL"){
if (this.config.showFullArticle){
} else if (notification === "ARTICLE_TOGGLE_FULL") {
if (this.config.showFullArticle) {
this.activeItem++;
this.resetDescrOrFullArticleAndTimer();
} else {
this.showFullArticle();
}
} else if (notification === "ARTICLE_INFO_REQUEST"){
} else if (notification === "ARTICLE_INFO_REQUEST") {
this.sendNotification("ARTICLE_INFO_RESPONSE", {
title: this.newsItems[this.activeItem].title,
title: this.newsItems[this.activeItem].title,
source: this.newsItems[this.activeItem].sourceTitle,
date: this.newsItems[this.activeItem].pubdate,
desc: this.newsItems[this.activeItem].description,
url: this.getActiveItemURL()
date: this.newsItems[this.activeItem].pubdate,
desc: this.newsItems[this.activeItem].description,
url: this.getActiveItemURL()
});
}
},
showFullArticle: function() {
showFullArticle: function () {
this.isShowingDescription = !this.isShowingDescription;
this.config.showFullArticle = !this.isShowingDescription;
// make bottom bar align to top to allow scrolling
if(this.config.showFullArticle === true){
if (this.config.showFullArticle === true) {
document.getElementsByClassName("region bottom bar")[0].style.bottom = "inherit";
document.getElementsByClassName("region bottom bar")[0].style.top = "-90px";
}

View File

@@ -11,13 +11,13 @@ var Fetcher = require("./fetcher.js");
module.exports = NodeHelper.create({
// Subclass start method.
start: function() {
start: function () {
console.log("Starting module: " + this.name);
this.fetchers = [];
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
socketNotificationReceived: function (notification, payload) {
if (notification === "ADD_FEED") {
this.createFetcher(payload.feed, payload.config);
return;
@@ -31,7 +31,7 @@ module.exports = NodeHelper.create({
* attribute feed object - A feed object.
* attribute config object - A configuration object containing reload interval in milliseconds.
*/
createFetcher: function(feed, config) {
createFetcher: function (feed, config) {
var self = this;
var url = feed.url || "";
@@ -48,11 +48,11 @@ module.exports = NodeHelper.create({
console.log("Create new news fetcher for url: " + url + " - Interval: " + reloadInterval);
fetcher = new Fetcher(url, reloadInterval, encoding, config.logFeedWarnings);
fetcher.onReceive(function(fetcher) {
fetcher.onReceive(function (fetcher) {
self.broadcastFeeds();
});
fetcher.onError(function(fetcher, error) {
fetcher.onError(function (fetcher, error) {
self.sendSocketNotification("FETCH_ERROR", {
url: fetcher.url(),
error: error
@@ -74,7 +74,7 @@ module.exports = NodeHelper.create({
* Creates an object with all feed items of the different registered feeds,
* and broadcasts these using sendSocketNotification.
*/
broadcastFeeds: function() {
broadcastFeeds: function () {
var feeds = {};
for (var f in this.fetchers) {
feeds[f] = this.fetchers[f].items();

View File

@@ -1,3 +1,3 @@
{
"configuration_changed": "Die Konfigurationsoptionen für das Newsfeed-Modul haben sich geändert. \nBitte überprüfen Sie die Dokumentation."
}
"configuration_changed": "Die Konfigurationsoptionen für das Newsfeed-Modul haben sich geändert. \nBitte überprüfen Sie die Dokumentation."
}

View File

@@ -1,3 +1,3 @@
{
"configuration_changed": "The configuration options for the newsfeed module have changed.\nPlease check the documentation."
}
"configuration_changed": "The configuration options for the newsfeed module have changed.\nPlease check the documentation."
}

View File

@@ -1,3 +1,3 @@
{
"configuration_changed": "Las opciones de configuración para el módulo de suministro de noticias han cambiado. \nVerifique la documentación."
}
"configuration_changed": "Las opciones de configuración para el módulo de suministro de noticias han cambiado. \nVerifique la documentación."
}

View File

@@ -1,3 +1,3 @@
{
"configuration_changed": "Les options de configuration du module newsfeed ont changé. \nVeuillez consulter la documentation."
}
"configuration_changed": "Les options de configuration du module newsfeed ont changé. \nVeuillez consulter la documentation."
}