voidrice/.local/bin/statusbar/sb-forecast
sban b8cd0ab495
Timeout added to forecast module, ncmpcpp now tracks player state (#1359)
* Added timeout to getforecast to prevent status bar breakage.

A 2 second timeout is used in the case that wttr.in is inaccessible when dwm is started; as otherwise it tries to curl wttr.in indefinitely, not allowing other status bar modules to be loaded.

* Update music status bar module on player state change
2023-09-05 07:51:05 +00:00

53 lines
1.7 KiB
Bash
Executable file

#!/bin/sh
# Displays today's precipication chance (☔), and daily low (🥶) and high (🌞).
# Usually intended for the statusbar.
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; }
# Forecast should be updated only once a day.
checkforecast() {
[ -s "$weatherreport" ] && [ "$(stat -c %y "$weatherreport" 2>/dev/null |
cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
}
getprecipchance() {
echo "$weatherdata" | sed '16q;d' | # Extract line 16 from file
grep -wo "[0-9]*%" | # Find a sequence of digits followed by '%'
sort -rn | # Sort in descending order
head -1q # Extract first line
}
getdailyhighlow() {
echo "$weatherdata" | sed '13q;d' | # Extract line 13 from file
grep -o "m\\([-+]\\)*[0-9]\\+" | # Find temperatures in the format "m<signed number>"
sed 's/[+m]//g' | # Remove '+' and 'm'
sort -g | # Sort in ascending order
sed -e 1b -e '$!d' # Extract the first and last lines
}
readfile() { weatherdata="$(cat "$weatherreport")" ;}
showweather() {
readfile
printf "☔%s 🥶%s° 🌞%s°\n" "$(getprecipchance)" $(getdailyhighlow)
}
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.
☔: Chance of rain/snow
🥶: Daily low
🌞: Daily high" ;;
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
checkforecast || getforecast
showweather