Last active
August 29, 2015 14:16
-
-
Save prurigro/9d968cff029234b74f2a to your computer and use it in GitHub Desktop.
Bitcoin price grabber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# bitprice - bitcoin price grabber | |
# | |
# Version 1.2 | |
# | |
# By Kevin MacMartin (prurigro@gmail.com) | |
# Released under the MIT license | |
# | |
script_name="${0//*\/}" | |
outdir='/var/lib/bitprice' | |
exchange_url='https://www.cavirtex.com' | |
currency='C$' | |
# colours | |
[[ -t 1 ]] && { | |
c_d=$'\e[1;30m' # DARK GREY | |
c_r=$'\e[1;31m' # RED | |
c_g=$'\e[1;32m' # GREEN | |
c_y=$'\e[1;33m' # YELLOW | |
c_b=$'\e[1;34m' # BLUE | |
c_m=$'\e[1;35m' # VIOLET | |
c_t=$'\e[1;36m' # TEAL | |
c_w=$'\e[1;37m' # WHITE | |
c_c=$'\e[0m' # DISABLES COLOUR | |
} | |
# output an error and exit with a failure code | |
function error() { | |
printf "%s\n" "${c_r}ERROR: $c_w$1$c_c" >&2 | |
exit 1 | |
} | |
# display usage information and available commands | |
function help(){ | |
printf '\n%s\n' "${c_b}USAGE$c_c" | |
printf ' %s\n\n' "$c_y$script_name$c_c $c_d[${c_r}OPTION$c_d]$c_c" | |
printf '%s\n' "${c_b}OPTIONS$c_c" | |
printf ' %-34s %s\n' "${c_w}h$c_d|${c_w}high$c_c" "${c_w}display the last recorded high price$c_c" | |
printf ' %-34s %s\n' "${c_w}l$c_d|${c_w}low$c_c" "${c_w}display the last recorded low price$c_c" | |
printf ' %-32s %s\n' "${c_w}a$c_d|${c_w}average$c_c" "${c_w}display the last recorded average price$c_c" | |
printf ' %-34s %s\n' "${c_w}u$c_d|${c_w}update$c_c" "${c_w}update the recorded prices$c_c" | |
printf ' %-34s %s\n\n' "${c_w}h$c_d|${c_w}help$c_c" "${c_w}display this help text$c_c" | |
exit | |
} | |
# fetch the given price | |
function fetch_price() { | |
local match='' domatch=0 | |
while read -r; do | |
(( domatch == 1 )) && { | |
[[ "$REPLY" =~ [^\<]*\<[^\>]*\>[^\&]* && "${BASH_REMATCH[0]}" =~ [0-9][^\>]*$ ]] && { | |
match="${BASH_REMATCH[0]/\.*}" | |
break | |
} | |
domatch=0 | |
} | |
[[ "$REPLY" =~ $1 ]] && domatch=1 | |
done < <(wget -q -O - "$exchange_url") | |
[[ -z "$match" ]] \ | |
&& error "unable to fetch the $1 price from: $exchange_url" | |
printf '%s\n' "$match" | |
} | |
# update the price | |
function update_prices() { | |
[[ $(<"$outdir/.auto-update") = 1 ]] \ | |
|| exit 0 | |
# Fetch High and Low prices | |
high="$(fetch_price High)" | |
low="$(fetch_price Low)" | |
[[ -z "$high" || -z "$low" ]] \ | |
&& exit 1 | |
# Calculate the Average price and tweak formatting | |
average=$(( (( high + low )) / 2 )) | |
[[ -z "$average" ]] \ | |
&& error "unable to process data from: $exchange_url" | |
# Write updated prices | |
printf '%s\n' "$high.00" > "$outdir/high" | |
printf '%s\n' "$low.00" > "$outdir/low" | |
printf '%s\n' "$average.00" > "$outdir/average" | |
printf '%s\n' "$(date '+%Y-%m-%d %R')" > "$outdir/.last_update" | |
} | |
# returns the high, low and average price respectively | |
function get_high() { printf '%s\n' "$(<"$outdir/high")"; } | |
function get_low() { printf '%s\n' "$(<"$outdir/low")"; } | |
function get_average() { printf '%s\n' "$(<"$outdir/average")"; } | |
if [[ ! -d "$outdir" ]]; then | |
install -d "$outdir" | |
update_prices | |
elif [[ ! -f "$outdir/high" || ! -f "$outdir/low" || ! -f "$outdir/average" ]]; then | |
[[ ! "$1" = 'update' && ! "$1" = 'u' ]] \ | |
&& update_prices | |
fi | |
if [[ -z "$1" ]]; then | |
printf '%s\n' "฿1 = $currency$(get_high) (high)" | |
printf '%s\n' "฿1 = $currency$(get_low) (low)" | |
printf '%s\n' "฿1 = $currency$(get_average) (average)" | |
printf '\n%s\n' "updated: $(<"$outdir/.last_update")" | |
else | |
case "$1" in | |
high|h) | |
printf '%s\n' "$(get_high)" | |
;; | |
low|l) | |
printf '%s\n' "$(get_low)" | |
;; | |
average|a) | |
printf '%s\n' "$(get_average)" | |
;; | |
update|u) | |
update_prices | |
;; | |
help|h|--help|-h) | |
help | |
;; | |
*) | |
error "$1 is not a valid command" | |
;; | |
esac | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment