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

42 lines
1.4 KiB
Plaintext
Raw Normal View History

2022-07-04 21:36:33 +02:00
#!/bin/sh
2022-08-22 20:33:37 +02:00
2023-04-03 12:49:26 +02:00
# NOTE: This script takes its dwmblocks update signal as an argument so that it can refresh itself (defaults to 5)
2022-08-22 20:33:37 +02:00
# Options can be found at https://wttr.in/:help
2023-02-09 15:29:14 +01:00
reset="\033[0m"
big="\033[11m"
verybig="\033[12m"
red="\033[31m"
blue="\033[34m"
2023-02-09 15:29:14 +01:00
weatherfile="$XDG_CACHE_HOME/weatherreport"
emojifile="$XDG_CACHE_HOME/weatheremoji"
2024-01-15 21:39:02 +01:00
url="wttr.in/$FORECAST_LOCATION"
2023-04-03 12:49:26 +02:00
weatheropts="?FA"
emojiopts="?FA&format=%c"
2023-02-09 15:29:14 +01:00
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
2022-07-04 21:36:33 +02:00
}
2023-04-03 12:49:26 +02:00
fresh() {
[ -s "$weatherfile" ] && [ "$(stat -c %y "$weatherfile" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
2022-08-22 20:33:37 +02:00
}
2023-04-03 12:49:26 +02:00
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")"
2023-04-03 12:49:26 +02:00
}
output() {
readfile
printf "%b" "$verybig$emoji$reset $precipitation $big$blue$reset $(echo $highlow | cut -d" " -f1)° $big$red$reset $(echo $highlow | cut -d" " -f2)°\n"
2023-04-03 12:49:26 +02:00
}
if fresh; then
output
2023-04-03 12:49:26 +02:00
else
getforecast && pkill -RTMIN+${1:-5} dwmblocks &
printf "%b" "$big$reset Getting Weather\n"
2023-04-03 12:49:26 +02:00
fi