silvrgit/lib/btc.js

129 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-06-12 12:37:38 +00:00
let http = require('http');
2017-08-07 14:14:32 +00:00
let https = require('https');
2017-06-12 12:37:38 +00:00
let btcCache = {};
2017-08-07 14:14:32 +00:00
let balanceCache = {};
2017-06-12 12:37:38 +00:00
exports.doBTC = function (req, res) {
2017-04-05 13:22:31 +00:00
console.log('Bitcoin request');
2017-06-12 12:37:38 +00:00
function btcQuery (callback, r) {
var req = r;
var options = {
host: 'api.coindesk.com',
// port: 80,
path: '/v1/bpi/currentprice.json',
// method: 'GET',
headers: {
/* 'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)*/
2016-03-08 21:52:21 +00:00
2017-06-12 12:37:38 +00:00
}
};
try {
http.request(options).on('response', function (response) {
var data = '';
response.on('data', function (chunk) {
data += chunk;
});
response.on('end', function () {
console.log('>> data', data);
2017-06-12 12:47:24 +00:00
let cData = {};
2017-04-05 13:22:31 +00:00
try {
2017-06-12 12:47:24 +00:00
cData = JSON.parse(data);
}
catch (e) {
2017-06-12 12:37:38 +00:00
console.error(e);
}
2017-06-12 12:47:24 +00:00
finally{
callback(cData, r);
}
2017-06-12 12:37:38 +00:00
});
response.on('error', function (e) {
console.error(e);
});
}).end();
} catch (e) {
console.error(e);
}
}
2016-03-08 21:52:21 +00:00
2017-04-05 13:22:31 +00:00
let now = new Date();
if (now - GLOBAL.lastcheck > (59000 )) {
2017-06-12 12:37:38 +00:00
btcQuery(function (a) {
// console.log(a);
console.log('Got btc data.');
btcCache = a;
GLOBAL.lastcheck = now;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(btcCache));
}, res);
} else {
console.log('Using cache');
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(btcCache));
}
2017-04-05 13:22:31 +00:00
};
2017-08-07 14:14:32 +00:00
exports.doBalance = function (req, res) {
console.log('Bitcoin request');
function balanceQuery (callback, r) {
var req = r;
// https://blockexplorer.com/api/addr/18sLVW5Aswp2KWLr4hMFZsuSPtvAauFiif
var options = {
host: 'blockexplorer.com',
// port: 80,
path: '/api/addr/18sLVW5Aswp2KWLr4hMFZsuSPtvAauFiif',
// method: 'GET',
headers: {
/* 'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)*/
}
};
try {
https.get('https://blockexplorer.com/api/addr/18sLVW5Aswp2KWLr4hMFZsuSPtvAauFiif').on('response', function (response) {
var data = '';
response.on('data', function (chunk) {
data += chunk;
});
response.on('end', function () {
console.log('>> data', data);
let cData = {};
try {
cData = JSON.parse(data);
}
catch (e) {
console.error(e);
}
finally{
callback(cData, r);
}
});
response.on('error', function (e) {
console.error(e);
});
}).end();
} catch (e) {
console.error(e);
}
}
let now = new Date();
if (now - GLOBAL.lastcheck > (59000 )) {
balanceQuery(function (a) {
// console.log(a);
console.log('Got balance data.');
balanceCache = a;
GLOBAL.lastcheck = now;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(balanceCache));
}, res);
} else {
console.log('Using cache');
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(balanceCache));
}
};