diff --git a/.config/polybar/bars.ini b/.config/polybar/bars.ini index a70b1df..8483a21 100644 --- a/.config/polybar/bars.ini +++ b/.config/polybar/bars.ini @@ -52,7 +52,7 @@ modules-left = bspwm modules-center = mpd - modules-right = memory cpu temperature eth check-arch-updates date + modules-right = openweather memory cpu temperature eth check-arch-updates date eth-interface = enp0s31f6 diff --git a/.config/polybar/custom.ini b/.config/polybar/custom.ini index 099c724..1ac8663 100644 --- a/.config/polybar/custom.ini +++ b/.config/polybar/custom.ini @@ -45,3 +45,11 @@ [module/bsp-layout-secondary] inherit = module/bsp-layout exec = bsp-layout get $(bspc query -D --names -d ${MONITOR_SECONDARY}:focused) + +[module/openweather] + type = custom/script + exec = $HOME/.config/polybar/scripts/openweather.sh + interval = 600 + + format-prefix-foreground = ${colors.foreground} + format-underline = ${colors.green} diff --git a/.config/polybar/scripts/openweather.sh b/.config/polybar/scripts/openweather.sh new file mode 100755 index 0000000..207e39c --- /dev/null +++ b/.config/polybar/scripts/openweather.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env sh + +. "$HOME/.config/environment.local" + +get_icon() { + case $1 in + # Icons for Font Awesome 5 Pro + 01d) icon="";; + 01n) icon="";; + 02d) icon="";; + 02n) icon="";; + 03d) icon="";; + 03n) icon="";; + 04*) icon="";; + 09*) icon="";; + 10d) icon="";; + 10n) icon="";; + 11*) icon="";; + 13*) icon="";; + 50*) icon="";; + *) icon=""; + esac + + echo $icon +} + +KEY="$OPENWEATHER_KEY" +CITY="$OPENWEATHER_CITY" +UNITS="metric" +SYMBOL="°" + +API="https://api.openweathermap.org/data/2.5" + +if [ -n "$CITY" ]; then + if [ "$CITY" -eq "$CITY" ] 2>/dev/null; then + CITY_PARAM="id=$CITY" + else + CITY_PARAM="q=$CITY" + fi + + weather=$(curl -sf "$API/weather?appid=$KEY&$CITY_PARAM&units=$UNITS") +else + location=$(curl -sf https://location.services.mozilla.com/v1/geolocate?key=geoclue) + + if [ -n "$location" ]; then + location_lat="$(echo "$location" | jq '.location.lat')" + location_lon="$(echo "$location" | jq '.location.lng')" + + weather=$(curl -sf "$API/weather?appid=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS") + fi +fi + +if [ -n "$weather" ]; then + weather_temp=$(echo "$weather" | jq ".main.temp" | cut -d "." -f 1) + weather_icon=$(echo "$weather" | jq -r ".weather[0].icon") + + echo "$(get_icon "$weather_icon")" "$weather_temp$SYMBOL" +fi