-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
55 lines (42 loc) · 1.42 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
/* Magic Mirror Module: MMM-NOAA-NHC helper
* Version: 1.0.0
*
* By Akira Heid https://github.com/akiraheid/
* MIT Licensed.
*/
const NodeHelper = require('node_helper')
const request = require('request')
const DOMParser = require('@xmldom/xmldom').DOMParser
module.exports = NodeHelper.create({
start: function () {
// Set up local values
this.result = null
},
getData: function(payload) {
const that = this
this.url = payload
request({url: this.url, method: 'GET'}, function(error, response, body) {
if (!error && response.statusCode == 200) {
const result = {}
const doc = new DOMParser().parseFromString(body)
const descriptions = doc.getElementsByTagName('description')
result.atlanticActive = that.isAreaActive(descriptions[2].textContent)
result.pacificActive = that.isAreaActive(descriptions[3].textContent)
that.result = result
}
that.sendSocketNotification(
'GOT-TROPICAL-DATA', {'url': that.url, 'result': that.result})
})
},
isAreaActive: function(cdata) {
// Parse CDATA text and determine if there is activity
const doc = new DOMParser().parseFromString(cdata)
const textDiv = doc.getElementsByTagName('div')[1]
return textDiv == undefined ? false : textDiv.textContent.search(/formation is not expected/) == -1
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'GET-TROPICAL-DATA') {
this.getData(payload)
}
}
})