From 788c9709fb6343d9515b6b20700f9be0bf15a623 Mon Sep 17 00:00:00 2001 From: frosty Date: Mon, 29 Jul 2024 04:10:24 -0400 Subject: [PATCH] (bin): create weather script --- .local/bin/weather | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 .local/bin/weather diff --git a/.local/bin/weather b/.local/bin/weather new file mode 100755 index 0000000..6e701d2 --- /dev/null +++ b/.local/bin/weather @@ -0,0 +1,31 @@ +#!/bin/sh + +WTTR_URL="${WTTR_URL:-wttr.in}" +REPORT_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/wttr-report" +UPDATE_INTERVAL_SECONDS=1800 + +notify() { + notify-send "${0##*/}" "$@" +} + +is_report_old() { + current_time="$(date +'%s')" + report_file_time="$(date -r "$REPORT_FILE" +'%s')" + [ "$((current_time - report_file_time))" -ge "$UPDATE_INTERVAL_SECONDS" ] +} + +if [ ! -f "$REPORT_FILE" ] || is_report_old; then + curl -s "$WTTR_URL?format=%C\n%t\n" >"$REPORT_FILE" + notify "Fetched new weather report." +fi + +i=0 +while IFS= read -r line; do + case "$i" in + 0) conditions="${line%[[:space:]]}" ;; + 1) temperature="${line#+}" ;; + esac + i=$((i + 1)) +done <"$REPORT_FILE" + +printf '%s (%s)\n' "$conditions" "$temperature"