Last active
September 16, 2024 17:48
-
-
Save t0m5k1/db8354b336b43ca107cf411d6581c423 to your computer and use it in GitHub Desktop.
.config/waybar/weather.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# You can specify a location here if you want, e.g., LOCATION="London" | |
LOCATION="changeme" | |
# Fetch weather data | |
WEATHER_DATA=$(curl -s "wttr.in/$LOCATION?format=j1") | |
# Extract current weather information | |
TEMP=$(echo "$WEATHER_DATA" | jq -r '.current_condition[0].temp_C') | |
CONDITION=$(echo "$WEATHER_DATA" | jq -r '.current_condition[0].weatherDesc[0].value') | |
WEATHER_CODE=$(echo "$WEATHER_DATA" | jq -r '.current_condition[0].weatherCode') | |
# Extract tomorrow's forecast | |
TOMORROW_TEMP_MAX=$(echo "$WEATHER_DATA" | jq -r '.weather[1].maxtempC') | |
TOMORROW_TEMP_MIN=$(echo "$WEATHER_DATA" | jq -r '.weather[1].mintempC') | |
TOMORROW_CONDITION=$(echo "$WEATHER_DATA" | jq -r '.weather[1].hourly[4].weatherDesc[0].value') | |
# Function to map weather code to icon | |
get_icon() { | |
case $1 in | |
113) echo "01d" ;; # Clear/Sunny | |
116) echo "02d" ;; # Partly cloudy | |
119|122) echo "03d" ;; # Cloudy | |
143|248|260) echo "50d" ;; # Mist/Fog | |
176|263|266|281|284|293|296|299|302|305|308|311|314|317|320|350|353|356|359|362|365|374|377) echo "09d" ;; # Light/Heavy Rain | |
200|386|389) echo "11d" ;; # Thunderstorm | |
179|182|185|227|230|323|326|329|332|335|338|368|371|392|395) echo "13d" ;; # Snow | |
*) echo "03d" ;; # Default to cloudy | |
esac | |
} | |
ICON=$(get_icon "$WEATHER_CODE") | |
# Create tooltip with current weather and tomorrow's forecast | |
TOOLTIP="Now: $CONDITION, $TEMP°C\nTomorrow: $TOMORROW_CONDITION, $TOMORROW_TEMP_MIN°C - $TOMORROW_TEMP_MAX°C" | |
# Output JSON for Waybar | |
echo "{\"text\":\"$TEMP\", \"alt\":\"$ICON\", \"tooltip\":\"$TOOLTIP\"}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment