-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
84 lines (78 loc) · 2.46 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Magic Mirror
* Module: MMM-Mashie-Skolmat
*
* By Johan Alvinger, https://github.com/Alvinger
* Based on a script by Johan Persson, https://github.com/retroflex
* MIT Licensed.
*/
const Log = require("../../js/logger.js");
const NodeHelper = require('node_helper');
const ical = require('./vendor/ical.js');
const moment = require('moment');
module.exports = NodeHelper.create({
start: function() {
},
// Send items to the module js.
// @param url - URL of the feed.
// @param items - Array of 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 feed.
socketNotificationReceived: function(notification, payload) {
if (notification === 'LOAD_FEED') {
this.loadFeed(payload.config, this.broadcastItems);
return;
}
},
// Load and parse a feed.
// @param url - URL of the feed.
// @param allEntriesParsedCB - Callback called when all items have been parsed.
// See broadcastItems() for args doc.
loadFeed: function(cfg, allEntriesParsedCB) {
var self = this;
var items = [];
var today = moment().startOf('day');
if (moment().hour() >= cfg.cutoffHour) {
// After cutoffHour, tomorrow is considered today
today.add(1, 'day');
}
ical.fromURL(cfg.url, {}, function (err, data) {
for (var k in data) {
if (data.hasOwnProperty(k)) {
var entry = data[k];
if (entry.type == 'VEVENT') {
// Don't show earlier days than today
if (moment(entry.start) < today) {
continue;
}
// Format title
var title = moment(entry.start).format('dddd [- Vecka] WW');
title = title.charAt(0).toUpperCase() + title.substring(1);
// Split description into separate lines
// Remove text up until first colon
// Skip empty lines
var desc = entry.description.split("\n");
var description = ''
for (var d in desc) {
desc[d] = desc[d].replace(/^.+:/, '');
desc[d] = desc[d].trim();
if (desc[d].length > 0) {
if (d > 0) {description += '<br>';}
description += desc[d];
}
}
items.push( { title: title, description: description } );
}
}
}
allEntriesParsedCB(cfg.url, items, self);
});
}
});