|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Trivial temporary workaround to get VTC/USD exchange rate in native Bitpay JSON format for coinpunk. |
| 4 | +# |
| 5 | +# Yes, this is a very ugly and inneficient hack. Feel free to improve or rewrite in vertpunk directly. |
| 6 | +# |
| 7 | +# Add to Cron for the vertpunk user via "crontab -u vertpunk -e" to get new pricing every 15 mins |
| 8 | +# 0,15,30,45 * * * * /path/to/get_vtc_exchange_rate.sh >> /path/to/vtcusd.log 2>&1 |
| 9 | +# |
| 10 | +# |
| 11 | +# Edit your vertpunk config.json and change pricesUrl to http://localhost:8080/rates.json |
| 12 | +# |
| 13 | +#set -x |
| 14 | + |
| 15 | +. /etc/profile |
| 16 | + |
| 17 | +# Where to write stuff |
| 18 | +TMPDIR=/tmp |
| 19 | +OUTPUTFILE=/home/vertpunk/vertpunk/public/rates.json |
| 20 | + |
| 21 | +PID=$$ |
| 22 | + |
| 23 | +# Functions |
| 24 | +clean_up () { |
| 25 | + rm $TMPDIR/*.tmp.$PID |
| 26 | +} |
| 27 | + |
| 28 | +get_btc_rates () { |
| 29 | +echo "Getting BTC Exchange Rates from Bitpay..." |
| 30 | +curl -s -f --retry 5 "https://bitpay.com/api/rates" > $TMPDIR/rates.tmp.$PID |
| 31 | +STATUS=$? |
| 32 | +BTCRATES=`cat $TMPDIR/rates.tmp.$PID` |
| 33 | +} |
| 34 | + |
| 35 | +calc_btc_usd () { |
| 36 | +echo "Isolating BTC Price..." |
| 37 | +BTCUSD="`echo $BTCRATES | awk -F\} '{print $1}' | awk -F: '{print $4}'`" |
| 38 | +BTCUSD=${BTCUSD//[[:space:]]/} |
| 39 | +echo "Current BTC Value is \$$BTCUSD..." |
| 40 | +} |
| 41 | + |
| 42 | +get_vtc_btc () { |
| 43 | +echo "Getting Cryptsy VTC-BTC Exchange Rate..." |
| 44 | +#curl --retry 5 "http://pubapi.cryptsy.com/api.php?method=singlemarketdata\&marketid=151" > $TMPDIR/cryptsy.tmp.$PID |
| 45 | +#cat $TMPDIR/cryptsy.tmp.$PID | awk -F, '{print $4}'|awk -F\" '{print $4}' > $TMPDIR/vtcbtc.tmp.$PID |
| 46 | +# Single Order Data API seems to be marginally more reliable... |
| 47 | +curl -s -f --retry 5 "http://pubapi.cryptsy.com/api.php?method=singleorderdata&marketid=151" > $TMPDIR/cryptsy.tmp.$PID |
| 48 | +cat $TMPDIR/cryptsy.tmp.$PID | awk -F :\" '{print $8}'|cut -b 1-8 > $TMPDIR/vtcbtc.tmp.$PID |
| 49 | +VTCBTC=`cat $TMPDIR/vtcbtc.tmp.$PID` |
| 50 | +VTCBTC=${VTCBTC//[[:space:]]/} |
| 51 | +} |
| 52 | + |
| 53 | +output_vtc_usd () { |
| 54 | +echo "Current VTC exchange rate is $VTCBTC BTC..." |
| 55 | +echo "Converting VTC-BTC to VTC-USD..." |
| 56 | +VTCUSD=$(echo "scale=4; $BTCUSD*$VTCBTC" | bc) |
| 57 | +echo "Current VTC value in USD is \$$VTCUSD" |
| 58 | +echo "Writing VTC/USD rate to local file..." |
| 59 | +echo -n '[{"code":"USD","name":"US Dollar","rate":' > $OUTPUTFILE |
| 60 | +echo -n $VTCUSD >> $OUTPUTFILE |
| 61 | +echo "}]" >> $OUTPUTFILE |
| 62 | +} |
| 63 | + |
| 64 | +# Let's Go |
| 65 | + |
| 66 | +# Get BTC Rates from BitPay |
| 67 | +echo "Started $0 on `date +%c`" |
| 68 | +get_btc_rates |
| 69 | + |
| 70 | +if [[ ! -z "$BTCRATES" ]] ; then |
| 71 | + # Isolate USD value from API output |
| 72 | + calc_btc_usd |
| 73 | + # Get VTC/BTC Rate from Cryptsy |
| 74 | + get_vtc_btc |
| 75 | + if [[ ! -z "$VTCBTC" ]] ; then |
| 76 | + # Calcualte VTC/USD |
| 77 | + output_vtc_usd |
| 78 | + else |
| 79 | + echo "Cryptsy API appears to be down. Curl exited with status $STATUS..." |
| 80 | + clean_up |
| 81 | + echo "Unable to convert, please try again later." |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | +else |
| 85 | + echo "Bitpay API appears to be down. Curl exited with status $STATUS..." |
| 86 | + clean_up |
| 87 | + echo "Unable to convert, please try again later." |
| 88 | + exit 1 |
| 89 | +fi |
| 90 | + |
| 91 | +clean_up |
| 92 | +echo "Finished!" |
| 93 | +exit 0 |
0 commit comments