src

sauce
got clone ssh://anon@src.dalliard.ch/src
log | files | refs

fx (888B)


      1 #!/bin/sh -e
      2 # pkg_add jq
      3 
      4 main() {
      5 	test -n "$1" || usage
      6 	currency="$1"
      7 	amount="${2:-1}"
      8 	case "$currency" in
      9 		(eur|usd) fiat "$@" ;;
     10 		(btc) bitcoin "$@" ;;
     11 		(*) fail 'currency not supported' ;;
     12 	esac
     13 	echo "$result"
     14 }
     15 
     16 fiat() {
     17 	test -z "$3" && date="$(date +%Y-%m-%d)" || date="$3"
     18 	result=$(ftp -MVo - "https://api.frankfurter.app/$date?to=chf&from=$currency&amount=$amount" | jq -r .rates.CHF)
     19 }
     20 
     21 bitcoin() {
     22 	test -z "$3" &&
     23 		price=$(ftp -MVo - "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=chf" | jq -r .bitcoin.chf) ||
     24 		price=$(ftp -MVo - "https://api.coingecko.com/api/v3/coins/bitcoin/history?date=$3&localization=false" | jq -r .market_data.current_price.chf)
     25 	result=$(echo "scale=8; $price * $amount / 100000000" | bc)
     26 }
     27 
     28 fail() { echo "$1"; exit 1; }
     29 usage() { echo "usage: ${0##*/} currency [amount] [dd-mm-yyyy]" >&2; exit 1; }
     30 main "$@"