dotfiles

My dotfiles: zsh, lf, sxiv, i3, ranger, mpd and more
Log | Files | Refs | Submodules | README | LICENSE

crypto (1749B)


      1 #!/bin/sh
      2 
      3 # Shows the price for desired cryptocurrencies. Module updates automatically
      4 # every calendar day, but can also be updated with a middle click.
      5 
      6 # Currencies should be ;-separated:
      7 # human-readable name;urlname;icon
      8 coins="Bitcoin;btc;💰
      9 Etherium;eth;🍸
     10 Basic Attention Token;bat;🦁
     11 LBC;lbc;📚"
     12 
     13 # Directory where currency info is stored.
     14 dir="${XDG_DATA_HOME:-$HOME/.local/share}/crypto-prices"
     15 
     16 getprices() { # The command to get the desired prices
     17 	printf "🔃 "; printprices
     18 	{ rm -rf "${dir:?}/*"
     19 	echo "$coins" | while IFS=';' read -r human web icon; do
     20 		val="$(curl -s "rate.sx/1$web")" &&
     21 		echo "$icon;$val;$human" > "$dir/$web"
     22 	done; [ -d "$dir" ] && touch "$dir"
     23 	pkill -RTMIN+13 "${STATUSBAR:-dwmblocks}" ;} &
     24 	exit
     25 	}
     26 
     27 printprices() { # Print/format all prices
     28 	for x in "$dir"/*; do
     29 		[ -f "$x" ] || break
     30 		info="$(cut -d';' -f-2 --output-delimiter=' ' "$x")"
     31 		printf "%s$%0.2f " "$info"
     32 	done | sed 's/ $/\n/'
     33 	}
     34 
     35 [ ! -d "$dir" ] && mkdir -p "$dir" && { getprices; exit ;}
     36 
     37 # If currencies haven't been updated today, try to update them.
     38 [ "$(stat -c %x "$HOME/.local/share/crypto-prices" | cut -d' ' -f1)" != "$(date '+%Y-%m-%d')" ] &&
     39 	{ ping -q -c 1 example.org >/dev/null 2>&1 && getprices || exit ;}
     40 
     41 case $BLOCK_BUTTON in
     42 	1) uptime="$(date -d "$(stat -c %x "$dir")" '+%D at %T' | sed "s|$(date '+%D')|Today|")"
     43 		notify-send "Exact prices in USD" "$(awk -F';' '{print $1, $3 ":\n\t$" $2}' "$dir"/*)
     44 <b>Last updated:</b>
     45 	$uptime" ;;
     46 	2) getprices ;;
     47 	3) notify-send "💸 Crypto-currency module" "\- Left click for exact prices.
     48 - Middle click to update.
     49 - Shows 🔃 if updating prices.
     50 - Manually add/remove currencies to list in the script." ;;
     51 	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
     52 esac
     53 
     54 printprices