Browse Source

Switch coinbase to coincap API in btc price tracker

master
Riyyi 4 years ago
parent
commit
84cae6762c
  1. 44
      .scripts/panel/btc.sh

44
.scripts/panel/btc.sh

@ -3,18 +3,20 @@
# Enable mathematics in POSIX shell # Enable mathematics in POSIX shell
calc() { awk "BEGIN { printf(\"%.2f\", $*) }"; } calc() { awk "BEGIN { printf(\"%.2f\", $*) }"; }
output() { data() {
[ -z "$1" ] || [ -z "$2" ] && return 1 [ -z "$1" ] && return 1
url="$1" url="$1"
price="$2" curl --location --request GET --silent "$url"
}
price_old="$(curl -s $url | jq -cr '.data.amount')" output() {
difference=$(calc "$price / $price_old * 100 - 100") [ -z "$1" ] && return 1
# Get symbol and color # Get symbol and color
old_higher=$(calc "$price < $price_old" | cut -c 1) difference="$1"
if [ $old_higher -eq 0 ]; then possitive=$(calc "$difference >= 0" | cut -c 1)
if [ "$possitive" -eq 1 ]; then
symbol="" symbol=""
color="$COLOR2" color="$COLOR2"
else else
@ -27,19 +29,29 @@ output() {
} }
# Get dates # Get dates
date_yesterday="$(date -d 'yesterday' +'%Y-%m-%d')" date_last_week_start="$(date --date 'last week' +%s000)"
date_last_week="$(date -d 'last week' +'%Y-%m-%d')" date_last_week_end=$(calc "$date_last_week_start + (1000 * 60 * 60)") # +1h
date_last_week_end=${date_last_week_end%.00} # cut off ".00" at the end
# Get API URLs # API URLs
url="https://api.coinbase.com/v2/prices/BTC-USD/spot" url="https://api.coincap.io/v2/assets/bitcoin"
url_yesterday="${url}?date=$date_yesterday" url_last_week="${url}/history?interval=h1&start=${date_last_week_start}&end=${date_last_week_end}"
url_last_week="${url}?date=$date_last_week"
# Current price # Current price
price="$(curl -s $url | jq -cr '.data.amount')" data="$(data "$url")"
price="$(echo "$data" | jq --compact-output --raw-output '.data.priceUsd')"
price=$(calc "$price")
# Get yesterdays difference
difference_yesterday="$(echo "$data" | jq --compact-output --raw-output '.data.changePercent24Hr')"
difference_yesterday="$(calc "$difference_yesterday")"
# Get last weeks difference
price_last_week="$(data "$url_last_week" | jq --compact-output --raw-output '.data[0].priceUsd')"
difference_last_week=$(calc "$price / $price_last_week * 100 - 100")
# Create output formatting # Create output formatting
difference_yesterday_output="d $(output $url_yesterday $price)" difference_yesterday_output="d $(output "$difference_yesterday")"
difference_last_week_output="w $(output $url_last_week $price)" difference_last_week_output="w $(output "$difference_last_week")"
echo "\$$price $difference_yesterday_output $difference_last_week_output" echo "\$$price $difference_yesterday_output $difference_last_week_output"

Loading…
Cancel
Save