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-05 14:35:11 -04:00
|
|
|
feedUrl: "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml",
|
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,
|
|
|
|
encoding: "UTF-8" //ISO-8859-1
|
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
|
|
|
},
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
this.fetchNews();
|
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-03-30 12:20:46 +02:00
|
|
|
if (payload.url === this.config.feedUrl) {
|
|
|
|
this.newsItems = payload.items;
|
|
|
|
if (!this.loaded) {
|
|
|
|
this.scheduleUpdateInterval();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loaded = true;
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 15:44:43 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Override dom generator.
|
|
|
|
getDom: function() {
|
|
|
|
var wrapper = document.createElement("div");
|
|
|
|
|
2016-04-05 13:16:23 +02:00
|
|
|
// wrapper.className = "small";
|
|
|
|
// for (var n in this.newsItems) {
|
|
|
|
// var item = this.newsItems[n];
|
|
|
|
// wrapper.innerHTML += item.title + '<br>';
|
|
|
|
// }
|
|
|
|
// return wrapper;
|
|
|
|
|
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-03-29 15:44:43 +02:00
|
|
|
if (this.config.showPublishDate) {
|
|
|
|
var timestamp = document.createElement("div");
|
|
|
|
timestamp.className = "light small dimmed";
|
2016-04-05 14:35:11 -04:00
|
|
|
timestamp.innerHTML = this.capitalizeFirstLetter(moment(new Date(this.newsItems[this.activeItem].pubdate)).fromNow() + ":");
|
2016-03-30 12:20:46 +02:00
|
|
|
//timestamp.innerHTML = this.config.feedUrl;
|
2016-03-29 15:44:43 +02:00
|
|
|
wrapper.appendChild(timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
wrapper.innerHTML = "Loading news ...";
|
|
|
|
wrapper.className = "small dimmed";
|
|
|
|
}
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
},
|
|
|
|
|
|
|
|
/* fetchNews(compliments)
|
|
|
|
* Requests new data from news proxy.
|
|
|
|
*/
|
|
|
|
fetchNews: function() {
|
2016-04-05 14:35:11 -04:00
|
|
|
Log.log("Add news feed to fetcher: " + this.config.feedUrl);
|
|
|
|
this.sendSocketNotification("ADD_FEED", {
|
2016-03-30 12:20:46 +02:00
|
|
|
url: this.config.feedUrl,
|
2016-04-05 13:16:23 +02:00
|
|
|
reloadInterval: this.config.reloadInterval,
|
|
|
|
encoding: this.config.encoding
|
2016-03-30 12:20:46 +02:00
|
|
|
});
|
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
|
|
|
}
|
|
|
|
});
|