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

50 lines
1.6 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.
2023-02-12 10:24:10 +00:00
weather_report="${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.
get_forecast() { curl -sf "wttr.in/$LOCATION" > "$weather_report" || exit 1; }
# Forecast should be updated only once a day
check_forecast() {
[ -s "$weather_report" ] && [ "$(stat -c %y "$weather_report" 2>/dev/null |
cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
}
get_precip_chance() {
sed '16q;d' "$weather_report" | # 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
}
get_daily_high_low() {
sed '13q;d' "$weather_report" | # 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 -n -k 2n | # Sort in ascending order
sed -e 1b -e '$!d' # Extract the first and last lines
}
show_weather() {
printf "☔%s 🥶%s° 🌞%s°\n" "$(get_precip_chance)" $(get_daily_high_low)
}
2019-11-23 21:23:24 +00:00
case $BLOCK_BUTTON in
2023-02-12 10:24:10 +00:00
1) setsid -f "$TERMINAL" -e less -Srf "$weather_report" ;;
2) get_forecast && show_weather ;;
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) "$TERMINAL" -e "$EDITOR" "$0" ;;
2019-11-23 21:23:24 +00:00
esac
2023-02-12 10:24:10 +00:00
check_forecast || get_forecast
2020-04-06 03:14:14 +00:00
2023-02-12 10:24:10 +00:00
show_weather