-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
186 lines (166 loc) · 4.73 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#! /usr/bin/env node
const request = require('request');
const chalk = require('chalk');
const Table = require('cli-table');
const figlet = require('figlet');
const Barcli = require("barcli");
const path = require('path');
const program = require('commander');
const getSymbolFromCurrency = require('currency-symbol-map');
const portfolio = require(path.resolve(__dirname,'portfolio.json'));
/**
* Command Line Options
*/
program
.version('1.4.3')
.option('-c, --currency [value]', 'An optional currency value', 'USD')
.parse(process.argv);
/**
* Set currency
*/
var curUp = program.currency.toUpperCase();
var curLow = program.currency.toLowerCase();
var curSym = getSymbolFromCurrency(curUp);
/**
* Loading Message Figlet Style
*/
figletLog('Crypto Portfolio Loading...');
/**
* Request and table
*/
const requestUrl = 'https://api.coinmarketcap.com/v1/ticker/?convert=' + curUp + '&limit=-1';
request(requestUrl, function (error, response, body) {
var data = JSON.parse(body);
var table = new Table({ head: [
chalk.blue('Rank'),
chalk.blue('Coin'),
chalk.blue(`${curUp} Price`),
chalk.blue('Coins Owned'),
chalk.blue('Net Worth'),
chalk.blue('24 Hour Volume'),
chalk.blue('Market Cap'),
chalk.blue('1 Hour'),
chalk.blue('24 Hours'),
chalk.blue('7 Days'),
chalk.blue('Last Updated'),
] });
var portfolioTotal = 0;
var barData = {};
data.forEach(function (value, key) {
if(portfolio.hasOwnProperty(value.id)) {
table.push([
chalk.blue(value.rank),
chalk.green(value.id),
chalk.green(curSym+addCommas(value['price_'+curLow])),
chalk.green(addCommas(portfolio[value.id])),
chalk.green(curSym+addCommas(Number(Math.round(value['price_'+curLow] * portfolio[value.id])))),
chalk.green(curSym+addCommas(addZeroes(value['24h_volume_'+curLow]))),
chalk.green(curSym+addCommas(addZeroes(value['market_cap_'+curLow]))),
chalk.green(`${value.percent_change_1h}%`),
chalk.green(`${value.percent_change_24h}%`),
chalk.green(`${value.percent_change_7d}%`),
chalk.green(timeSince(new Date(value.last_updated * 1000)) + ' ago'),
]);
var totalValue = Number(Math.round(value['price_'+curLow] * portfolio[value.id]));
var coinName = value.id;
barData[coinName] = totalValue;
portfolioTotal += totalValue;
}
});
barGraph(barData, portfolioTotal);
console.log('\n'+table.toString());
console.log(chalk.underline.blue(`Portfolio Total: ${curSym}${portfolioTotal}`));
console.log(' ');
});
/**
* Figlet console log
*/
function figletLog(text) {
figlet(text, function(err, data) {
if (err) {
console.log('Something went wrong...');
console.dir(err);
return;
}
console.log(data)
});
}
/**
* Bar Graphs For Coins
*/
function barGraph(barData, total) {
Object.keys(barData).forEach(function(key) {
var label = `${key} ${curSym}${barData[key]}`;
var graph = new Barcli({
label: label,
range: [0, 100],
});
var percent = Math.round((barData[key] / total) * 100);
graph.update(percent);
});
}
/**
* Add zero if number only has one zero
* Example: $666,888.0 >> $666,888.00
* Fixes coinmarketcap API issues for market caps
* https://stackoverflow.com/a/24039448
*/
function addZeroes(num) {
if (!num)
return '?';
var value = Number(num);
var res = num.split(".");
if(num.indexOf('.') === -1) {
value = value.toFixed(2);
num = value.toString();
} else if (res[1].length < 3) {
value = value.toFixed(2);
num = value.toString();
}
return num
}
/**
* Comma seperate big numbers
* Took multiple answers
* from https://stackoverflow.com/questions/1990512/add-comma-to-numbers-every-three-digits/
* This work with small coins like dogecoin and does not comma seperate AFTER decimals
*/
function addCommas(nStr){
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
};
/**
* Pretty time format X ago function
* https://stackoverflow.com/a/3177838
*/
function timeSince(date) {
var seconds = Math.floor((new Date() - date) / 1000);
var interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + " years";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + " months";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + " days";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + " hours";
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + " minutes";
}
return Math.floor(seconds) + " seconds";
}