summaryrefslogtreecommitdiff
path: root/crypto
blob: 736d696612e564e076a9ac669b1e408c3e8f9fe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/sbin/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;📚
#Monero;xmr;"
coins="Etherium;eth;e
Bitcoin;btc;b
Monero;xmr;m"

# 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
                icon=`cut -d';' -f1 "$x"`
                price=`cut -d';' -f2 "$x"`
		printf "[%s$%0.2f]" "$icon" "$price"
	done | sed 's/ $/\n/'
	}

[ ! -d "$dir" ] && mkdir -p "$dir" && { getprices; exit ;}

# 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" ;;
	3) getprices ;;
	2) 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." ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

printprices