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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/sh
# Weather forecast with Nerd Font icons
# Uses wttr.in weather codes to determine icon
url="${WTTRURL:-wttr.in}"
weatherreport="${XDG_CACHE_HOME:-$HOME/.cache}/weatherreport"
# Get a weather report from 'wttr.in' and save it locally.
getforecast() { timeout --signal=1 2s curl -sf "$url/$LOCATION" > "$weatherreport" || exit 1; }
# Check if forecast is fresh (less than 30 minutes old)
checkforecast() {
[ -s "$weatherreport" ] || return 1
last_modified=$(stat -c %Y "$weatherreport" 2>/dev/null) || return 1
now=$(date +%s)
age=$((now - last_modified))
[ "$age" -lt 1800 ] # 1800 seconds = 30 minutes
}
# Get weather icon based on wttr.in output
# Weather conditions from line 3 of the report
getweathericon() {
condition=$(echo "$weatherdata" | sed '3q;d' | tr '[:upper:]' '[:lower:]')
case "$condition" in
*thunder*rain*|*storm*) echo "" ;; # lightning_rainy
*thunder*|*lightning*) echo "" ;; # lightning
*snow*rain*|*sleet*) echo "" ;; # snowy_rainy
*heavy*snow*) echo "" ;; # partly_snowy_heavy
*snow*) echo "" ;; # snowy
*heavy*rain*|*pour*) echo "" ;; # pouring
*rain*|*drizzle*) echo "" ;; # rainy
*hail*) echo "" ;; # hail
*fog*|*mist*) echo "" ;; # fog
*haz*) echo "" ;; # hazy
*tornado*) echo "" ;; # tornado
*hurricane*) echo "" ;; # hurricane
*wind*) echo "" ;; # windy
*partly*cloud*|*overcast*) echo "" ;; # partly_cloudy
*cloud*) echo "" ;; # cloudy
*sun*|*clear*) echo "" ;; # sunny
*) echo "" ;; # default: partly_cloudy
esac
}
getprecipchance() {
precipdata=$(echo "$weatherdata" | sed '16q;d' | grep -wo "[0-9]*%" 2>/dev/null)
if [ -n "$precipdata" ]; then
echo "$precipdata" | sort -rn 2>/dev/null | head -1
else
echo "0%"
fi
}
getdailyhighlow() {
tempdata=$(echo "$weatherdata" | sed '13q;d' | grep -o "m\\([-+]\\)*[0-9]\\+" 2>/dev/null | sed 's/[+m]//g')
if [ -n "$tempdata" ]; then
echo "$tempdata" | sort -g 2>/dev/null | sed -e 1b -e '$!d'
else
echo "??"
echo "??"
fi
}
readfile() { weatherdata="$(cat "$weatherreport")" ;}
validateweather() {
[ -z "$weatherdata" ] && return 1
tempdata=$(echo "$weatherdata" | sed '13q;d' | grep -o "m\\([-+]\\)*[0-9]\\+" 2>/dev/null | sed 's/[+m]//g')
[ -z "$tempdata" ] && return 1
return 0
}
showweather() {
readfile
# If data invalid, try fetching new data
if ! validateweather; then
getforecast 2>/dev/null || exit 0
readfile
validateweather || exit 0
fi
icon=$(getweathericon)
precip=$(getprecipchance)
temps=$(getdailyhighlow)
low=$(echo "$temps" | head -1)
high=$(echo "$temps" | tail -1)
# Don't show if we got placeholder data
echo "$temps" | grep -q "??" && exit 0
printf "%s %s° %s°\n" "$icon" "$low" "$high"
}
case $BLOCK_BUTTON in
1) setsid -f "$TERMINAL" -e less -Sf "$weatherreport" ;;
2) getforecast && showweather ;;
3) notify-send " Weather module" "\- Left click for full forecast.
- Middle click to update forecast.
- Shows current conditions with temp range" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
checkforecast || getforecast 2>/dev/null
showweather