-
Notifications
You must be signed in to change notification settings - Fork 28
/
node_helper.js
executable file
·64 lines (59 loc) · 1.71 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
/* global module */
/* Magic Mirror
* Node Helper: MMM-COVID19
*
* By Jose Forte
* MIT Licensed.
*/
var NodeHelper = require('node_helper')
var request = require('request')
var byCountryUrl = 'https://coronavirus-monitor.p.rapidapi.com/coronavirus/cases_by_country.php'
var worldStatsUrl = 'https://coronavirus-monitor.p.rapidapi.com/coronavirus/worldstat.php'
module.exports = NodeHelper.create({
start: function () {
console.log('Starting node helper for: ' + this.name)
},
getGlobalStats: function(key) {
var self = this
var options = {
method: 'GET',
url: worldStatsUrl,
headers: {
'x-rapidapi-host': 'coronavirus-monitor.p.rapidapi.com',
'x-rapidapi-key': key
}
}
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var result = JSON.parse(body)
self.sendSocketNotification('GLOBAL_RESULT', result)
}
})
},
getStatsByCoutry: function(key) {
var self = this
var options = {
method: 'GET',
url: byCountryUrl,
headers: {
'x-rapidapi-host': 'coronavirus-monitor.p.rapidapi.com',
'x-rapidapi-key': key
}
}
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var result = JSON.parse(body)
self.sendSocketNotification('BYCOUNTRY_RESULT', result)
}
})
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === 'GET_BY_COUNTRY_STATS') {
this.getStatsByCoutry(payload)
}
if (notification === 'GET_GLOBAL_STATS') {
this.getGlobalStats(payload)
}
}
});