2020-05-26 00:23:06 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# Usage:
|
2023-04-20 12:48:34 +00:00
|
|
|
# price <currency-base currency> <name of currency> <icon> <signal>
|
|
|
|
# price bat-btc "Basic Attention Token" 🦁 24
|
|
|
|
# This will give the price of BAT denominated in BTC and will update on
|
|
|
|
# signal 24.
|
2020-05-26 00:23:06 +00:00
|
|
|
# When the name of the currency is multi-word, put it in quotes.
|
|
|
|
|
2023-04-20 12:48:34 +00:00
|
|
|
[ -z "$1" ] && exit 1
|
2021-08-18 12:35:04 +00:00
|
|
|
|
2023-03-20 13:50:18 +00:00
|
|
|
url="${CRYPTOURL:-rate.sx}"
|
2023-04-20 12:48:34 +00:00
|
|
|
target="${1%%-*}"
|
|
|
|
denom="${1##*-}"
|
|
|
|
name="${2:-$1}"
|
|
|
|
icon="${3:-💰}"
|
|
|
|
case "$denom" in
|
|
|
|
"$target"|usd) denom="usd"; symb="$" ;;
|
|
|
|
gbp) symb="£" ;;
|
|
|
|
eur) symb="€" ;;
|
|
|
|
btc) symb="" ;;
|
|
|
|
esac
|
2020-06-06 23:59:43 +00:00
|
|
|
interval="@14d" # History contained in chart preceded by '@' (7d = 7 days)
|
2023-03-20 13:50:18 +00:00
|
|
|
dir="${XDG_CACHE_HOME:-$HOME/.cache}/crypto-prices"
|
2023-04-20 12:48:34 +00:00
|
|
|
pricefile="$dir/$target-$denom"
|
|
|
|
chartfile="$dir/$target-$denom-chart"
|
2023-03-20 13:50:18 +00:00
|
|
|
filestat="$(stat -c %x "$pricefile" 2>/dev/null)"
|
2020-06-06 23:59:43 +00:00
|
|
|
|
2020-05-26 00:28:19 +00:00
|
|
|
[ -d "$dir" ] || mkdir -p "$dir"
|
|
|
|
|
2023-04-20 12:48:34 +00:00
|
|
|
updateprice() { curl -sf -m 1 --fail-early $denom.$url/{1$target,$target$interval} --output "$pricefile" --output "$chartfile" ||
|
2023-03-20 13:50:18 +00:00
|
|
|
rm -f "$pricefile" "$chartfile" ;}
|
|
|
|
|
|
|
|
[ "${filestat%% *}" != "$(date '+%Y-%m-%d')" ] &&
|
2023-03-31 13:44:59 +00:00
|
|
|
updateme="1"
|
2020-05-26 00:23:06 +00:00
|
|
|
|
|
|
|
case $BLOCK_BUTTON in
|
2021-03-10 12:19:47 +00:00
|
|
|
1) setsid "$TERMINAL" -e less -Srf "$chartfile" ;;
|
2023-04-20 12:48:34 +00:00
|
|
|
2) notify-send -u low "$icon Updating..." "Updating $name price..." ; updateme="1" ; showupdate="1" ;;
|
2023-03-20 13:50:18 +00:00
|
|
|
3) uptime="$(date -d "$filestat" '+%D at %T' | sed "s|$(date '+%D')|Today|")"
|
2023-04-20 12:48:34 +00:00
|
|
|
notify-send "$icon $name module" "\- <b>Exact price: \$$(cat "$pricefile")</b>
|
2020-05-26 00:23:06 +00:00
|
|
|
- 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" ] &&
|
2023-04-20 12:48:34 +00:00
|
|
|
updateprice "$target" &&
|
2023-03-31 13:44:59 +00:00
|
|
|
[ -n "$showupdate" ] &&
|
2023-04-20 12:48:34 +00:00
|
|
|
notify-send "$icon Update complete." "$name price is now
|
2023-03-31 13:44:59 +00:00
|
|
|
\$$(cat "$pricefile")"
|
|
|
|
|
2023-04-20 12:48:34 +00:00
|
|
|
[ -f "$pricefile" ] && printf "%s%s%0.2f" "$icon" "$symb" "$(cat "$pricefile")"
|