Skip to content

Instantly share code, notes, and snippets.

@morishin
Last active January 30, 2018 03:35
Show Gist options
  • Save morishin/1d2f6da1ec40347599d6d9bcfdd1e928 to your computer and use it in GitHub Desktop.
Save morishin/1d2f6da1ec40347599d6d9bcfdd1e928 to your computer and use it in GitHub Desktop.
BitBar Plugin - Show Crypt Currency Rates to JPY
#!/usr/bin/env /path/to/the/node/executable
const https = require('https')
const Currency = {
Bitcoin: 'btc',
Ethereum: 'eth',
EthereumClassic: 'etc',
Lisk: 'lsk',
Factom: 'fct',
Monero: 'xmr',
Augur: 'rep',
Ripple: 'xrp',
Zcash: 'zec',
NEM: 'xem',
Litecoin: 'ltc',
Dash: 'dash',
BitcoinCash: 'bch'
}
// Please edit this line yourself
const subscribed = [Currency.Bitcoin, Currency.Ethereum]
function getRate(currency) {
return new Promise((resolve, reject) => {
https.get(`https://coincheck.com/api/rate/${currency}_jpy/`, (response) => {
response.setEncoding('utf8')
response.on('data', (raw) => {
const data = JSON.parse(raw)
const rate = parseFloat(data.rate)
const prettify = (rawRate) => {
if (rawRate < 100) {
return rawRate.toFixed(2)
} else if (1000 <= rawRate) {
return `${(rawRate / 1000).toPrecision(4)}k`
} else {
return rawRate.toFixed()
}
}
resolve(`¥${prettify(rate)}/${currency === 'btc' ? '₿' : currency.toUpperCase()}`)
});
}).on('error', (e) => {
reject(e)
})
})
}
async function showRates(currencies) {
const results = await Promise.all(currencies.map(getRate))
console.log(results.join(' '))
}
showRates(subscribed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment