-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode_helper.js
executable file
·131 lines (110 loc) · 2.82 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
/* Magic Mirror
* Module: MMM-bergfex
*
* By Juergen Wolf-Hofer
* Apache 2.0 Licensed.
*/
const NodeHelper = require('node_helper');
var async = require('async');
var sys = require('sys');
var exec = require('child_process').exec;
const request = require('request');
const cheerio = require("cheerio");
// Constants
const URL = "http://www.bergfex.at/oesterreich/schneewerte/";
module.exports = NodeHelper.create({
start: function() {
console.log('Starting node helper: ' + this.name);
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === 'CONFIG') {
var self = this;
this.config = payload;
self.retrieveAndUpdate();
setInterval(function() {
self.retrieveAndUpdate();
}, this.config.updateInterval);
}
},
retrieveAndUpdate: function() {
var self = this;
console.log('retrieveAndUpdate()');
request(URL, function (err, response, html) {
let $ = cheerio.load(html);
var allSnowReports = [];
var tbody = $('tr.tr0');
tbody.each(function() {
var entry = parseEntry($(this));
allSnowReports.push(entry);
});
tbody = $('tr.tr1');
tbody.each(function() {
var entry = parseEntry($(this));
allSnowReports.push(entry);
});
console.log(allSnowReports.length + " snow reports from bergfex.at retrieved.");
var selSnowReports = [];
for (var i=0; i<self.config.skiareas.length; i++) {
console.log("searching for " + self.config.skiareas[i]);
selSnowReports.push(searchData(allSnowReports, self.config.skiareas[i]));
}
console.log(selSnowReports);
self.sendSocketNotification('SNOW_REPORT', selSnowReports);
});
}
});
function searchData(snow_reports, skiarea) {
for (var i=0; i<snow_reports.length; i++) {
if (snow_reports[i].skiarea === skiarea) {
return snow_reports[i];
}
}
return null;
}
function parseEntry(row) {
var entry = {skiarea: "", tal: "", berg: "", neu: "", lifte: ""};
var td1 = row.children().first();
var td2 = td1.next();
var td3 = td2.next();
var td4 = td3.next();
var td5 = td4.next();
var td6 = td5.next();
var td7 = td6.next();
entry.skiarea = td1.text().trim();
entry.tal = td2.text().trim();
entry.berg = td3.text().trim();
entry.neu = td4.text().trim();
entry.lifte = td5.text().trim();
entry.update = td7.text().trim();
return entry;
}
function testData() {
return [
{
skiarea: 'Gosau - Dachstein West',
tal: '80 cm',
berg: '100 cm',
neu: '10 cm',
lifte: '32/32',
update: 'Heute, 08:13'
},
{
skiarea: 'Gerlos - Zillertal',
tal: '40 cm',
berg: '70 cm',
neu: '15 cm',
lifte: '11/11',
update: 'Heute, 08:10'
},
{
skiarea: 'Hauser Kaibling',
tal: '85 cm',
berg: '70 cm',
neu: '20 cm',
lifte: '16/17',
update: 'Heute, 07:13'
},
];
}