summaryrefslogtreecommitdiff
path: root/xe
diff options
context:
space:
mode:
authorAnton Bobov <abobov@gmail.com>2018-11-01 22:16:21 +0500
committerAnton Bobov <abobov@gmail.com>2018-11-01 22:16:21 +0500
commit2bf34f4efd5643b847170443ca9aa03dafed976c (patch)
treeed4f121a91f6a5f3214e4cea25c47475abd9e86d /xe
parent88fcd4ab2ea43395da996ccfe067b3b5651eb458 (diff)
Update bin.
Diffstat (limited to 'xe')
-rwxr-xr-xxe39
1 files changed, 23 insertions, 16 deletions
diff --git a/xe b/xe
index 7612164..97dc724 100755
--- a/xe
+++ b/xe
@@ -6,25 +6,32 @@ if [ $# = 0 ] ; then
exit 0
fi
+if ! (hash phantomjs >/dev/null 2>&1) ; then
+ exit 1
+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
+ script=$(tempfile)
+ url="http://www.xe.com/currencyconverter/convert/?Amount=1&From=$from&To=$to"
+ cat <<EOF >$script
+ var page = require('webpage').create();
+page.open('$url', function(status) {
+ if (status !== 'success') {
+ console.log('Unable to access network');
+ } else {
+ var ua = page.evaluate(function() {
+ return document.querySelector('.converterresult-toAmount').textContent;
+ });
+ console.log(ua);
+ }
+ phantom.exit();
+});
+EOF
+ timeout 15s phantomjs $script | head -n 1
+ rm $script
}
-if [ -z "$2" ] ; then
- val=$(get_value)
-else
- val=$(get_value_at_date "$2")
-fi
-
-printf '%.4f\n' $val
+get_value $from $to