mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-08-21 12:55:22 +00:00
Add module subfolder support.
This commit is contained in:
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