-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitbay.js
105 lines (80 loc) · 2.3 KB
/
bitbay.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var args = process.argv
var currencyNameArg = args[2]
// List of my personal cryptocurrencies 😎
var myFavoriteCurrencies = ['LSK', 'ETH', 'BTC']
const https = require('https');
class Currency {
constructor(name, rate) {
this.name = name;
this.rate = rate;
}
}
function parseResponse(currencyName, jsonResponse) {
var rate = jsonResponse.bids[0][0];
if (rate) {
return new Currency(currencyName, rate);
}
return null;
}
var prepareCurrencyTableRow = (currencyName) => {
var currencyValTextRepresentation = currencyName + " ➡ PLN ";
return {'Currency': currencyValTextRepresentation}
}
var prepareRateTableRow = (currencyRate) => {
return { '🤑 RATE': String(currencyRate) }
}
var printTable = (currencyName, currencyRate) => {
var Table = require('cli-table');
var table = new Table();
table.push(
prepareCurrencyTableRow(currencyName),
prepareRateTableRow(currencyRate)
);
console.log(table.toString());
}
var sentRequest = (requestParameter ,callback) => {
var url = 'https://bitbay.net/API/Public/'+requestParameter+'PLN/orderbook.json';
https.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
var currencyObj = parseResponse(requestParameter, JSON.parse(data))
callback(true, currencyObj);
});
}).on("error", (err) => {
callback(false, null);
});
}
var getRequestParameters = () => {
var currencyNames;
if (currencyNameArg == null ) {
currencyNames = myFavoriteCurrencies;
} else {
currencyNames = [currencyNameArg];
}
return currencyNames;
}
var parameters = getRequestParameters();
var recursiveItemIndex = 0;
// Recurrence function (invoking with delay) which contains serial request to the 'bitbay' API
var showResults = (requestParameter) => {
if (recursiveItemIndex < parameters.length) {
sentRequest(requestParameter, (success, currencyObj) => {
if (success && currencyObj != null) {
printTable(currencyObj.name, currencyObj.rate);
} else {
console.log("💥 REQ ERROR");
}
setTimeout(() => {
recursiveItemIndex++;
showResults(parameters[recursiveItemIndex]);
}, 300);
})
}
}
// ------------------------------------------------
// Run script
// ------------------------------------------------
showResults(parameters[0])