-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_helper.js
More file actions
51 lines (45 loc) · 1.38 KB
/
node_helper.js
File metadata and controls
51 lines (45 loc) · 1.38 KB
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
var NodeHelper = require('node_helper');
var request = require('request');
module.exports = NodeHelper.create({
services: {
favqs: {
url: 'https://favqs.com/api/qotd',
content: function(json) { return json.quote.body },
author: function(json) { return json.quote.author },
},
forismatic_en: {
url: 'http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en',
content: function(json) { return json.quoteText },
author: function(json) { return json.quoteAuthor },
},
forismatic_ru: {
url: 'http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=ru',
content: function(json) { return json.quoteText },
author: function(json) { return json.quoteAuthor }
},
},
start: function() {
console.log('Starting node helper: ' + this.name);
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if(notification === 'GET_NEW_QUOTE') {
if (payload.service in this.services) {
var service = this.services[payload.service];
request(service.url, function (error, response, body) {
if (!error && response.statusCode == 200) {
try {
var json = JSON.parse(body);
self.sendSocketNotification('NEW_QUOTE', {
content: service.content(json),
author: service.author(json),
});
} catch (e) {
// JSON parsing failed
}
}
});
}
}
},
});