Rewrite sb-forecast
This commit is contained in:
parent
e0331ad0e7
commit
fe198c960f
1 changed files with 33 additions and 19 deletions
|
@ -1,24 +1,41 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# Displays todays precipication chance (☔) and daily low (🥶) and high (🌞).
|
# Displays today's precipication chance (☔), and daily low (🥶) and high (🌞).
|
||||||
# Usually intended for the statusbar.
|
# Usually intended for the statusbar.
|
||||||
|
|
||||||
# If we have internet, get a weather report from wttr.in and store it locally.
|
weather_report="${XDG_CACHE_HOME:-$HOME/.cache}/weatherreport"
|
||||||
# You could set up a shell alias to view the full file in a pager in the
|
|
||||||
# terminal if desired. This function will only be run once a day when needed.
|
|
||||||
weatherreport="${XDG_CACHE_HOME:-$HOME/.cache}/weatherreport"
|
|
||||||
getforecast() { curl -sf "wttr.in/$LOCATION" > "$weatherreport" || exit 1 ;}
|
|
||||||
|
|
||||||
# Some very particular and terse stream manipulation. We get the maximum
|
# Get a weather report from 'wttr.in' and save it locally.
|
||||||
# precipitation chance and the daily high and low from the downloaded file and
|
get_forecast() { curl -sf "wttr.in/$LOCATION" > "$weather_report" || exit 1; }
|
||||||
# display them with coresponding emojis.
|
|
||||||
showweather() { printf "%s" "$(sed '16q;d' "$weatherreport" |
|
# Forecast should be updated only once a day
|
||||||
grep -wo "[0-9]*%" | sort -rn | sed "s/^/☔/g;1q" | tr -d '\n')"
|
check_forecast() {
|
||||||
sed '13q;d' "$weatherreport" | grep -o "m\\([-+]\\)*[0-9]\\+" | sed 's/+//g' | sort -n -t 'm' -k 2n | sed -e 1b -e '$!d' | tr '\n|m' ' ' | awk '{print " 🥶" $1 "°","🌞" $2 "°"}' ;}
|
[ -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)
|
||||||
|
}
|
||||||
|
|
||||||
case $BLOCK_BUTTON in
|
case $BLOCK_BUTTON in
|
||||||
1) setsid -f "$TERMINAL" -e less -Srf "$weatherreport" ;;
|
1) setsid -f "$TERMINAL" -e less -Srf "$weather_report" ;;
|
||||||
2) getforecast && showweather ;;
|
2) get_forecast && show_weather ;;
|
||||||
3) notify-send "🌈 Weather module" "\- Left click for full forecast.
|
3) notify-send "🌈 Weather module" "\- Left click for full forecast.
|
||||||
- Middle click to update forecast.
|
- Middle click to update forecast.
|
||||||
☔: Chance of rain/snow
|
☔: Chance of rain/snow
|
||||||
|
@ -27,9 +44,6 @@ case $BLOCK_BUTTON in
|
||||||
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
|
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# The test if our forcecast is updated to the day. If it isn't download a new
|
check_forecast || get_forecast
|
||||||
# weather report from wttr.in with the above function.
|
|
||||||
[ -s "$weatherreport" ] && [ "$(stat -c %y "$weatherreport" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] ||
|
|
||||||
getforecast
|
|
||||||
|
|
||||||
showweather
|
show_weather
|
||||||
|
|
Loading…
Reference in a new issue