nixos/home/hyprland/ironbar/reference/scripts/weather

166 lines
6.6 KiB
Plaintext
Raw Normal View History

2023-10-10 20:00:04 +01:00
#!/usr/bin/env bash
## Collect data
cache_dir="$HOME/.cache/eww/weather"
cache_weather_stat=${cache_dir}/weather-stat
cache_weather_degree=${cache_dir}/weather-degree
cache_weather_quote=${cache_dir}/weather-quote
cache_weather_hex=${cache_dir}/weather-hex
cache_weather_icon=${cache_dir}/weather-icon
## Weather data
KEY=8289fc0cb3cc706f3078a8c86bea911f
ID=5288636
UNIT=metric # Available options : 'metric' or 'imperial'
## Make cache dir
if [[ ! -d "$cache_dir" ]]; then
mkdir -p ${cache_dir}
fi
## Get data
get_weather_data() {
weather=$(curl -sf "http://api.openweathermap.org/data/2.5/weather?APPID="$KEY"&lat=24.963214912659005&lon=121.26339510521424&units="$UNIT"")
echo ${weather}
if [ -n "$weather" ]; then
weather_temp=$(echo "$weather" | jq ".main.temp" | cut -d "." -f 1)
weather_icon_code=$(echo "$weather" | jq -r ".weather[].icon" | head -1)
weather_description=$(echo "$weather" | jq -r ".weather[].description" | head -1 | sed -e "s/\b\(.\)/\u\1/g")
#Big long case statement of doom
case "$weather_icon_code" in
50d)
weather_icon=" "
weather_quote="Forecast says it's misty \nMake sure you don't get lost on your way..."
weather_hex="#84afdb"
;;
50n)
weather_icon=" "
weather_quote="Forecast says it's a misty night \nDon't go anywhere tonight or you might get lost..."
weather_hex="#84afdb"
;;
01d)
weather_icon=" "
weather_quote="It's a sunny day, gonna be fun! \nDon't go wandering all by yourself though..."
weather_hex="#ffd86b"
;;
01n)
weather_icon=" "
weather_quote="It's a clear night \nYou might want to take a evening stroll to relax..."
weather_hex="#fcdcf6"
;;
02d)
weather_icon=" "
weather_quote="It's cloudy, sort of gloomy \nYou'd better get a book to read..."
weather_hex="#adadff"
;;
02n)
weather_icon=" "
weather_quote="It's a cloudy night \nHow about some hot chocolate and a warm bed?"
weather_hex="#adadff"
;;
03d)
weather_icon=" "
weather_quote="It's cloudy, sort of gloomy \nYou'd better get a book to read..."
weather_hex="#adadff"
;;
03n)
weather_icon=" "
weather_quote="It's a cloudy night \nHow about some hot chocolate and a warm bed?"
weather_hex="#adadff"
;;
04d)
weather_icon=" "
weather_quote="It's cloudy, sort of gloomy \nYou'd better get a book to read..."
weather_hex="#adadff"
;;
04n)
weather_icon=" "
weather_quote="It's a cloudy night \nHow about some hot chocolate and a warm bed?"
weather_hex="#adadff"
;;
09d)
weather_icon=" "
weather_quote="It's rainy, it's a great day! \nGet some ramen and watch as the rain falls..."
weather_hex="#6b95ff"
;;
09n)
weather_icon=" "
weather_quote=" It's gonna rain tonight it seems \nMake sure your clothes aren't still outside..."
weather_hex="#6b95ff"
;;
10d)
weather_icon=" "
weather_quote="It's rainy, it's a great day! \nGet some ramen and watch as the rain falls..."
weather_hex="#6b95ff"
;;
10n)
weather_icon=" "
weather_quote=" It's gonna rain tonight it seems \nMake sure your clothes aren't still outside..."
weather_hex="#6b95ff"
;;
11d)
weather_icon=""
weather_quote="There's storm for forecast today \nMake sure you don't get blown away..."
weather_hex="#ffeb57"
;;
11n)
weather_icon=""
weather_quote="There's gonna be storms tonight \nMake sure you're warm in bed and the windows are shut..."
weather_hex="#ffeb57"
;;
13d)
weather_icon=" "
weather_quote="It's gonna snow today \nYou'd better wear thick clothes and make a snowman as well!"
weather_hex="#e3e6fc"
;;
13n)
weather_icon=" "
weather_quote="It's gonna snow tonight \nMake sure you get up early tomorrow to see the sights..."
weather_hex="#e3e6fc"
;;
40d)
weather_icon=" "
weather_quote="Forecast says it's misty \nMake sure you don't get lost on your way..."
weather_hex="#84afdb"
;;
40n)
weather_icon=" "
weather_quote="Forecast says it's a misty night \nDon't go anywhere tonight or you might get lost..."
weather_hex="#84afdb"
;;
*)
weather_icon=" "
weather_quote="Sort of odd, I don't know what to forecast \nMake sure you have a good time!"
weather_hex="#adadff"
;;
esac
echo "$weather_icon" >${cache_weather_icon}
echo "$weather_description" >${cache_weather_stat}
echo "$weather_temp""°C" >${cache_weather_degree}
echo -e "$weather_quote" >${cache_weather_quote}
echo "$weather_hex" >${cache_weather_hex}
else
echo "Weather Unavailable" >${cache_weather_stat}
echo " " >${cache_weather_icon}
echo -e "Ah well, no weather huh? \nEven if there's no weather, it's gonna be a great day!" >${cache_weather_quote}
echo "-" >${cache_weather_degree}
echo "#adadff" >${tcache_weather_hex}
fi
}
## Execute
case $1 in
--getdata) get_weather_data ;;
--mintemp) get_weather_data | jq ".main.temp_min" ;;
--maxtemp) get_weather_data | jq ".main.temp_max" ;;
--icon) cat ${cache_weather_icon} ;;
--temp) cat ${cache_weather_degree} ;;
--hex) cat ${cache_weather_hex} ;;
--stat) cat ${cache_weather_stat} ;;
--quote) cat ${cache_weather_quote} | head -n1 ;;
--quote2) cat ${cache_weather_quote} | tail -n1 ;;
esac