summaryrefslogtreecommitdiffstats
path: root/.local/bin/statusbar/crypto
diff options
context:
space:
mode:
authorLuke Smith <luke@lukesmith.xyz>2020-04-30 10:24:32 -0400
committerLuke Smith <luke@lukesmith.xyz>2020-04-30 10:24:32 -0400
commit53ef201c08d2180a371b3d9a6c5e35cbe1f88230 (patch)
tree95a30d78c01be4a401702d4a96176014ff0b7053 /.local/bin/statusbar/crypto
parentc83f43bed1a24d46b9f327624bc2b0b783449b44 (diff)
downloadeibhear-53ef201c08d2180a371b3d9a6c5e35cbe1f88230.tar.gz
eibhear-53ef201c08d2180a371b3d9a6c5e35cbe1f88230.tar.zst
eibhear-53ef201c08d2180a371b3d9a6c5e35cbe1f88230.zip
optional cryptocurrencies module
Diffstat (limited to '.local/bin/statusbar/crypto')
-rwxr-xr-x.local/bin/statusbar/crypto54
1 files changed, 54 insertions, 0 deletions
diff --git a/.local/bin/statusbar/crypto b/.local/bin/statusbar/crypto
new file mode 100755
index 0000000..88b2365
--- /dev/null
+++ b/.local/bin/statusbar/crypto
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+# Shows the price for desired cryptocurrencies. Module updates automatically
+# every calendar day, but can also be updated with a middle click.
+
+# Currencies should be ;-separated:
+# human-readable name;urlname;icon
+coins="Bitcoin;btc;💰
+Etherium;eth;🍸
+Basic Attention Token;bat;🦁
+LBC;lbc;📚
+"
+
+# Directory where currency info is stored.
+dir="${XDG_DATA_HOME:-$HOME/.local/share}/crypto-prices"
+
+getprices() { # The command to get the desired prices
+ printf "🔃 "; printprices
+ { rm -rf "${dir:?}/*"
+ echo "$coins" | while IFS=';' read -r human web icon; do
+ val="$(curl -s "rate.sx/1$web")" &&
+ echo "$icon;$val;$human" > "$dir/$web"
+ done; [ -d "$dir" ] && touch "$dir"
+ pkill -RTMIN+13 "${STATUSBAR:-dwmblocks}" ;} &
+ exit
+ }
+
+printprices() { # Print/format all prices
+ for x in "$dir"/*; do
+ [ -f "$x" ] || break
+ info="$(cut -d';' -f-2 --output-delimiter=' ' "$x")"
+ printf "%s $%0.2f " $info
+ done | sed 's/ $//'
+ }
+
+mkdir -p "$dir"
+
+# If currencies haven't been updated today, try to update them.
+[ "$(stat -c %x "$HOME/.local/share/crypto-prices" | cut -d' ' -f1)" != "$(date '+%Y-%m-%d')" ] &&
+ { ping -q -c 1 1.1.1.1 >/dev/null 2>&1 && getprices || exit ;}
+
+case $BLOCK_BUTTON in
+ 1) uptime="$(date -d "$(stat -c %x "$dir")" '+%D at %T' | sed "s|$(date '+%D')|Today|")"
+ notify-send "Exact prices in USD" "$(awk -F';' '{print $1, $3 ":\n\t$" $2}' "$dir"/*)
+<b>Last updated:</b>
+ $uptime" ;;
+ 2) getprices ;;
+ 3) notify-send "💸 Crypto-currency module" "\- Left click for exact prices.
+- Middle click to update.
+- Shows 🔃 if updating prices.
+- Manually add/remove currencies to list in the script." ;;
+esac
+
+printprices