mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-08-23 13:24:06 +00:00
Add module subfolder support.
This commit is contained in:
76
modules/default/newsfeed/README.md
Normal file
76
modules/default/newsfeed/README.md
Normal file
@@ -0,0 +1,76 @@
|
||||
# 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.
|
||||
|
||||
## Using the module
|
||||
|
||||
To use this module, add it to the modules array in the `config/config.js` file:
|
||||
````javascript
|
||||
modules: [
|
||||
{
|
||||
module: 'newsfeed',
|
||||
position: 'bottom_bar', // This can be any of the regions. Best results in center regions.
|
||||
config: {
|
||||
// The config property is optional.
|
||||
// If no config is set, an example calendar is shown.
|
||||
// See 'Configuration options' for more information.
|
||||
}
|
||||
}
|
||||
]
|
||||
````
|
||||
|
||||
## Configuration options
|
||||
|
||||
The following properties can be configured:
|
||||
|
||||
|
||||
<table width="100%">
|
||||
<!-- why, markdown... -->
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Option</th>
|
||||
<th width="100%">Description</th>
|
||||
</tr>
|
||||
<thead>
|
||||
<tbody>
|
||||
|
||||
<tr>
|
||||
<td><code>feedUrl</code></td>
|
||||
<td>The url of the feed used for the headlines.<br>
|
||||
<br><b>Default value:</b> <code>'http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml'</code>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><code>showPublishDate</code></td>
|
||||
<td>Display the publish date of an headline.<br>
|
||||
<br><b>Default value:</b> <code>true</code>
|
||||
<br><b>Default value:</b> <code>true</code> or <code>false</code>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><code>reloadInterval</code></td>
|
||||
<td>How often does the content needs to be fetched? (Milliseconds)<br>
|
||||
<br><b>Possible values:</b> <code>1000</code> - <code>86400000</code>
|
||||
<br><b>Default value:</b> <code>300000</code> (5 minutes)
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>updateInterval</code></td>
|
||||
<td>How often do you want to display a new headline? (Milliseconds)<br>
|
||||
<br><b>Possible values:</b><code>1000</code> - <code>60000</code>
|
||||
<br><b>Default value:</b> <code>7500</code> (7.5 seconds)
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>animationSpeed</code></td>
|
||||
<td>Speed of the update animation. (Milliseconds)<br>
|
||||
<br><b>Possible values:</b><code>0</code> - <code>5000</code>
|
||||
<br><b>Default value:</b> <code>2000</code> (2.5 seconds)
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
|
111
modules/default/newsfeed/fetcher.js
Normal file
111
modules/default/newsfeed/fetcher.js
Normal file
@@ -0,0 +1,111 @@
|
||||
/* Magic Mirror
|
||||
* Fetcher
|
||||
*
|
||||
* By Michael Teeuw http://michaelteeuw.nl
|
||||
* MIT Licensed.
|
||||
*/
|
||||
|
||||
var NewsFetcher = require('./newsfetcher.js');
|
||||
|
||||
/* Fetcher
|
||||
* Responsible for requesting an update on the set interval and broadcasting the data.
|
||||
*
|
||||
* attribute url string - URL of the news feed.
|
||||
* attribute reloadInterval number - Reload interval in milliseconds.
|
||||
*/
|
||||
|
||||
var Fetcher = function(url, reloadInterval) {
|
||||
var self = this;
|
||||
var newsFetcher = new NewsFetcher();
|
||||
if (reloadInterval < 1000) {
|
||||
reloadInterval = 1000;
|
||||
}
|
||||
|
||||
var reloadTimer = null;
|
||||
var items = [];
|
||||
|
||||
var fetchFailedCallback = function() {};
|
||||
var itemsReceivedCallback = function() {};
|
||||
|
||||
/* private methods */
|
||||
|
||||
/* fetchNews()
|
||||
* Request the new items from the newsFetcher.
|
||||
*/
|
||||
|
||||
var fetchNews = function() {
|
||||
//console.log('Fetch news.');
|
||||
clearTimeout(reloadTimer);
|
||||
reloadTimer = null;
|
||||
newsFetcher.fetchNews(url, function(fetchedItems) {
|
||||
items = fetchedItems;
|
||||
self.broadcastItems();
|
||||
scheduleTimer();
|
||||
}, function(error) {
|
||||
fetchFailedCallback(self, error);
|
||||
scheduleTimer();
|
||||
});
|
||||
};
|
||||
|
||||
/* scheduleTimer()
|
||||
* Schedule the timer for the next update.
|
||||
*/
|
||||
|
||||
var scheduleTimer = function() {
|
||||
//console.log('Schedule update timer.');
|
||||
clearTimeout(reloadTimer);
|
||||
reloadTimer = setTimeout(function() {
|
||||
fetchNews();
|
||||
}, reloadInterval);
|
||||
};
|
||||
|
||||
/* public methods */
|
||||
|
||||
/* setReloadInterval()
|
||||
* Update the reload interval, but only if we need to increase the speed.
|
||||
*
|
||||
* attribute interval number - Interval for the update in milliseconds.
|
||||
*/
|
||||
this.setReloadInterval = function(interval) {
|
||||
if (interval > 1000 && interval < reloadInterval) {
|
||||
reloadInterval = interval;
|
||||
}
|
||||
};
|
||||
|
||||
/* startFetch()
|
||||
* Initiate fetchNews();
|
||||
*/
|
||||
this.startFetch = function() {
|
||||
fetchNews();
|
||||
};
|
||||
|
||||
/* broadcastItems()
|
||||
* Broadcast the exsisting items.
|
||||
*/
|
||||
this.broadcastItems = function() {
|
||||
if (items.length <= 0) {
|
||||
//console.log('No items to broadcast yet.');
|
||||
return;
|
||||
}
|
||||
//console.log('Broadcasting ' + items.length + ' items.');
|
||||
itemsReceivedCallback(self);
|
||||
};
|
||||
|
||||
this.onReceive = function(callback) {
|
||||
itemsReceivedCallback = callback;
|
||||
};
|
||||
|
||||
this.onError = function(callback) {
|
||||
fetchFailedCallback = callback;
|
||||
};
|
||||
|
||||
this.url = function() {
|
||||
return url;
|
||||
};
|
||||
|
||||
this.items = function() {
|
||||
return items;
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = Fetcher;
|
123
modules/default/newsfeed/newsfeed.js
Normal file
123
modules/default/newsfeed/newsfeed.js
Normal file
@@ -0,0 +1,123 @@
|
||||
/* global Module */
|
||||
|
||||
/* Magic Mirror
|
||||
* Module: NewsFeed
|
||||
*
|
||||
* By Michael Teeuw http://michaelteeuw.nl
|
||||
* MIT Licensed.
|
||||
*/
|
||||
|
||||
Module.register('newsfeed',{
|
||||
|
||||
// Default module config.
|
||||
defaults: {
|
||||
feedUrl: 'http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml',
|
||||
showPublishDate: true,
|
||||
reloadInterval: 5 * 60 * 1000, // every 5 minutes
|
||||
updateInterval: 7.5 * 1000,
|
||||
animationSpeed: 2.5 * 1000,
|
||||
},
|
||||
|
||||
// Define required scripts.
|
||||
getScripts: function() {
|
||||
return ['moment.js'];
|
||||
},
|
||||
|
||||
// Define start sequence.
|
||||
start: function() {
|
||||
Log.info('Starting module: ' + this.name);
|
||||
|
||||
// Set locale.
|
||||
moment.locale(config.language);
|
||||
|
||||
this.newsItems = [];
|
||||
this.loaded = false;
|
||||
this.activeItem = 0;
|
||||
|
||||
this.fetchNews();
|
||||
|
||||
},
|
||||
|
||||
// Override socket notification handler.
|
||||
socketNotificationReceived: function(notification, payload) {
|
||||
if (notification === 'NEWS_ITEMS') {
|
||||
if (payload.url === this.config.feedUrl) {
|
||||
this.newsItems = payload.items;
|
||||
if (!this.loaded) {
|
||||
this.scheduleUpdateInterval();
|
||||
}
|
||||
|
||||
this.loaded = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// Override dom generator.
|
||||
getDom: function() {
|
||||
var wrapper = document.createElement("div");
|
||||
|
||||
if (this.activeItem >= this.newsItems.length) {
|
||||
this.activeItem = 0;
|
||||
}
|
||||
|
||||
if (this.newsItems.length > 0) {
|
||||
|
||||
if (this.config.showPublishDate) {
|
||||
var timestamp = document.createElement("div");
|
||||
timestamp.className = "light small dimmed";
|
||||
timestamp.innerHTML = this.capitalizeFirstLetter(moment(new Date(this.newsItems[this.activeItem].pubdate)).fromNow() + ':');
|
||||
//timestamp.innerHTML = this.config.feedUrl;
|
||||
wrapper.appendChild(timestamp);
|
||||
}
|
||||
|
||||
var title = document.createElement("div");
|
||||
title.className = "bright medium light";
|
||||
title.innerHTML = this.newsItems[this.activeItem].title;
|
||||
wrapper.appendChild(title);
|
||||
|
||||
} else {
|
||||
wrapper.innerHTML = "Loading news ...";
|
||||
wrapper.className = "small dimmed";
|
||||
}
|
||||
|
||||
return wrapper;
|
||||
},
|
||||
|
||||
/* fetchNews(compliments)
|
||||
* Requests new data from news proxy.
|
||||
*/
|
||||
fetchNews: function() {
|
||||
Log.log('Add news feed to fetcher: ' + this.config.feedUrl);
|
||||
this.sendSocketNotification('ADD_FEED', {
|
||||
url: this.config.feedUrl,
|
||||
reloadInterval: this.config.reloadInterval
|
||||
});
|
||||
},
|
||||
|
||||
/* 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) {
|
||||
return string.charAt(0).toUpperCase() + string.slice(1);
|
||||
}
|
||||
});
|
||||
|
||||
|
53
modules/default/newsfeed/newsfetcher.js
Normal file
53
modules/default/newsfeed/newsfetcher.js
Normal file
@@ -0,0 +1,53 @@
|
||||
/* Magic Mirror
|
||||
* NewsFetcher
|
||||
*
|
||||
* By Michael Teeuw http://michaelteeuw.nl
|
||||
* MIT Licensed.
|
||||
*/
|
||||
|
||||
var FeedMe = require('feedme');
|
||||
var request = require('request');
|
||||
|
||||
var NewsFetcher = function() {
|
||||
var self = this;
|
||||
|
||||
self.successCallback = function(){};
|
||||
self.errorCallback = function(){};
|
||||
|
||||
self.items = [];
|
||||
|
||||
var parser = new FeedMe();
|
||||
|
||||
parser.on('item', function(item) {
|
||||
//console.log(item);
|
||||
self.items.push({
|
||||
title: item.title,
|
||||
pubdate: item.pubdate,
|
||||
});
|
||||
});
|
||||
|
||||
parser.on('end', function(item) {
|
||||
self.successCallback(self.items);
|
||||
});
|
||||
|
||||
parser.on('error', function(error) {
|
||||
self.errorCallback(error);
|
||||
});
|
||||
|
||||
/* public methods */
|
||||
|
||||
/* fetchNews()
|
||||
* Fetch the new news items.
|
||||
*
|
||||
* attribute url string - The url to fetch.
|
||||
* attribute success function(items) - Callback on succes.
|
||||
* attribute error function(error) - Callback on error.
|
||||
*/
|
||||
self.fetchNews = function(url, success, error) {
|
||||
self.successCallback = success;
|
||||
self.errorCallback = error;
|
||||
request(url).pipe(parser);
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = NewsFetcher;
|
76
modules/default/newsfeed/node_helper.js
Normal file
76
modules/default/newsfeed/node_helper.js
Normal file
@@ -0,0 +1,76 @@
|
||||
/* Magic Mirror
|
||||
* Node Helper: Newsfeed
|
||||
*
|
||||
* By Michael Teeuw http://michaelteeuw.nl
|
||||
* MIT Licensed.
|
||||
*/
|
||||
|
||||
var NodeHelper = require('node_helper');
|
||||
var validUrl = require('valid-url');
|
||||
var Fetcher = require('./fetcher.js');
|
||||
|
||||
module.exports = NodeHelper.create({
|
||||
// Subclass start method.
|
||||
start: function() {
|
||||
console.log('Starting module: ' + this.name);
|
||||
|
||||
this.fetchers = [];
|
||||
},
|
||||
|
||||
// Subclass socketNotificationReceived received.
|
||||
socketNotificationReceived: function(notification, payload) {
|
||||
if(notification === 'ADD_FEED') {
|
||||
this.createFetcher(payload.url, payload.reloadInterval);
|
||||
}
|
||||
},
|
||||
|
||||
/* createFetcher(url, reloadInterval)
|
||||
* Creates a fetcher for a new url if it doesn't exsist yet.
|
||||
* Otherwise it reoses the exsisting one.
|
||||
*
|
||||
* attribute url string - URL of the news feed.
|
||||
* attribute reloadInterval number - Reload interval in milliseconds.
|
||||
*/
|
||||
|
||||
createFetcher: function(url, reloadInterval) {
|
||||
var self = this;
|
||||
|
||||
if (!validUrl.isUri(url)){
|
||||
self.sendSocketNotification('INCORRECT_URL', url);
|
||||
return;
|
||||
}
|
||||
|
||||
var fetcher;
|
||||
if (typeof self.fetchers[url] === 'undefined') {
|
||||
console.log('Create new news fetcher for url: ' + url + ' - Interval: ' + reloadInterval);
|
||||
fetcher = new Fetcher(url, reloadInterval);
|
||||
|
||||
fetcher.onReceive(function(fetcher) {
|
||||
self.sendSocketNotification('NEWS_ITEMS', {
|
||||
url: fetcher.url(),
|
||||
items: fetcher.items()
|
||||
});
|
||||
});
|
||||
|
||||
fetcher.onError(function(fetcher, error) {
|
||||
self.sendSocketNotification('FETCH_ERROR', {
|
||||
url: fetcher.url(),
|
||||
error: error
|
||||
});
|
||||
});
|
||||
|
||||
self.fetchers[url] = fetcher;
|
||||
} else {
|
||||
console.log('Use exsisting news fetcher for url: ' + url);
|
||||
fetcher = self.fetchers[url];
|
||||
fetcher.setReloadInterval(reloadInterval);
|
||||
fetcher.broadcastItems();
|
||||
}
|
||||
|
||||
fetcher.startFetch();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user