Last active
January 27, 2021 23:03
-
-
Save renie/d51158272f017966cad3f7b7021b5803 to your computer and use it in GitHub Desktop.
Simple script to get last prices for a list of currencies on CoinMarketCap.com. Export your API KEY as COINMARKETCAP_APIKEY variable to your environment. Fiat and crypto lists can be changed in the beginning of the file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https') | |
// ====================================== | |
// ============ SETTINGS ================ | |
// ====================================== | |
const fiatList = ['USD', 'BRL'] | |
const cryptoList = ['DASH', 'LTC', 'ETH', 'BTC'] | |
const url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest' | |
const options = { | |
'headers': { | |
'Accept': 'application/json', | |
'X-CMC_PRO_API_KEY': process.env.COINMARKETCAP_APIKEY | |
} | |
} | |
// ====================================== | |
// ====================================== | |
const buildRow = (data, currency, fiat) => | |
`${currency.padEnd(6, ' ')} |`+ | |
` $ ${data[currency].quote[fiat].price.toFixed(3).padEnd(10, ' ')} | ` + | |
`${data[currency].quote[fiat].percent_change_1h.toFixed(3).padEnd(9, ' ')}% | ` + | |
`${data[currency].quote[fiat].percent_change_24h.toFixed(3).padEnd(9, ' ')}% | ` + | |
`${data[currency].quote[fiat].percent_change_7d.toFixed(3).padEnd(9, ' ')}% | ` + | |
`${data[currency].quote[fiat].percent_change_30d.toFixed(3).padEnd(9, ' ')}% ` | |
const buildRows = (data, fiat) => | |
Object | |
.keys(data) | |
.map(currency => buildRow(data, currency, fiat)) | |
const buildTable = (data, fiat) => | |
console.log( | |
`================================== ${fiat} PRICES ==================================\n` + | |
'SYMBOL | PRICE | CHANGE(1H) | CHANGE(1D) | CHANGE(1W) | CHANGE(1M)\n' + | |
'------------------------------------------------------------------------------------\n' + | |
buildRows(data, fiat).join('\n') + | |
'\n====================================================================================\n' | |
) | |
const requestCallback = (response, fiat) => { | |
let data = '' | |
response.on('data', chunk => data += chunk) | |
response.on('end', () => buildTable(JSON.parse(data).data, fiat)) | |
} | |
const loadData = (fiat, customURL) => | |
https | |
.get(customURL, options, response => requestCallback(response, fiat)) | |
.on('error', err => console.log(`Error: ${err.message}`)) | |
const createURL = fiat => `${url}?symbol=${cryptoList.join(',')}&convert=${fiat}` | |
// ====================================== | |
// ========= Start running here ========= | |
// ====================================== | |
console.log('\n================================== Cryptos Ticker ==================================\n') | |
fiatList.forEach(fiat => loadData(fiat, createURL(fiat))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple script to get last prices for a list of currencies on CoinMarketCap.com. Export your API KEY as COINMARKETCAP_APIKEY variable to your environment. Fiat and crypto lists can be changed in the beginning of the file.