Forked from ionvv/gist:5749c2105459c47d8fe527f953dd3924
Created
April 22, 2021 06:42
-
-
Save croogie/75ede7415320ce30a831031b89d85a1c to your computer and use it in GitHub Desktop.
BitBar plugin - Crypto stats (CoinGecko API)
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
#!/usr/local/bin/node | |
/* | |
* <bitbar.title>Elrond Stats</bitbar.title> | |
* <bitbar.version>v1.0</bitbar.version> | |
* <bitbar.author>Ion Vrinceanu</bitbar.author> | |
* <bitbar.author.github>ionvv</bitbar.author.github> | |
* <bitbar.desc>Elrond stats</bitbar.desc> | |
* <bitbar.dependencies>node</bitbar.dependencies> | |
*/ | |
// START HERE. EDIT ONLY 2 ROWS BELOW: | |
let my_currency = "usd"; // Choose one from the available_currencies list below. For example: usd | |
let my_coin = "egld"; // Choose one from the available_coins list below. For example: egld | |
// STOP HERE. DO NOT EDIT BELOW! | |
let available_coins = { | |
"eth": "ethereum", | |
"btc": "bitcoin", | |
"egld": "elrond-erd-2", | |
"dot": "polkadot", | |
"xrp": "ripple", | |
"ada": "cardano", | |
"link": "chainlink", | |
"ltc": "litecoin", | |
"bch": "bitcoin-cash", | |
"bnb": "binancecoin", | |
"xlm": "stellar", | |
"uni": "uniswap", | |
"aave": "aave", | |
"eos": "eos", | |
"xmr": "monero", | |
"snx": "havven", | |
"doge": "dogecoin", | |
"xtz": "tezos", | |
"trx": "tron", | |
"xem": "nem", | |
"theta": "theta-token", | |
"atom": "cosmos", | |
"cel": "celsius-degree-token", | |
"vet": "vechain", | |
"ceth": "compound-ether", | |
"cro": "crypto-com-chain", | |
"okb": "okb", | |
"leo": "leo-token", | |
"mkr": "maker", | |
} | |
let available_currencies = { | |
"aud": { | |
"symbol": "A$", | |
"front": true | |
}, // Australian Dollar | |
"btc": { | |
"symbol": "BTC", | |
"front": false | |
}, // Bitcoin | |
"cad": { | |
"symbol": "C$", | |
"front": true | |
}, // Canadian Dollar | |
"dot": { | |
"symbol": "DOT", | |
"front": false | |
}, // Polkadot | |
"eos": { | |
"symbol": "EOS", | |
"front": false | |
}, // EOS | |
"eth": { | |
"symbol": "ETH", | |
"front": false | |
}, // Ethereum | |
"eur": { | |
"symbol": "€", | |
"front": true | |
}, // Euro | |
"gbp": { | |
"symbol": "£", | |
"front": true | |
}, // Pound sterling | |
"link": { | |
"symbol": "LINK", | |
"front": false | |
}, // Chainlink | |
"usd": { | |
"symbol": "$", | |
"front": true | |
}, // USD | |
} | |
if ( ! available_coins[ my_coin ] ) { | |
console.log( 'Invalid coin' ); | |
return; | |
} | |
let coin = available_coins[ my_coin ]; | |
let options = { | |
hostname: 'api.coingecko.com', | |
path: '/api/v3/coins/' + coin, | |
method: 'GET' | |
}; | |
let https = require('https'); | |
let displayInMenuBar = ' | color=black dropdown=true'; | |
function getCoinData(){ | |
let output = ''; | |
let req = https.request(options, function(res) { | |
let body = ''; | |
res.on('data', function(chunk) { | |
body += chunk; | |
}); | |
res.on('end', function() { | |
let response = JSON.parse(body); | |
let currency = available_currencies[my_currency]; | |
let current_price = response.market_data.current_price[my_currency]; | |
let name = response.name; | |
displayInMenuBar = name + ' ' + formatValue( current_price ); | |
console.log(displayInMenuBar); | |
console.log('---'); | |
console.log('Price Change (%) 1h: ' + response.market_data.price_change_percentage_1h_in_currency[my_currency] ); | |
console.log('Price Change (%) 24h: ' + response.market_data.price_change_percentage_24h ); | |
console.log('Low 24h: ' + formatValue( response.market_data.low_24h[my_currency] ) ); | |
console.log('High 24h: ' + formatValue( response.market_data.high_24h[my_currency] ) ); | |
console.log('Price Change 24h: ' + formatValue( response.market_data.price_change_24h_in_currency[my_currency] ) ); | |
console.log('Market Cap Rank: ' + response.market_cap_rank); | |
}); | |
}); | |
req.end(); | |
req.on('error', function(e) { | |
console.error(e); | |
}); | |
} | |
function formatValue( value ) { | |
let currency = available_currencies[my_currency]; | |
if ( true === currency.front ) { | |
return currency.symbol + value; | |
} | |
return value + currency.symbol; | |
} | |
getCoinData(options); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment