-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
72 lines (60 loc) · 2.05 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
/*
Xapo Bitcoin Price
====================================
Developer : Zulkifli Mohamed (putera)
E-mail : [email protected]
*/
var NodeHelper = require('node_helper');
var request = require('request');
var async = require('async');
module.exports = NodeHelper.create(
{
start: function() {
console.log('Xapo Bitcoin price module started...');
},
getPrice: function(currency) {
var self = this;
var priceBuy = 0.00; var priceSell = 0.00;
currency = currency.toUpperCase();
var cBTC = currency + 'BTC';
var BTCc = 'BTC' + currency;
var urlBuy = 'https://api.xapo.com/v3/quotes/' + cBTC;
var urlSell = 'https://api.xapo.com/v3/quotes/' + BTCc;
async.parallel({
buy: function(callback) {
request({ url: urlBuy, method: 'GET' }, function(error, response, body) {
if (!error && response.statusCode == 200) {
callback(error, body);
}
});
},
sell: function(callback) {
request({ url: urlSell, method: 'GET' }, function(error, response, body) {
if (!error && response.statusCode == 200) {
callback(error, body);
}
});
}
},
function(error, result) {
if (error) {
console.log('[MMM-BTC-XAPO] ' + error);
}
var rBuy = JSON.parse(result.buy), rSell = JSON.parse(result.sell);
if (rBuy) {
priceBuy = rBuy.fx_etoe[cBTC].source_amt.toFixed(2);
}
if (rSell) {
priceSell = rSell.fx_etoe[BTCc].rate.toFixed(2);
}
var price = {buy: priceBuy, sell: priceSell};
self.sendSocketNotification('PRICE_RESULT', price);
});
},
socketNotificationReceived: function(notification, payload)
{
if (notification === 'GET_PRICE') {
this.getPrice(payload);
}
}
});