voidrice/.local/bin/statusbar/sb-price

54 lines
1.6 KiB
Text
Raw Normal View History

#!/bin/sh
# Usage:
# price <url> <Name of currency> <icon> <Price to show in>
# price bat "Basic Attention Token" 🦁
# When the name of the currency is multi-word, put it in quotes.
[ -z "$3" ] && exit 1
# use $4 as currency, if not passed in use "usd" as default
url="${CRYPTOURL:-rate.sx}"
currency="${4:-usd}"
2020-06-06 23:59:43 +00:00
interval="@14d" # History contained in chart preceded by '@' (7d = 7 days)
dir="${XDG_CACHE_HOME:-$HOME/.cache}/crypto-prices"
2021-09-24 13:50:24 +00:00
pricefile="$dir/$1-$currency"
chartfile="$dir/$1-$currency-chart"
filestat="$(stat -c %x "$pricefile" 2>/dev/null)"
2020-06-06 23:59:43 +00:00
[ -d "$dir" ] || mkdir -p "$dir"
2023-03-31 13:44:59 +00:00
updateprice() { curl -sf -m 1 --fail-early $currency.$url/{1$1,$1$interval} --output "$pricefile" --output "$chartfile" ||
rm -f "$pricefile" "$chartfile" ;}
[ "${filestat%% *}" != "$(date '+%Y-%m-%d')" ] &&
2023-03-31 13:44:59 +00:00
updateme="1"
case $BLOCK_BUTTON in
1) setsid "$TERMINAL" -e less -Srf "$chartfile" ;;
2023-03-31 13:44:59 +00:00
2) notify-send -u low "$3 Updating..." "Updating $2 price..." ; updateme="1" ; showupdate="1" ;;
3) uptime="$(date -d "$filestat" '+%D at %T' | sed "s|$(date '+%D')|Today|")"
2020-05-26 01:29:12 +00:00
notify-send "$3 $2 module" "\- <b>Exact price: \$$(cat "$pricefile")</b>
- Left click for chart of changes.
- Middle click to update.
- Shows 🔃 if updating prices.
- <b>Last updated:
$uptime</b>" ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
2020-06-06 23:59:43 +00:00
2023-03-31 13:44:59 +00:00
[ -n "$updateme" ] &&
updateprice "$1" &&
[ -n "$showupdate" ] &&
notify-send "$3 Update complete." "$2 price is now
\$$(cat "$pricefile")"
2021-09-24 13:50:24 +00:00
case "$currency" in
usd) symb="$" ;;
gbp) symb="£" ;;
eur) symb="€" ;;
btc) symb="" ;;
2021-09-24 13:50:24 +00:00
esac
[ -f "$pricefile" ] && printf "$3$symb%0.2f" "$(cat "$pricefile")"