forked from mykle1/MMM-EveryNews
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
43 lines (40 loc) · 1.63 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
/* Magic Mirror
* Module: MMM-EveryNews
*
* By Mykle1
*
*/
const NodeHelper = require('node_helper');
const request = require('request');
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node_helper for: " + this.name);
},
getNatGeo: function(url) {
// @TODO the request api is deprecated. use a different API here to get these results. Perhaps the NewsAPI module?
request({
url: 'https://newsapi.org/v2/top-headlines?sources='+this.config.source+'&pageSize=50&sortBy=popularity&apiKey='+this.config.apiKey,
headers: {'User-Agent':this.config.userAgent},
method: 'GET'
}, (error, response, body) => {
if (!error && response.statusCode == 200) {
var result = JSON.parse(body).articles;
this.sendSocketNotification('NATGEO_RESULT', result);
} else {
// add error checking so the user gets a visible error they will need to do some troubleshooting
// this will be in lieu of the infinite loading indicator
var errorString = "Error loading NewsAPI data. <br> Error # " + response.statusCode + ", " + JSON.parse(body).code + "<br>" + JSON.parse(body).message;
this.sendSocketNotification('NATGEO_ERROR', errorString);
}
});
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'GET_NATGEO') {
this.getNatGeo(payload);
}
if (notification === 'CONFIG') {
this.config = payload;
// console.log(this.config);
}
}
});