3 ms·
The rate.sx site is pretty nifty. It provides a simple REST endpoint that server cryptocurrency exchange rate info. The nifty part is that it's built to be used
by xelxebar 6y ago
The rate.sx site is pretty nifty. It provides a simple REST endpoint that server cryptocurrency exchange rate info. The nifty part is that it's built to be used on the command line!
$ curl rate.sx # outputs a nifty table of coins and their market rates
$ curl rate.sx/:help # to see how to use the api
I whipped up a shell function wrapper that makes this feel more like a standard cli command. In the off chance anyone is interested, I'm sharing it below (unfortunately, HN turns my tabs into spaces, so indentation is now a single space.)
Note that I was careful to not let it clutter up your environment, so feel free to include it in your .profile with impugnity. Also, this sort of exhibits my current experiments with terse shell style, so don't hate on me for that, but otherwise the function should provide all the typical ergonomics you expect of a standard cli utility.
ratesx() (
set -o errexit -o nounset -o noclobber
RATESX_DOMAIN=${RATESX_DOMAIN:-rate.sx}
RATESX_CURRENCY=${RATESX_CURRENCY:-USD}
RATESX_NROWS=${RATESX_NROWS:-10}
RATESX_FETCH_CMD=${RATESX_FETCH_CMD:-curl --silent}
usage=$(cat <<-USAGE
usage: ratesx [<options>] [<command> <arguments>]
Show information about cryptocurrencies. Command defaults to 'table'.
Commands
graph <coin> [<interval>]
Display exchange rate graph for <coin> over <interval> (defaults to
\$RATESX_CURRENCY hours)
value <amount> <coin> [<amount> <coin>] ..]
Convert sum of <amount>*<coin> to <currency> (defaults to
\$RATESX_NROWS).
table Display table of top coins by market capitalization
coins Show list of supported cryptocurrency coins
currencies Show list of supported currencies
info Show upstream API info
Options
-q Quiet mode. Hide header and footer
-F Hide "Follow" line
-T Disable ANSI color
-n <nrows> Number of currencies (rows) to display in output table
-c <currency> Base currency for exchange rate calculations
-d <domain> Use API endpoint located at <domain>
Variables
RATESX_DOMAIN=$RATESX_DOMAIN
Default domain name of API endpoint
RATESX_NROWS=$RATESX_NROWS
Default count of rows to display in output table
RATESX_CURRENCY=$RATESX_CURRENCY
Default base currency for unit conversions
RATESX_FETCH_CMD=$RATESX_FETCH_CMD
Default command with which to submit API requests
USAGE
)
fetch() { $RATESX_FETCH_CMD "$@"; }
table() { fetch "$1/${2:+?$2}"; }
graph() { fetch "$1/$4${5:+@$5}${2:+?$2}"; }
value() { u=$1; o=$2; c=$3; shift 3; p=0
for v in "$@"; do
p=$((p^1)); [ $p = 1 ] && r="${r:+$r+}"; r="${r:-}$v"
done
printf '%s %s\n' "$(fetch "$u/$r${o:+?$o}")" "$c"
}
info() { fetch "$1/:help"; }
currencies() { fetch "$1/:currencies"; }
coins() { fetch "$1/:coins"; }
while getopts ':qFTn:c:d:' opt "$@"; do
case "$opt" in
q) quiet=q;; F) follow=F;; T) ansi=T;;
n) nrows=$OPTARG;; c) currency=$OPTARG;; d) url=$OPTARG;;
:) error "Need argument: -$OPTARG" "$usage";;
*) error "Unknown option: -$OPTARG" "$usage";;
esac
done; shift $((OPTIND - 1))
nrows=${nrows:-$RATESX_NROWS}; currency=${currency:-$RATESX_CURRENCY};
url=${url:-$RATESX_DOMAIN}; cmd=${1:-table}; shift || :;
url="https://$currency${currency:+.}$url"
opts="${quiet:-}${follow:-}${ansi:-}"
opts="${opts:+$opts&}${nrows:+n=$nrows}"
"$cmd" "$url" "$opts" "$currency" "$@"
)