Created
June 12, 2022 12:44
-
-
Save Summertime/98cf345b278d5cc8a683eeb5a4b388fe to your computer and use it in GitHub Desktop.
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
APPNAME=exchange.sh | |
p () { printf '%s\n' "$@";} | |
: "${XDG_CACHE_HOME:=$HOME/.cache}" | |
cachedget () { | |
URL=$1 | |
CACHEDIR="$XDG_CACHE_HOME/$APPNAME/curl/GET-$(base64 -w0 <<< "$URL")" | |
mkdir --parents "$CACHEDIR" | |
curl -sS \ | |
--etag-save "$CACHEDIR/etag" \ | |
--etag-compare "$CACHEDIR/etag" \ | |
--output "$CACHEDIR/data" \ | |
"$URL" | |
p "$CACHEDIR/data" | |
} | |
DATAFILE=$(cachedget https://api.exchangerate.host/latest) | |
p 'unset _exchange_sh_SYMBOLS' | |
p 'declare -a _exchange_sh_SYMBOLS=(' | |
jq -r '.rates | keys[] | "\t\(.)"' "$DATAFILE" | |
p ')' | |
p 'unset _exchange_sh_RATES' | |
p 'declare -A _exchange_sh_RATES=(' | |
jq -r '.rates | to_entries[] | "\t[\(.key)]=\(.value)"' "$DATAFILE" | |
p ')' | |
# add validation | |
p 'exchange () { set -eu; bc <<< "scale=2; $1 * ${_exchange_sh_RATES[$3]} / ${_exchange_sh_RATES[$2]}";}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment