diff options
author | awy <awy@awy.one> | 2025-08-18 19:17:30 +0300 |
---|---|---|
committer | awy <awy@awy.one> | 2025-08-18 19:17:30 +0300 |
commit | b3526d31b40dbdf37ea4b314c8e9aa94d39b7f8c (patch) | |
tree | f262a162e13d28f8915d09d9406f5c08f9729841 /sb-weather |
init
Diffstat (limited to 'sb-weather')
-rwxr-xr-x | sb-weather | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/sb-weather b/sb-weather new file mode 100755 index 0000000..dfaad4b --- /dev/null +++ b/sb-weather @@ -0,0 +1,82 @@ +#!/bin/sh +arg=$1 +report=~/.cache/weather_report.json + +ifinstalled jq + +# Get a weather report from 'wttr.in' and save it locally. +getforecast() { { rg -q -m1 '^up$' /sys/class/net/w*/operstate || rg -q -m1 '^up$' /sys/class/net/e*/operstate; } && + curl -sf "wttr.in/?format=j1" --output "$report" && touch "$report" +} + +# Forecast should be updated only once a day. +checkforecast() { + [ "$(stat -c %y "$report" 2>/dev/null | + cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] +} + +# Function to get weather emoji from code +get_weather_emoji() { + local code="$1" + grep "^$code " ~/.local/share/extras/weather_codes | awk '{print $2}' +} + +showweather(){ + temp=$(jq -r '.current_condition[0].temp_C' $report) + code=$(jq -r '.current_condition[0].weatherCode' $report) + rainchance=$(jq '.weather[].hourly[].chanceofrain | tonumber' $report | sort -n | tail -n 1) + + emoji=$(get_weather_emoji "$code") + case "$arg" in + weather) + case 1 in + $((temp >= 30)) ) class="very_hot" ;; + $((temp >= 26)) ) class="hot" ;; + $((temp >= 21)) ) class="warm" ;; + $((temp >= 16)) ) class="mild" ;; + $((temp >= 11)) ) class="cool" ;; + $((temp >= 6)) ) class="chilly" ;; + $((temp >= 0)) ) class="cold" ;; + $((temp < 0)) ) class="below_zero" ;; + * ) echo unavailable && exit ;; + esac + output="$emoji$temp°C" + ;; + rain) + case 1 in + $((rainchance >= 70)) ) class="high" ;; + $((rainchance >= 45)) ) class="mid" ;; + $((rainchance > 0)) ) class="low" ;; + $((rainchance <= 0)) ) printf '' && exit ;; + * ) echo unavailable && exit ;; + esac + output="$rainchance%" + ;; + *) + echo "Usage: $0 {weather|rain}" + exit 1 + ;; + esac + printf '{"text": "%s", "class": "%s" }' "$output" "$class" +} + +case $BLOCK_BUTTON in + 2) getforecast && showweather ;; + 3) notify-send " Weather module" "\- Middle click to update forecast. +: Chance of rain/snow +: Current temperature" ;; + 8) setsid -f "$TERMINAL" -e "$EDITOR" "$0" >/dev/null 2>&1 ;; +esac + +# shellcheck disable=SC2015 +checkforecast && showweather || + ( flock -n 9 && + ( tries=0; while [ $tries -ne 100 ]; do + getforecast && break || + { tries=$((tries+1)); sleep .1; } + done + ! checkforecast && + until getforecast; do sleep 60; done + pkill -RTMIN+5 "${STATUSBAR:-waybar}" + ) & + echo ) 9>"${XDG_RUNTIME_DIR}/sb-forecast.lock" |