summaryrefslogtreecommitdiff
path: root/.local/bin/statusbar/i3weather
blob: dec12dd18a8773ab1a034672a99a04231447f960 (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
#!/bin/sh
# i3blocks weather module using wttr.in
# output is set to -- 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"

notify() {
	[ "$isUpdating" = 1 ] &&
		notify-send --replace-id=217534 "$@"
}

err() {
	printf "i3weather: %s\n" "$@" >&2
	notify "🛑 ERROR (i3weather)" "$*"
	echo "⛅ -- "
	exit 1
}

case "$BLOCK_BUTTON" in
	'') true ;;
	1)
		[ -f "$WTTR_CACHE" ] || BLOCK_BUTTON=5 i3weather
		st -c "dropdown_weather" \
			-g "$(wc -L < "$WTTR_TXT")x30" \
			-e less -r "$WTTR_CACHE" & sleep 0.3 ;;
	2) $TERMINAL -e nvim "$0" ;;
	*) isUpdating=1; notify "⛅ Refreshing weather info..." ;;
esac

# update cache files
res="$(curl -Ss "$WTTR_URL" > "$WTTR_CACHE" 2>&1)" || err "$res"
sed 's,\x1B\[[0-9;]*[a-zA-z],,g' "$WTTR_CACHE" > "$WTTR_TXT"
[ "$(wc -l "$WTTR_TXT" | cut -d' ' -f1)" -lt 20 ] && err "$(cat "$WTTR_TXT")"

# get weather based on argument
case "$1" in
	info) weather="$(curl -Ss "$WTTR_URL?format=%c%C" 2>&1)" ;;
	cache) weather="$(sed -E '1,7!d; 3d; 6d; s/.{16}(.{,13}).*/\1/; s/\s//g' "$WTTR_TXT" |
			sed -z 's/\n/:/; s/\n/  /g; s/\s*$//' 2>&1)" ;;
	*) weather="$(curl -Ss "$WTTR_URL?format=%l:+%c+%t(%f)++%w++%p" 2>&1)" ;;
esac

# check for empty responses and errors
[ "$(echo "$weather" | grep -Ec "(Unknown|curl|HTML)")" -gt 0 ] &&
  err "$weather" || echo "${weather:-⛅ -- }"

# End response when no error occurs
notify "✔️ Weather Updated."