sb-price (1886B)
1 #!/bin/sh 2 3 # Usage: 4 # price <currency-base currency> <name of currency> <icon> <signal> 5 # price bat-btc "Basic Attention Token" 🦁 24 6 # This will give the price of BAT denominated in BTC and will update on 7 # signal 24. 8 # When the name of the currency is multi-word, put it in quotes. 9 10 [ -z "$1" ] && exit 1 11 12 url="${CRYPTOURL:-rate.sx}" 13 target="${1%%-*}" 14 denom="${1##*-}" 15 name="${2:-$1}" 16 icon="${3:-💰}" 17 case "$denom" in 18 "$target"|usd) denom="usd"; symb="$" ;; 19 gbp) symb="£" ;; 20 eur) symb="€" ;; 21 rub) symb="₽" ;; 22 btc) symb="" ;; 23 esac 24 interval="@14d" # History contained in chart preceded by '@' (7d = 7 days) 25 dir="${XDG_CACHE_HOME:-$HOME/.cache}/crypto-prices" 26 pricefile="$dir/$target-$denom" 27 chartfile="$dir/$target-$denom-chart" 28 filestat="$(stat -c %y "$pricefile" 2>/dev/null)" 29 30 [ -d "$dir" ] || mkdir -p "$dir" 31 32 updateprice() { curl -sf \ 33 --fail-early "${denom}.${url}/1${target}" "${denom}.${url}/${target}${interval}" \ 34 --output "$pricefile" --output "$chartfile" || 35 rm -f "$pricefile" "$chartfile" ;} 36 37 [ "${filestat%% *}" != "$(date '+%Y-%m-%d')" ] && 38 updateme="1" 39 40 case $BLOCK_BUTTON in 41 1) setsid -f "$TERMINAL" -e less -Srf "$chartfile" >/dev/null ;; 42 2) notify-send -u low "$icon Updating..." "Updating $name price..." ; updateme="1" ; showupdate="1" ;; 43 3) uptime="$(date -d "$filestat" '+%D at %T' | sed "s|$(date '+%D')|Today|")" 44 notify-send "$icon $name module" "\- <b>Exact price: $symb$(cat "$pricefile")</b> 45 - Left click for chart of changes. 46 - Middle click to update. 47 - Shows 🔃 if updating prices. 48 - <b>Last updated: 49 $uptime</b>" ;; 50 8) setsid -f "$TERMINAL" -e "$EDITOR" "$0" >/dev/null ;; 51 esac 52 53 [ -n "$updateme" ] && 54 updateprice "$target" && 55 [ -n "$showupdate" ] && 56 notify-send "$icon Update complete." "$name price is now 57 $symb$(cat "$pricefile")" 58 59 [ -f "$pricefile" ] && printf "%s%s%0.2f\n" "$icon" "$symb" "$(cat "$pricefile")"