~mikini/hometools

158a6504d2be767f8ecff7fb28a79aed78c37d9b — Mikkel Kirkgaard Nielsen a month ago da42685 master
Exit on network error & indicate date of rate used

* check for and report wget errors
* parse and report date of ECB rate in used
1 files changed, 19 insertions(+), 2 deletions(-)

M exchange_rates.sh
M exchange_rates.sh => exchange_rates.sh +19 -2
@@ 12,9 12,26 @@ TARGET=${TARGET^^}
# ECB rates available from https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/index.en.html
# As XML http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
RATES=$(wget -q -O- http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml)
RATES=$(echo "$RATES"; echo "<Cube currency='EUR' rate='1'") # fake a EUR entry
RATES_RES=$?
if [ ! $RATES_RES -eq 0 ]; then
    echo -n "Failed retrieving ECB eurofxref: "
    case $RATES_RES in
	1) ERR=Generic ;;
	3) ERR=File ;;
	4) ERR=Network ;;
	5) ERR=SSL/TSL ;;
	8) ERR=Server\ response ;;
    esac
    echo "$ERR error!"
    exit 42
fi

# Parse rates for data needed
RATES=$(echo "$RATES"; echo "<Cube currency='EUR' rate='1' />") # fake a EUR entry
BASEVAL=$(echo "$RATES" |egrep "<Cube currency='$BASE'" |egrep "rate='([[:digit:].]*)'" -o |egrep "[[:digit:].]*" -o)
TARGETVAL=$(echo "$RATES" |egrep "<Cube currency='$TARGET'" |egrep "rate='([[:digit:].]*)'" -o |egrep "[[:digit:].]*" -o)
DATE=$(echo "$RATES" |egrep "<Cube time=" |egrep "time='([[:digit:]-]{10})'" -o |egrep "[[:digit:]-]{10}" -o)
#CURRENCIES=$(echo "$RATES" |egrep "<Cube currency='([[:alpha:]])'" |egrep "rate='([[:digit:].]*)'" -o |egrep "[[:digit:].]*" -o)

if [ -z $BASEVAL ]; then
  echo "Base currency is not listed by ECB: $BASE." 


@@ 33,4 50,4 @@ if [ ! -z $VERBOSE ]; then
  echo "Target: EUR 1 = $TARGET $TARGETVAL"
  echo "$BASE 1 = $TARGET $UNITYVAL"
fi
echo $BASE $AMOUNTB = $TARGET $AMOUNTT @ $UNITYVAL $TARGET/$BASE
echo $BASE $AMOUNTB = $TARGET $AMOUNTT @ $UNITYVAL $TARGET/$BASE \($DATE\)