summaryrefslogtreecommitdiff
path: root/local/bin/grab-spot
blob: 989d03722535963f07e34476df9e4eeffaf4d81d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/sh

# Grabs a spot price for a currency and stores it in ~/.cache
# Useful to run as a cron job and use its cached output in motds or status
# lines
#
# syntax: grab-spot TO FROM
# for example, to get the value of USD in NZD:
#     grab-spot USD NZD
# would place a file at ~/.cache/USDNZD containing the value of the USD in NZD,
# to 4 decimal places

if [ -z $1 ]; then
	logger "$0: blank FROM currency"
	exit
fi
if [ -z $2 ]; then
	logger "$0: blank TO currency"
	exit
fi

# Only bother downloading if it's more than 3 hours old
if ([ ! -f ~/.cache/"$1$2" ] || [ $(find ~/.cache/"$1$2" -mmin +60) ]) ; then
	rate=$(curl "http://rate-exchange-1.appspot.com/currency?from=$1&to=$2" | jshon -e rate)
	if [ $? -ne 0 ] ; then
		logger "$0: curl error"
		exit
	fi
	printf "$1$2: %.4f" $rate > ~/.cache/"$1$2"
fi