summaryrefslogtreecommitdiff
path: root/xe
diff options
context:
space:
mode:
authorAnton Bobov <abobov@gmail.com>2017-11-06 12:39:55 +0500
committerAnton Bobov <abobov@gmail.com>2017-11-15 16:32:12 +0500
commit3381572cd3f1c6f628e17de824267505135a3401 (patch)
treec3c15461ed5c4620812cc05aeadefcbc6e377db9 /xe
parent1f44d74c080c168ccdb315f18a99f270f09c548d (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.
Diffstat (limited to 'xe')
-rwxr-xr-xxe30
1 files changed, 30 insertions, 0 deletions
diff --git a/xe b/xe
new file mode 100755
index 0000000..7612164
--- /dev/null
+++ b/xe
@@ -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