commit db2ea1b659dc7ce1ce031b95cd379fb9ef5f3b79 parent 3197f49d62b7d2f58f5e8c9261724ce7c0eea6b0 Author: awy <awy@awy.one> Date: Mon, 19 May 2025 19:36:27 +0300 revert Diffstat:
M | .local/bin/statusbar/sb-forecast | | | 38 | +++++++++++++++++--------------------- |
1 file changed, 17 insertions(+), 21 deletions(-)
diff --git a/.local/bin/statusbar/sb-forecast b/.local/bin/statusbar/sb-forecast @@ -11,29 +11,25 @@ getforecast() { { grep -q -m1 '^up$' /sys/class/net/w*/operstate || grep -q -m1 curl -sf "$url/$LOCATION" --output "$weatherreport" && touch "$weatherreport" } +# Forecast should be updated only once a day. checkforecast() { - [ -f "$weatherreport" ] || return 1 # file doesn't exist -> needs update - - last_fetch=$(stat -c %Y "$weatherreport") - now=$(date +%s) - max_age=$((30 * 60)) # 30 minutes in seconds - - [ $((now - last_fetch)) -lt $max_age ] + [ "$(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 + 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 + 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")" ;} @@ -41,18 +37,18 @@ readfile() { weatherdata="$(cat "$weatherreport")" ;} showweather() { readfile # shellcheck disable=SC2046,SC2183 - printf "ā%s š„¶%s° š%s°\n" "$(getprecipchance)" $(getdailyhighlow) + 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. + 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" ;; - 8) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;; + 8) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;; esac # shellcheck disable=SC2015