Last active
May 25, 2022 12:31
-
-
Save knbknb/bfaddb3db3e17e49df4f7908b0df67fe to your computer and use it in GitHub Desktop.
virtual_currencies.sh - bash script to get financial metrics of 10 most popular cryptocurrencies
This file contains hidden or 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
#!/bin/sh | |
# https://stackoverflow.com/questions/48100901/convert-a-string-to-a-number-using-jq | |
#infile=cryptocur_top10.json | |
infile=$(mktemp /tmp/cryptocurr-$(date +"%Y-%m-%d_%T_XXXXXX")).json | |
export COINMARKETCAP_API_KEY=$([ -f ~/.env ] && cat ~/.env | grep COINMARKETCAP_API_KEY | cut -d '=' -f2) | |
# get current top-10 data from coinmarketcap | |
curl -sLo "$infile" \ | |
-H "X-CMC_PRO_API_KEY: $COINMARKETCAP_API_KEY" \ | |
-H "Accept: application/json" \ | |
"https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?convert=EUR&limit=10" \ | |
# jq: parse JSON, | |
# awk: format floats, | |
# xargs: lpad, | |
# perl: add header | |
< "$infile" jq -r '.data[]| [.cmc_rank, .symbol, .quote.EUR.percent_change_1h, .quote.EUR.percent_change_1h, .quote.EUR.percent_change_7d, .quote.EUR.price, .quote.EUR.market_cap] | @tsv' \ | |
| awk '{ $3=sprintf("%.2f",$3); $4=sprintf("%.2f",$4); $5=sprintf("%.2f",$5); $6=sprintf("%.3f",$6); $7=sprintf("%.3f",$7/(1*10^9)) }1' \ | |
| xargs printf '%-2s %-5s %7s %7s %7s %10s %8s\n' \ | |
| perl -pE 'BEGIN {say qq(rk sym pctch_1h pctch_24h pctch_7d price_eur mt_cap_eur);} $_' | |
## optional | |
## grab symbols and currency-names from json file | |
< $infile jq -r '.data[]| [.symbol + " ", .name] | @tsv' | |
exit | |
## same as | |
cat <<END | |
BTC Bitcoin | |
ETH Ethereum | |
USDT Tether | |
BNB BNB | |
USDC USD Coin | |
XRP XRP | |
SOL Solana | |
ADA Cardano | |
LUNA Terra | |
AVAX Avalanche | |
END | |
rm $infile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Creates text output that will look like this: