Files
configs/.config/waybar/scripts/weather-norrkoping.sh
2026-04-17 19:38:35 +02:00

89 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -u
LOCATION="Norrkoping"
URL="https://wttr.in/${LOCATION}?format=%t|%C"
json_escape() {
local s="$1"
s=${s//\\/\\\\}
s=${s//\"/\\\"}
s=${s//$'\n'/\\n}
printf '%s' "$s"
}
markup_escape() {
local s="$1"
s=${s//&/&}
s=${s//</&lt;}
s=${s//>/&gt;}
printf '%s' "$s"
}
icon_markup() {
local icon="$1"
local color="$2"
printf '<span font_desc="SauceCodePro Nerd Font 15" foreground="%s">%s</span>' "$color" "$icon"
}
icon_for_condition() {
local c
c=$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')
case "$c" in
*thunder*|*storm*)
printf '󰖓'
;;
*snow*|*sleet*|*blizzard*|*ice*)
printf '󰼶'
;;
*rain*|*drizzle*|*shower*)
printf '󰖗'
;;
*fog*|*mist*|*haze*|*smoke*)
printf '󰖑'
;;
*partly*)
printf '󰖕'
;;
*clear*|*sunny*)
printf '󰖙'
;;
*cloud*|*overcast*)
printf '󰖐'
;;
*)
printf '󰖐'
;;
esac
}
if ! command -v curl >/dev/null 2>&1; then
text="<span foreground=\"#EBCB8B\">--°C Unavailable</span> $(icon_markup "󰖐" "#D08770")"
printf '{"text":"%s","tooltip":"curl is required for weather","class":"weather-unavailable"}\n' "$(json_escape "$text")"
exit 0
fi
raw=$(curl -fsS --max-time 6 "$URL" 2>/dev/null || true)
if [[ -z "$raw" || "$raw" != *"|"* ]]; then
text="<span foreground=\"#EBCB8B\">--°C Unavailable</span> $(icon_markup "󰖐" "#D08770")"
printf '{"text":"%s","tooltip":"Weather unavailable for Norrkoping","class":"weather-unavailable"}\n' "$(json_escape "$text")"
exit 0
fi
temp=${raw%%|*}
condition=${raw#*|}
icon=$(icon_for_condition "$condition")
updated=$(date '+%H:%M')
text="<span foreground=\"#D8DEE9\">$(markup_escape "$temp $condition")</span> $(icon_markup "$icon" "#88C0D0")"
tooltip=$(cat <<EOF
Norrkoping, Sweden
$temp - $condition
Updated: $updated
EOF
)
printf '{"text":"%s","tooltip":"%s","class":"weather"}\n' \
"$(json_escape "$text")" \
"$(json_escape "$tooltip")"