fixing btc
This commit is contained in:
parent
3d444486f1
commit
bcc7a3d87e
95
lib/btc.js
95
lib/btc.js
@ -1,52 +1,57 @@
|
||||
let http = require('http');
|
||||
let btcCache = {};
|
||||
exports.doBTC = function(req, res) {
|
||||
let http = require('http');
|
||||
let btcCache = {};
|
||||
exports.doBTC = function (req, res) {
|
||||
console.log('Bitcoin request');
|
||||
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)*/
|
||||
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)*/
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
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);
|
||||
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);
|
||||
callback(JSON.parse(data), r);
|
||||
});
|
||||
response.on('error', function(e) {
|
||||
console.error(e);
|
||||
});
|
||||
}).end();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
callback(JSON.parse(data), r);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
});
|
||||
response.on('error', function (e) {
|
||||
console.error(e);
|
||||
});
|
||||
}).end();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
let now = new Date();
|
||||
if (now - GLOBAL.lastcheck > (59000 )) {
|
||||
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));
|
||||
}
|
||||
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));
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user