voidrice/.local/bin/statusbar/sb-forecast

54 lines
1.7 KiB
Text
Raw Normal View History

#!/bin/sh
2023-02-12 10:24:10 +00:00
# Displays today's precipication chance (☔), and daily low (🥶) and high (🌞).
2020-04-06 03:14:14 +00:00
# Usually intended for the statusbar.
url="${WTTRURL:-wttr.in}"
2023-02-12 14:00:23 +00:00
weatherreport="${XDG_CACHE_HOME:-$HOME/.cache}/weatherreport"
2019-11-23 21:23:24 +00:00
2023-02-12 10:24:10 +00:00
# Get a weather report from 'wttr.in' and save it locally.
getforecast() { timeout --signal=1 2s curl -sf "$url/$LOCATION" > "$weatherreport" || exit 1; }
2023-02-12 10:24:10 +00:00
2023-02-12 14:00:23 +00:00
# Forecast should be updated only once a day.
checkforecast() {
[ -s "$weatherreport" ] && [ "$(stat -c %y "$weatherreport" 2>/dev/null |
2023-02-12 10:24:10 +00:00
cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
}
2023-02-12 14:00:23 +00:00
getprecipchance() {
echo "$weatherdata" | sed '16q;d' | # Extract line 16 from file
2023-02-12 10:24:10 +00:00
grep -wo "[0-9]*%" | # Find a sequence of digits followed by '%'
sort -rn | # Sort in descending order
head -1q # Extract first line
}
2023-02-12 14:00:23 +00:00
getdailyhighlow() {
echo "$weatherdata" | sed '13q;d' | # Extract line 13 from file
2023-02-12 10:24:10 +00:00
grep -o "m\\([-+]\\)*[0-9]\\+" | # Find temperatures in the format "m<signed number>"
sed 's/[+m]//g' | # Remove '+' and 'm'
2023-02-17 17:12:10 +00:00
sort -g | # Sort in ascending order
2023-02-12 10:24:10 +00:00
sed -e 1b -e '$!d' # Extract the first and last lines
}
2023-02-12 14:00:23 +00:00
readfile() { weatherdata="$(cat "$weatherreport")" ;}
showweather() {
readfile
printf "☔%s 🥶%s° 🌞%s°\n" "$(getprecipchance)" $(getdailyhighlow)
2023-02-12 10:24:10 +00:00
}
2019-11-23 21:23:24 +00:00
case $BLOCK_BUTTON in
1) setsid -f "$TERMINAL" -e less -Sf "$weatherreport" ;;
2023-02-12 14:00:23 +00:00
2) getforecast && showweather ;;
3) notify-send "🌈 Weather module" "\- Left click for full forecast.
2019-11-23 21:23:24 +00:00
- Middle click to update forecast.
☔: Chance of rain/snow
🥶: Daily low
2019-11-23 21:23:24 +00:00
🌞: Daily high" ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
2019-11-23 21:23:24 +00:00
esac
2023-02-12 14:00:23 +00:00
checkforecast || getforecast
2020-04-06 03:14:14 +00:00
2023-02-12 14:00:23 +00:00
showweather