diff options
author | 2022-08-25 23:20:45 +0100 | |
---|---|---|
committer | 2022-08-25 23:20:45 +0100 | |
commit | c44179966af4714e27f9fe113d48d925def4c2ca (patch) | |
tree | 248138401d89e4a730924ea76fde476289bdd663 /crypto.js | |
download | crypto_list_cli-master.tar.gz crypto_list_cli-master.tar.bz2 crypto_list_cli-master.zip |
Diffstat (limited to 'crypto.js')
-rw-r--r-- | crypto.js | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/crypto.js b/crypto.js new file mode 100644 index 0000000..0fac36b --- /dev/null +++ b/crypto.js @@ -0,0 +1,54 @@ +const https = require('https') +const URL = 'https://www.binance.com/bapi/composite/v1/public/marketing/symbol/list' + +const roundSmall = (n) => { + const log10 = n ? Math.floor(Math.log10(n)) : 0, div = log10 < 0 ? Math.pow(10, 1 - log10) : 100; + return Math.round(n * div) / div; +} + +const formatResponse = token => ({ + 'Rank': token.rank, + 'Name': token.name, + 'Full name': token.fullName, + 'Price': '€'+(token.price<0.1?roundSmall(token.price):token.price.toFixed(2)), + 'Last 24h': token.dayChange.toFixed(2)+'%', + 'Market Cap': '€'+Math.round(token.marketCap), + 'Circulating': token.totalSupply ? Math.round(token.circulatingSupply/token.totalSupply*100)+'%' : '-', + 'Total': token.maxSupply ? Math.round(token.totalSupply/token.maxSupply*100)+'%' : '-' +}) + +const getResponse = (url) => https.get(url, res => { + let response = '' + res.on('data', data => { response += data }) + res.on('end', () => { handleResponse(response) }) + res.on('error', err => { console.error(err) }) +}) + +const handleResponse = (response) => { + const list = JSON.parse(response) + const processed = list.data + .filter(token => token.rank && token.rank <= 40) + .map(formatResponse) + + render(processed) +} + +const render = (processed) => { + const paddings = Object.keys(processed[0]).map(key => { + const longest = processed.sort((a,b) => b[key].length - a[key].length)[0][key] + return longest.length > key.length ? longest.length : key.length + }) + const header = '| '+Object.keys(processed[0]).map((el, i) => el.padStart(paddings[i])).join(' | ')+' |' + const line = '+'+Array(header.length-1).join('-')+'+' + + console.log(line+'\n\x1b[1m'+header+'\x1b[0m\n'+line) + processed + .sort((a, b) => a.Rank - b.Rank) + .forEach(row => { + const color = `\x1b[${Number(row['Last 24h'].slice(0, -1))<0?31:32}m` + console.log('| '+color+Object.values(row).map((el, i) => el.toString().padStart(paddings[i])).join('\x1b[0m | '+color)+'\x1b[0m |') + }) + console.log(line) +} + +getResponse(URL) |