xe (1388B)
1 #!/bin/sh 2 # A CLI utility for exchange rates via https://www.xe.com 3 checkdeps() { 4 for com in "$@"; do 5 command -v "$com" >/dev/null 2>&1 \ 6 || { printf '%s required but not found.\n' "$com" >&2 && exit 1; } 7 done 8 } 9 checkdeps curl pup tr 10 11 die() { printf '%s\n' "$1" >&2 && exit 1; } 12 13 # if args == amount from to 14 if [ $# -eq 3 ]; then 15 amount="$1" 16 from="$(printf '%s' "$2" | tr '[:lower:]' '[:upper:]')" 17 to="$(printf '%s' "$3" | tr '[:lower:]' '[:upper:]')" 18 # if args == from to, and amount on stdin 19 elif ! [ -t 0 ] && [ $# -eq 2 ]; then 20 read -r amount 21 from="$(printf '%s' "$1" | tr '[:lower:]' '[:upper:]')" 22 to="$(printf '%s' "$2" | tr '[:lower:]' '[:upper:]')" 23 # else usage is incorrect 24 else 25 die 'Usage: xe AMOUNT FROM_CURRENCY TO_CURRENCY' 26 fi 27 28 29 curl -sL "https://www.xe.com/currencyconverter/convert/?Amount=$amount&From=$from&To=$to" \ 30 -H 'authority: www.xe.com' \ 31 -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \ 32 -H 'accept-language: en-US,en;q=0.9,uk-UA;q=0.8,uk;q=0.7,ru-RU;q=0.6,ru;q=0.5' \ 33 -H 'cache-control: no-cache' \ 34 -H 'pragma: no-cache' \ 35 -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36' \ 36 | pup 'p[class*=result__BigRate] text{}' | tr -d '\n'