1
0
Fork 0
dotfiles/.local/libexec/statusbar/sb-forecast

37 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
. "$HOME/.local/libexec/statusbar/colors"
# NOTE: This script takes its dwmblocks update signal as an argument so that it can refresh itself (defaults to 5)
# Options can be found at https://wttr.in/:help
weatherfile="$XDG_CACHE_HOME/weatherreport"
emojifile="$XDG_CACHE_HOME/weatheremoji"
url="wttr.in/$FORECAST_LOCATION"
weatheropts="?FA"
emojiopts="?FA&format=%c"
getforecast() {
curl -sfL --retry 5 "$url$weatheropts" >"$weatherfile" || return 1
curl -sfL "$url$emojiopts" --retry 5 | sed 's/☀️//;s/☁️//;s/⛅️//;s/⛈//;s/✨//;s/❄️//;s/🌦//;s/🌧//;s/🌨//;s/🌩//;s/🌫//;' | cut -d ' ' -f 1 >"$emojifile" || return 1
}
fresh() {
[ -s "$weatherfile" ] && [ "$(stat -c %y "$weatherfile" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
}
readfile() {
weather="$(cat "$weatherfile")"
precipitation="$(echo "$weather" | sed '16q;d' | grep -wo "[0-9]*%" | sort -rn | head -1q)"
highlow="$(echo "$weather" | sed '13q;d' | grep -o "m\\([-+]\\)*[0-9]\\+" | sed 's/[+m]//g' | sort -g | sed -e 1b -e '$!d')"
emoji="$(cat "$emojifile")"
}
output() {
readfile
printf "%b" "$emoji $precipitation <span $fgblue></span> $(echo "$highlow" | cut -d" " -f1)° <span $fgred></span> $(echo "$highlow" | cut -d" " -f2)°\n"
}
if fresh; then
output
else
getforecast && pkill "-RTMIN+${1:-5}" dwmblocks &
printf "%b" " Getting Weather\n"
fi