A small script to check the price of ZEC
authorEinar Jørgen Haraldseid <einar@haraldseid.net>
Mon, 29 May 2017 07:01:47 +0000 (09:01 +0200)
committerEinar Jørgen Haraldseid <einar@haraldseid.net>
Mon, 29 May 2017 07:01:47 +0000 (09:01 +0200)
zec.sh [new file with mode: 0755]

diff --git a/zec.sh b/zec.sh
new file mode 100755 (executable)
index 0000000..75d7c94
--- /dev/null
+++ b/zec.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+LC_NUMERIC="en_US.utf-8"
+CacheFile="/tmp/coinindex.json"
+CacheFile2="/tmp/usd-nok.csv"
+ApiURL="https://www.worldcoinindex.com/apiservice/json?key=iOU8ko9QJ01Qj1JXoXfAXaoLD"
+ApiURL2="https://data.norges-bank.no/api/data/EXR/B.NOK.USD.SP.A?lastNObservations=1&format=csv-:-tab-false-y"
+CacheTime="150" # 150 seconds = 2.5 minutes
+NumberRegex="^[0-9]+([.][0-9]+)?$"
+
+if [ -z ${1+x} ]; then
+  Value=1
+else
+  Value=${1}
+fi
+
+if ! [[ ${Value} =~ ${NumberRegex} ]]; then
+  echo "+++ OUT OF CHEESE ERROR +++"
+  exit 1
+fi
+
+if [ -f ${CacheFile} ]; then
+  if [ $(stat --format=%Y ${CacheFile}) -le $(( $(date +%s) - ${CacheTime} )) ]; then
+    wget -q -O ${CacheFile} ${ApiURL}
+    wget -q -O ${CacheFile2} ${ApiURL2}
+  fi
+else
+  wget -q -O ${CacheFile} ${ApiURL}
+  wget -q -O ${CacheFile2} ${ApiURL2}
+fi
+
+PriceZECinUSD="$(jq '.Markets | .[] | select(.Name=="Zcash") | .Price_usd' ${CacheFile})"
+PriceUSDinNOK="$(tail -n 1 ${CacheFile2} | cut -f 2 | tr -d \")"
+PriceZECinNOK="$(echo "${PriceZECinUSD}*${PriceUSDinNOK}" | bc)"
+
+ValueUSD="$(echo "${PriceZECinUSD}*${Value}" | bc | sed 's/^\./0./')"
+ValueNOK="$(echo "${PriceZECinNOK}*${Value}" | bc | sed 's/^\./0./')"
+ValueUSDPretty="$(printf "%0.4f\n" $ValueUSD)"
+ValueNOKPretty="$(printf "%0.4f\n" $ValueNOK)"
+
+echo "${Value} ZEC = \$ ${ValueUSDPretty} (about NOK ${ValueNOKPretty})"