2016-03-29 15:44:43 +02:00
|
|
|
/* global Module */
|
|
|
|
|
|
|
|
/* Magic Mirror
|
|
|
|
* Module: NewsFeed
|
|
|
|
*
|
|
|
|
* By Michael Teeuw http://michaelteeuw.nl
|
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
Module.register("newsfeed",{
|
2016-03-29 15:44:43 +02:00
|
|
|
|
|
|
|
// Default module config.
|
|
|
|
defaults: {
|
2016-04-20 14:39:38 +02:00
|
|
|
feeds: [
|
|
|
|
{
|
|
|
|
title: "New York Times",
|
|
|
|
url: "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml",
|
|
|
|
encoding: "UTF-8" //ISO-8859-1
|
|
|
|
}
|
|
|
|
],
|
|
|
|
showSourceTitle: true,
|
2016-03-29 15:44:43 +02:00
|
|
|
showPublishDate: true,
|
2016-04-12 11:06:59 +02:00
|
|
|
showDescription: false,
|
2016-03-31 11:05:32 +02:00
|
|
|
reloadInterval: 5 * 60 * 1000, // every 5 minutes
|
2016-04-12 11:06:59 +02:00
|
|
|
updateInterval: 10 * 1000,
|
2016-04-05 14:35:11 -04:00
|
|
|
animationSpeed: 2.5 * 1000,
|
2016-06-06 00:16:49 +02:00
|
|
|
maxNewsItems: 0 // 0 for unlimited
|
2016-03-29 15:44:43 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Define required scripts.
|
|
|
|
getScripts: function() {
|
2016-04-05 14:35:11 -04:00
|
|
|
return ["moment.js"];
|
2016-03-29 15:44:43 +02:00
|
|
|
},
|
|
|
|
|
2016-05-11 12:38:41 +02:00
|
|
|
// Define required translations.
|
|
|
|
getTranslations: function() {
|
|
|
|
// The translations for the defaut modules are defined in the core translation files.
|
|
|
|
// Therefor we can just return false. Otherwise we should have returned a dictionairy.
|
|
|
|
// If you're trying to build yiur own module including translations, check out the documentation.
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2016-03-29 15:44:43 +02:00
|
|
|
// Define start sequence.
|
|
|
|
start: function() {
|
2016-04-05 14:35:11 -04:00
|
|
|
Log.info("Starting module: " + this.name);
|
2016-03-29 15:44:43 +02:00
|
|
|
|
|
|
|
// Set locale.
|
|
|
|
moment.locale(config.language);
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2016-03-29 15:44:43 +02:00
|
|
|
this.newsItems = [];
|
|
|
|
this.loaded = false;
|
|
|
|
this.activeItem = 0;
|
2016-03-30 12:20:46 +02:00
|
|
|
|
2016-04-20 14:39:38 +02:00
|
|
|
this.registerFeeds();
|
2016-03-31 13:05:23 +02:00
|
|
|
|
2016-03-30 12:20:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Override socket notification handler.
|
|
|
|
socketNotificationReceived: function(notification, payload) {
|
2016-04-05 14:35:11 -04:00
|
|
|
if (notification === "NEWS_ITEMS") {
|
2016-04-20 14:39:38 +02:00
|
|
|
this.generateFeed(payload);
|
2016-03-30 12:20:46 +02:00
|
|
|
|
2016-04-20 14:39:38 +02:00
|
|
|
if (!this.loaded) {
|
|
|
|
this.scheduleUpdateInterval();
|
2016-03-30 12:20:46 +02:00
|
|
|
}
|
2016-04-20 14:39:38 +02:00
|
|
|
|
2016-05-11 12:38:41 +02:00
|
|
|
this.loaded = true;
|
2016-03-30 12:20:46 +02:00
|
|
|
}
|
2016-03-29 15:44:43 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Override dom generator.
|
|
|
|
getDom: function() {
|
|
|
|
var wrapper = document.createElement("div");
|
|
|
|
|
2016-04-20 14:39:38 +02:00
|
|
|
if (this.config.feedUrl) {
|
|
|
|
wrapper.className = "small bright";
|
|
|
|
wrapper.innerHTML = "The configuration options for the newsfeed module have changed.<br>Please check the documentation.";
|
|
|
|
return wrapper;
|
|
|
|
}
|
2016-04-05 13:16:23 +02:00
|
|
|
|
2016-03-29 15:44:43 +02:00
|
|
|
if (this.activeItem >= this.newsItems.length) {
|
|
|
|
this.activeItem = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.newsItems.length > 0) {
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2016-04-20 14:39:38 +02:00
|
|
|
if (this.config.showSourceTitle || this.config.showPublishDate) {
|
|
|
|
var sourceAndTimestamp = document.createElement("div");
|
|
|
|
sourceAndTimestamp.className = "light small dimmed";
|
|
|
|
|
|
|
|
if (this.config.showSourceTitle && this.newsItems[this.activeItem].sourceTitle !== '') sourceAndTimestamp.innerHTML = this.newsItems[this.activeItem].sourceTitle;
|
|
|
|
if (this.config.showSourceTitle && this.newsItems[this.activeItem].sourceTitle !== '' && this.config.showPublishDate) sourceAndTimestamp.innerHTML += ', ';
|
|
|
|
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) sourceAndTimestamp.innerHTML += ':';
|
|
|
|
|
|
|
|
wrapper.appendChild(sourceAndTimestamp);
|
2016-03-29 15:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var title = document.createElement("div");
|
|
|
|
title.className = "bright medium light";
|
|
|
|
title.innerHTML = this.newsItems[this.activeItem].title;
|
|
|
|
wrapper.appendChild(title);
|
|
|
|
|
2016-04-12 11:06:59 +02:00
|
|
|
if (this.config.showDescription) {
|
|
|
|
var description = document.createElement("div");
|
|
|
|
description.className = "small light";
|
|
|
|
description.innerHTML = this.newsItems[this.activeItem].description;
|
|
|
|
wrapper.appendChild(description);
|
|
|
|
}
|
|
|
|
|
2016-03-29 15:44:43 +02:00
|
|
|
} else {
|
2016-05-11 12:38:41 +02:00
|
|
|
wrapper.innerHTML = this.translate("LOADING");
|
2016-03-29 15:44:43 +02:00
|
|
|
wrapper.className = "small dimmed";
|
|
|
|
}
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
},
|
|
|
|
|
2016-04-20 14:39:38 +02:00
|
|
|
/* registerFeeds()
|
|
|
|
* registers the feeds to be used by the backend.
|
2016-03-29 15:44:43 +02:00
|
|
|
*/
|
2016-04-20 14:39:38 +02:00
|
|
|
|
|
|
|
registerFeeds: function() {
|
|
|
|
for (var f in this.config.feeds) {
|
|
|
|
var feed = this.config.feeds[f];
|
|
|
|
this.sendSocketNotification("ADD_FEED", {
|
|
|
|
feed: feed,
|
|
|
|
config: this.config
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/* registerFeeds()
|
|
|
|
* Generate an ordered list of items for this configured module.
|
|
|
|
*
|
|
|
|
* attribute feeds object - An object with feeds returned by the nod helper.
|
|
|
|
*/
|
|
|
|
generateFeed: function(feeds) {
|
|
|
|
var newsItems = [];
|
|
|
|
for (var feed in feeds) {
|
|
|
|
var feedItems = feeds[feed];
|
|
|
|
if (this.subscribedToFeed(feed)) {
|
|
|
|
for (var i in feedItems) {
|
|
|
|
var item = feedItems[i];
|
|
|
|
item.sourceTitle = this.titleForFeed(feed);
|
|
|
|
newsItems.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newsItems.sort(function(a,b) {
|
|
|
|
var dateA = new Date(a.pubdate);
|
|
|
|
var dateB = new Date(b.pubdate);
|
|
|
|
return dateB - dateA;
|
2016-03-30 12:20:46 +02:00
|
|
|
});
|
2016-06-06 00:16:49 +02:00
|
|
|
if(this.config.maxNewsItems > 0) {
|
|
|
|
newsItems = newsItems.slice(0, this.config.maxNewsItems);
|
|
|
|
}
|
2016-04-20 14:39:38 +02:00
|
|
|
this.newsItems = newsItems;
|
|
|
|
},
|
|
|
|
|
|
|
|
/* subscribedToFeed(feedUrl)
|
|
|
|
* Check if this module is configured to show this feed.
|
|
|
|
*
|
|
|
|
* attribute feedUrl string - Url of the feed to check.
|
|
|
|
*
|
|
|
|
* returns bool
|
|
|
|
*/
|
|
|
|
subscribedToFeed: function(feedUrl) {
|
|
|
|
for (var f in this.config.feeds) {
|
|
|
|
var feed = this.config.feeds[f];
|
|
|
|
if (feed.url === feedUrl) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/* subscribedToFeed(feedUrl)
|
|
|
|
* Returns title for a specific feed Url.
|
|
|
|
*
|
|
|
|
* attribute feedUrl string - Url of the feed to check.
|
|
|
|
*
|
|
|
|
* returns string
|
|
|
|
*/
|
|
|
|
titleForFeed: function(feedUrl) {
|
|
|
|
for (var f in this.config.feeds) {
|
|
|
|
var feed = this.config.feeds[f];
|
|
|
|
if (feed.url === feedUrl) {
|
|
|
|
return feed.title || '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
2016-03-29 15:44:43 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/* scheduleUpdateInterval()
|
|
|
|
* Schedule visual update.
|
|
|
|
*/
|
|
|
|
scheduleUpdateInterval: function() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.updateDom(self.config.animationSpeed);
|
|
|
|
|
|
|
|
setInterval(function() {
|
|
|
|
self.activeItem++;
|
|
|
|
self.updateDom(self.config.animationSpeed);
|
|
|
|
}, this.config.updateInterval);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* capitalizeFirstLetter(string)
|
|
|
|
* Capitalizes the first character of a string.
|
|
|
|
*
|
|
|
|
* argument string string - Input string.
|
|
|
|
*
|
|
|
|
* return string - Capitalized output string.
|
|
|
|
*/
|
|
|
|
capitalizeFirstLetter: function(string) {
|
2016-04-05 14:35:11 -04:00
|
|
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
2016-03-29 15:44:43 +02:00
|
|
|
}
|
|
|
|
});
|