Create a gist now

Instantly share code, notes, and snippets.

@ligurio /ccc
Last active Sep 29, 2016

What would you like to do?
#!/bin/sh
# http://www.cbr.ru/scripts/XML_daily.asp
# http://www.xe.com/iso4217.php
# http://www.google.com/ig/calculator?hl=en&q=100EUR=?USD
# http://www.gummy-stuff.org/Yahoo-data.htm
# http://finance.yahoo.com/currency-converter/#from=USD;to=EUR;amt=1
# http://www.xe.com/iso4217.php
# http://alfabank.ru/_/rss/_currency.html
# http://wiki.github.com/jwiegley/ledger/multiple-currencies
#DBFILE=$HOME/tmp/currencies.txt
DBFILE=`mktemp -t currencies.XXXXX`
URL=http://www.cbr.ru/scripts/XML_daily.asp
FETCH=`whereis curl`
DC=`whereis dc`
get_currency_data() {
grep ^$1 $DBFILE | cut -d ' ' -f 2,3
}
fetch_database_file() {
TMPFILE=`mktemp -t ccc.XXXXX`
if $FETCH -s $URL > $TMPFILE; then
awk -F"[<>]" '/<CharCode>([A-Z]+)<\/CharCode>/ { charcode=$3 } /<Nominal>([0-9]+)<\/Nominal>/ { nominal=$3 } /<Value>([0-9,]+)<\/Value>/ { sub(/\,/,".",$3); value=$3 } /<\/Valute>/ { print charcode,nominal,value }' < $TMPFILE > $DBFILE && rm -f $TMPFILE
else
return 1
fi
}
if [ x$1 = x ]; then
echo
echo Currency course converter, uses data from http://cbr.ru/.
echo
echo Usage:
echo " `basename $0` <sum to convert> <currency of the sum> <currency of result>"
echo
echo Simple text database is stored in $DBFILE and is automatically updated
echo from cbr.ru if this file is older than 1 day \(or is retrieved if absent\).
echo
echo Currency names must be international 3 chars currency codes
echo \(e.g. USD for dollars, EUR for euro, RUR for rubble etc.\).
echo Case is not important \("usd", "Usd" and "USD" - all mean the same\).
echo
echo "Supported currencies:"
echo `curl -s http://www.cbr.ru/scripts/XML_daily.asp | grep CharCode | sed "s/^.*\<CharCode\>\\(.*\\)<\/CharCode.*$/\\1/" | tr "\n" " "`
exit
fi
SUM=$1
#FROMC=${(U)2:-RUR}
#TOC=${(U)3:-USD}
FROMC=${2:-RUR}
TOC=${3:-USD}
[ "$FROMC" = "$TOC" ] && exit 1
[ -z "$SUM" ] && exit 1
FDBF=`find $DBFILE -mtime -1 2> /dev/null`
if [ -n "$FDBF" ]; then
#echo -n Updating currency database \($DBFILE\)... >&2
#fetch_database_file && echo OK >&2 || echo ERROR >&2
fetch_database_file || die "Update DB: $!\n";
fi
if [ ! -f $DBFILE ]; then
echo Can not fetch currencies database from cbr.ru! >&2
exit 1
else
touch $DBFILE
fi
if [ "$FROMC" = "RUR" ]; then
CFGC=`get_currency_data $TOC`
OP="*"
elif [ "$TOC" = "RUR" ]; then
CFGC=`get_currency_data $FROMC`
OP="/"
else
CFGC="`get_currency_data $FROMC` / / `get_currency_data $TOC`"
OP="*"
fi
#echo "DEBUG: CFGC - $CFGC SUM - $SUM OP - $OP"
$DC -e "8k $SUM $CFGC / $OP p"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment