From 9d90d53c3d94a4c0dcf9922401f96887dfdd7668 Mon Sep 17 00:00:00 2001 From: M1dgard Date: Sun, 8 Apr 2018 22:08:44 +0200 Subject: [PATCH] Add OpenWeatherMap module It needs an API key for which you have to solve a Google captcha which I refuse, so I'm probably going to remove this again --- polybar/config.ini | 7 ++ polybar/openweathermap-fullfeatured.sh | 90 ++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100755 polybar/openweathermap-fullfeatured.sh diff --git a/polybar/config.ini b/polybar/config.ini index 198db8d..0ce0f24 100644 --- a/polybar/config.ini +++ b/polybar/config.ini @@ -53,6 +53,7 @@ font-2 = fixed:pixelsize=10;1 font-3 = "Material Icons:pixelsize=14;3" font-4 = "Fira Sans:size=12;1" font-5 = "Fira Mono:size=10;1" +font-6 = "Weather Icons:size=10;1" ;font-3 = unifont:fontformat=truetype:size=8:antialias=false;0 ;font-4 = siji:pixelsize=10;1 @@ -395,6 +396,12 @@ type = custom/script exec = LAYOUT=azerty ICON= METRIC=wpm ~/.config/polybar/info-hackspeed.sh tail = true +[module/weather-full] +type = custom/script +exec = ~/.config/polybar/openweathermap-fullfeatured.sh +interval = 600 +label-font = 7 + [settings] screenchange-reload = true ; they say: see https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-operator-t diff --git a/polybar/openweathermap-fullfeatured.sh b/polybar/openweathermap-fullfeatured.sh new file mode 100755 index 0000000..1cde46f --- /dev/null +++ b/polybar/openweathermap-fullfeatured.sh @@ -0,0 +1,90 @@ +#!/bin/sh + +get_icon() { + case $1 in + 01d) icon="";; + 01n) icon="";; + 02d) icon="";; + 02n) icon="";; + 03*) icon="";; + 04*) icon="";; + 09d) icon="";; + 09n) icon="";; + 10d) icon="";; + 10n) icon="";; + 11d) icon="";; + 11n) icon="";; + 13d) icon="";; + 13n) icon="";; + 50d) icon="";; + 50n) icon="";; + *) icon=""; + esac + + echo $icon +} + +get_duration() { + + osname=$(uname -s) + + case $osname in + *BSD) date -r "$1" -u +%H:%M;; + *) date --date="@$1" -u +%H:%M;; + esac + +} + +KEY="" +CITY="" +UNITS="metric" +SYMBOL="°" + +if [ ! -z $CITY ]; then + current=$(curl -sf "http://api.openweathermap.org/data/2.5/weather?APPID=$KEY&id=$CITY&units=$UNITS") + forecast=$(curl -sf "http://api.openweathermap.org/data/2.5/forecast?APPID=$KEY&id=$CITY&units=$UNITS&cnt=1") +else + location=$(curl -sf https://location.services.mozilla.com/v1/geolocate?key=geoclue) + + if [ ! -z "$location" ]; then + location_lat="$(echo "$location" | jq '.location.lat')" + location_lon="$(echo "$location" | jq '.location.lng')" + + current=$(curl -sf "http://api.openweathermap.org/data/2.5/weather?appid=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS") + forecast=$(curl -sf "http://api.openweathermap.org/data/2.5/forecast?APPID=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS&cnt=1") + fi +fi + +if [ ! -z "$current" ] && [ ! -z "$forecast" ]; then + current_temp=$(echo "$current" | jq ".main.temp" | cut -d "." -f 1) + current_icon=$(echo "$current" | jq -r ".weather[].icon") + + forecast_temp=$(echo "$forecast" | jq ".list[].main.temp" | cut -d "." -f 1) + forecast_icon=$(echo "$forecast" | jq -r ".list[].weather[].icon") + + + if [ "$current_temp" -gt "$forecast_temp" ]; then + trend="" + elif [ "$forecast_temp" -gt "$current_temp" ]; then + trend="" + else + trend="" + fi + + + sun_rise=$(echo "$current" | jq ".sys.sunrise") + sun_set=$(echo "$current" | jq ".sys.sunset") + now=$(date +%s) + + if [ "$sun_rise" -gt "$now" ]; then + daytime=" $(get_duration "$(($sun_rise-$now))")" + elif [ "$sun_set" -gt "$now" ]; then + daytime=" $(get_duration "$(($sun_set-$now))")" + else + daytime=" $(get_duration "$(($sun_rise-$now))")" + fi + + echo "$(get_icon "$current_icon") $current_temp$SYMBOL $trend $(get_icon "$forecast_icon") $forecast_temp$SYMBOL $daytime" +else + echo "No weather info" +fi