summaryrefslogtreecommitdiff
path: root/.local/bin/statusbar/i3weather
blob: c894be6fa6155c0e6de386e332ef1619e2bb9ef6 (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
#!/bin/sh
# weather module for i3blocks
# output is set to 'WEATHER UNAVAILABLE' in case wttr.in is unreachable

WTTR_CACHE_DIR="/tmp"
WTTR_CACHE="$WTTR_CACHE_DIR/wttr.in"
WTTR_TXT="$WTTR_CACHE_DIR/wttr.txt"
WTTR_LOCATION="Vasai"
WTTR_URL="https://wttr.in/$WTTR_LOCATION"

case "$BLOCK_BUTTON" in
	'') true ;;
	1)
		[ -f "$WTTR_CACHE" ] || BLOCK_BUTTON=5 i3weather
		sed 's,\x1B\[[0-9;]*[a-zA-z],,g' "$WTTR_CACHE" > "$WTTR_TXT"
		st -c "dropdown_weather" \
			-g "$(wc -L < "$WTTR_TXT")x38" \
			-e less -r "$WTTR_CACHE" & sleep 0.3
		;;
	2) $TERMINAL -e nvim "$0" ;;
	*) notify-send "⛅ Refreshing weather info..." ;;
esac

case "$1" in
	1) weather="$(curl -s "$WTTR_URL?format=%c")$WTTR_LOCATION   $(curl -Ss "$WTTR_URL?0&T&Q" | sed -E '2,3!d; s/.{16}(.{13}).*/\1/; s/ {4}$//' | tr -d '\n')" ;;
	2) weather="$(curl -s "$WTTR_URL?format=%c%C")" ;;
	*) weather="$(curl -s "$WTTR_URL?format=%c") $(curl -Ss "$WTTR_URL?0&T&Q" | cut -c 16- | head -2)" ;;
esac

if [ "$(echo "$weather" | grep -Ec "(Unknown|curl|HTML)")" -gt 0 ]; then
    echo "WEATHER UNAVAILABLE"
else
    echo "${weather:-⛅ -- }"
fi

curl -s "$WTTR_URL" > "$WTTR_CACHE"