aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/statusbar/sb-forecast
diff options
context:
space:
mode:
authorawy <awy@awy.one>2024-11-28 17:49:11 +0300
committerawy <awy@awy.one>2024-11-28 17:49:11 +0300
commitabeb2dcdf2f2e6ca3ed1836d8cf3d6edf93129f3 (patch)
treeb67df3bfdaeddf4165361899053cb4c74d2681e5 /.local/bin/statusbar/sb-forecast
upload
Diffstat (limited to '.local/bin/statusbar/sb-forecast')
-rwxr-xr-x.local/bin/statusbar/sb-forecast53
1 files changed, 53 insertions, 0 deletions
diff --git a/.local/bin/statusbar/sb-forecast b/.local/bin/statusbar/sb-forecast
new file mode 100755
index 0000000..d0e5326
--- /dev/null
+++ b/.local/bin/statusbar/sb-forecast
@@ -0,0 +1,53 @@
+#!/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 "rain: %s min: %sĀ° max: %sĀ°\n" "$(getprecipchance)" $(getdailyhighlow)
+}
+
+case $BLOCK_BUTTON in
+ 1) setsid -f "$TERMINAL" -e less -Sfr "$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" ;;
+esac
+
+checkforecast || getforecast
+
+showweather