forked from retroflex/MMM-Skolmaten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
58 lines (50 loc) · 1.53 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* Magic Mirror
* Module: MMM-Skolmaten
*
* By Johan Persson, https://github.com/retroflex
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const Parser = require('rss-parser');
module.exports = NodeHelper.create({
start: function() {
},
// Send RSS items to the module js.
// @param url - URL of the RSS feed.
// @param items - Array of RSS items. Each item has a title and a description.
// @param self - Pointer to this. Needed when this method is used as callback.
broadcastItems: function(url, items, self) {
self.sendSocketNotification('ITEMS', {
url: url,
items: items
});
},
// Notification from module js.
// @param notification - Notification type.
// @param payload - Contains url of RSS feed.
socketNotificationReceived: function(notification, payload) {
if (notification === 'LOAD_FEED') {
this.loadFeed(payload.url, this.broadcastItems);
return;
}
},
// Load and parse an RSS feed.
// @param url - URL of the RSS feed.
// @param allEntriesParsedCB - Callback called when all RSS items have been parsed.
// See broadcastItems() for args doc.
loadFeed: function(url, allEntriesParsedCB) {
var items = [];
var self = this;
Parser.parseURL(url, function(err, parsed) {
var len = parsed.feed.entries.length;
var count = 0;
parsed.feed.entries.forEach(function(entry) {
items.push( { title: entry.title, description: entry.content } );
++count;
if (count === len) {
allEntriesParsedCB(url, items, self);
}
});
});
}
});