diff options
| author | Anton Bobov <abobov@gmail.com> | 2017-11-06 12:39:55 +0500 |
|---|---|---|
| committer | Anton Bobov <abobov@gmail.com> | 2017-11-15 16:32:12 +0500 |
| commit | 3381572cd3f1c6f628e17de824267505135a3401 (patch) | |
| tree | c3c15461ed5c4620812cc05aeadefcbc6e377db9 | |
| parent | 1f44d74c080c168ccdb315f18a99f270f09c548d (diff) | |
Exchange rate query script, ledger price db update config.
Move XE function out from the ledger-price-db-update and introduce
ledger-commodities configuration file.
| -rwxr-xr-x | ledger-price-db-update.sh | 18 | ||||
| -rwxr-xr-x | xe | 30 |
2 files changed, 40 insertions, 8 deletions
diff --git a/ledger-price-db-update.sh b/ledger-price-db-update.sh index 478efb9..46d7bb5 100755 --- a/ledger-price-db-update.sh +++ b/ledger-price-db-update.sh @@ -2,9 +2,15 @@ # # Get currency exchanges rate and update a ledger price file. # +# Read commodities from file in format: COMMODITY SYMBOL (ex.: EURUSD $) set -e +LEDGER_COMMODITIES="$HOME/.ledger-commodities" +if [ ! -f "$LEDGER_COMMODITIES" ] ; then + echo "No ledger commodities files: $LEDGER_COMMODITIES" + exit 1 +fi LEDGER="$HOME/.ledgerrc" if [[ ! -f "$LEDGER" ]] ; then echo "No ledger configuration file: $LEDGER" >&2 @@ -13,22 +19,18 @@ fi PRICE_DB_FILE=$(sed -nE "s/^--price-db (.*)/\1/p" "$LEDGER") PRICE_DB_FILE="${PRICE_DB_FILE/#\~/$HOME}" -xe() { - curl -s "http://download.finance.yahoo.com/d/quotes.csv?s=$1&f=p" -} - print_rate() { currency="$1" symbol="$2" val=$(xe "$currency=X") - test -n "$val" && echo P $(date +"%Y/%m/%d %H:%M:%S") $symbol $val R + [[ $val =~ ^[0-9]+\.[0-9]+$ ]] && echo P $(date +"%Y/%m/%d %H:%M:%S") $symbol $val R } { - print_rate "USDRUB" "\$" - print_rate "EURRUB" "€" - print_rate "PLNRUB" "P" + cat "$LEDGER_COMMODITIES" | while read commodity symbol ; do + print_rate "$commodity" "$symbol" + done } >> "$PRICE_DB_FILE" @@ -0,0 +1,30 @@ +#!/bin/bash + +set -e + +if [ $# = 0 ] ; then + exit 0 +fi + +from=${1:0:3} +to=${1:3:3} + +get_value() { + curl -s "http://www.xe.com/currencyconverter/convert/?Amount=1&From=$from&To=$to" | \ + grep -o "<span class='uccResultAmount'>[^<]\+" | \ + grep -o '[0-9\.]\+' +} + +get_value_at_date() { + date=$(date -d "$1" +%F) + curl -s "http://www.xe.com/currencytables/?from=$from&date=$date" | \ + xmllint --nonet --html --xpath "//table[@id='historicalRateTbl']/tbody/tr[td/a/text() = '${to^^}']/td[3]/text()" - 2>/dev/null +} + +if [ -z "$2" ] ; then + val=$(get_value) +else + val=$(get_value_at_date "$2") +fi + +printf '%.4f\n' $val |
