commit 733c2ab93d1c02e1161992538c8ad966ef472f37 parent afcb7e9ae3bdb4d64965188fae17a5fd5dfaa9df Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Sun, 5 Jan 2020 11:38:08 +0100 Use curl instead of wget for currency conversion Diffstat:
M | .local/bin/currency | | | 47 | ++++++++++++++--------------------------------- |
1 file changed, 14 insertions(+), 33 deletions(-)
diff --git a/.local/bin/currency b/.local/bin/currency @@ -13,36 +13,17 @@ if [ $# -lt 1 ]; then exit 0 fi -toUpper() { - echo "$@" | tr "[:lower:]" "[:upper:]" -} - -if [ -n "$1" ]; then A=$1; else A=1; fi -if [ -n "$2" ]; then FROM=$(echo "$2" | awk '{print toupper($0)}'); else FROM=USD; fi -if [ -n "$3" ]; then TO=$(echo "$3" | awk '{print toupper($0)}'); else TO=DKK; fi -if [ "$TO" = "$FROM" ]; then echo "Nothing to do!"; exit 2; fi - -#CONVERTER="https://finance.google.com/finance/converter?a=$A&from=$FROM&to=$TO" -CONVERTER="https://free.currencyconverterapi.com/api/v6/convert?q=${FROM}_${TO}&compact=ultra&apiKey=$(pass Online/currencyconverterapi.com-apikey)" - -#echo $CONVERTER - -GETCMD="wget --no-verbose" -#if command -v torsocks >/dev/null 2>/dev/null; then -# GETCMD="torsocks $GETCMD" -#fi - -# google finance -#RESULT=`$GETCMD -O - "$CONVERTER" 2>&1 | \ -# LC_ALL=C sed -n -e 's/.*<span class=bld>\(.*\)<\/span>.*/\1/p'` - -# currencyconverterapi.com -RESULT=$($GETCMD -O - "$CONVERTER" 2>/dev/null | sed 's/.*://' | sed 's/}$//') - -#echo "Result: $A $FROM = $RESULT" - -# Google -#echo $RESULT - -# currencyconverterapi -echo "$A $FROM = $(echo "${RESULT}*${A}" | bc -l) $TO" +if [ -n "$1" ]; then a=$1; else a=1; fi +if [ -n "$2" ]; then from=$(echo "$2" | awk '{print toupper($0)}'); else from=USD; fi +if [ -n "$3" ]; then to=$(echo "$3" | awk '{print toupper($0)}'); else to=DKK; fi +if [ "$to" = "$from" ]; then echo "Nothing to do!"; exit 2; fi + +url="https://free.currencyconverterapi.com/api/v6/convert?q=${from}_${to}&compact=ultra&apiKey=$(pass Online/currencyconverterapi.com-apikey)" +result="$(curl "$url" 2>/dev/null | sed 's/.*://;s/}$//')" + +if [ -n "$result" ]; then + echo "$a $from = $(echo "${result}*${a}" | bc -l) $to" +else + echo "error: could not get conversion" + exit 1 +fi