feat(theme): better weather

how many times have i refactored the weather now
This commit is contained in:
bendn 2023-01-11 08:48:39 +07:00 committed by Jan De Dobbeleer
parent c6396dd1b7
commit 86c0daaa68

View file

@ -101,76 +101,39 @@ blocks:
leading_diamond: leading_diamond:
properties: properties:
script: | script: |
api_key="$OWM_API_KEY" # change this
location='$LOCATION' # change this (or dont, and set the variable in your env)
set -e
now="$(date +'%I:%M%P')" now="$(date +'%I:%M%P')"
if [[ -f /tmp/weather.w ]]; then
if [[ -f /tmp/weather.w && $NO_WEATHER_CACHE -ne 1 ]]; then update="$(cat /tmp/last_update.w)"
update=$(jq '.minute' /tmp/weather.w | tr -d '"') if [[ -f /tmp/w_lock.w || $(date +'%s') -le $((update + 600)) ]]; then
# refresh every ten minutes echo -n "$now"
if [[ $(bc -l <<<"$(date +'%M')<=($update+10)") -eq 1 ]]; then cat /tmp/weather.w
echo "$now" "$(jq .weather /tmp/weather.w | tr -d '"')" && exit 0 exit 0
fi fi
elif [[ -f /tmp/w_lock.w ]]; then
echo "$now"
exit 0
fi fi
touch /tmp/w_lock.w
gurl="curl -Gsm.7 https://api.openweathermap.org" if ! res=$(curl -s wttr.in -Gsm2 --data-urlencode "format=j1"); then
enc="--data-urlencode" echo "$now"
date +'%s' >/tmp/last_update.w
# get location echo 睊 >/tmp/weather.w
[[ ! -f /tmp/location.w ]] && rm /tmp/w_lock.w
$gurl/geo/1.0/direct $enc "q=$location" $enc "limit=0" $enc "appid=$api_key" | jq -c '{"lat": .[0].lat, "lon": .[0].lon}' >/tmp/location.w exit 0
location="$(cat /tmp/location.w)" fi
res=$(jq .current_condition[] -c <<<"$res")
# get weather temp=$(jq -r .temp_C <<<"$res")
res=$($gurl/data/2.5/weather $enc "lat=$(jq .lat <<<"$location")" $enc "lon=$(jq .lon <<<"$location")" $enc "units=metric" $enc "appid=$api_key") feels_like=$(jq -r .FeelsLikeC <<<"$res")
temp=$(jq .main.temp <<<"$res") [[ $(bc -l <<<"$feels_like!=$temp") -eq 1 ]] && feels_string=" feels $feels_like°C"
temp_min=$(jq .main.temp_min <<<"$res") WWC_MAP='{"113":"Sunny","116":"PartlyCloudy","119":"Cloudy","122":"VeryCloudy","143":"Fog","176":"LightShowers","179":"LightSleetShowers","182":"LightSleet","185":"LightSleet","200":"ThunderyShowers","227":"LightSnow","230":"HeavySnow","248":"Fog","260":"Fog","263":"LightShowers","266":"LightRain","281":"LightSleet","284":"LightSleet","293":"LightRain","296":"LightRain","299":"HeavyShowers","302":"HeavyRain","305":"HeavyShowers","308":"HeavyRain","311":"LightSleet","314":"LightSleet","317":"LightSleet","320":"LightSnow","323":"LightSnowShowers","326":"LightSnowShowers","329":"HeavySnow","332":"HeavySnow","335":"HeavySnowShowers","338":"HeavySnow","350":"LightSleet","353":"LightShowers","356":"HeavyShowers","359":"HeavyRain","362":"LightSleetShowers","365":"LightSleetShowers","368":"LightSnowShowers","371":"HeavySnowShowers","374":"LightSleetShowers","377":"LightSleet","386":"ThunderyShowers","389":"ThunderyHeavyRain","392":"ThunderySnowShowers","395":"HeavySnowShowers"}';NF_DAY_MAP='{"Unknown":"ﮊ","Cloudy":"","Fog":"","HeavyRain":"","HeavyShowers":"","HeavySnow":"","HeavySnowShowers":"","LightRain":"","LightShowers":"","LightSleet":"","LightSleetShowers":"","LightSnow":"","LightSnowShowers":"","PartlyCloudy":"","Sunny":"","ThunderyHeavyRain":"","ThunderyShowers":"","ThunderySnowShowers":"","VeryCloudy":""}';NF_NIGHT_MAP='{"Unknown":"ﮊ","Cloudy":"","Fog":"","HeavyRain":"","HeavyShowers":"","HeavySnow":"","HeavySnowShowers":"","LightRain":"","LightShowers":"","LightSleet":"","LightSleetShowers":"","LightSnow":"","LightSnowShowers":"","PartlyCloudy":"","Sunny":"","ThunderyHeavyRain":"","ThunderyShowers":"","ThunderySnowShowers":"","VeryCloudy":""}'
temp_max=$(jq .main.temp_max <<<"$res") cur_h="$(date +'%H')" ;wwc="$(jq '.weatherCode' <<<"$res")"
[[ $(bc -l <<<"$temp_min!=$temp") -eq 1 ]] && temp_min_s="$temp_min-" if [[ $cur_h -gt 18 || $cur_h -lt 6 ]]; then icon=$(jq -r ".$(echo "$WWC_MAP" | jq -r ".$wwc")" <<<"$NF_DAY_MAP")
[[ $(bc -l <<<"$temp_max==$temp") -eq 1 ]] && temp_s="$temp_min_s$temp°C" || temp_s+="(${temp_min}${temp}-${temp_max})°C" else icon=$(jq -r ".$(echo "$WWC_MAP" | jq -r ".$wwc")" <<<"$NF_NIGHT_MAP"); fi
feels_like=$(jq .main.feels_like <<<"$res") weather="$icon $temp°C$feels_string"
[[ $(bc -l <<<"$feels_like!=$temp_min&&$feels_like!=$temp&&$feels_like!=$temp_max") -eq 1 ]] && feels_string="feels $feels_like°C" echo "$now $weather"
date +'%s' >/tmp/last_update.w
# pick weather icon echo "$weather" >/tmp/weather.w
case $(jq .weather[0].icon <<<"$res" | tr -d '"') in rm /tmp/w_lock.w
01n | 01d)
icon='\ufa98'
;;
02n | 02d)
icon='\ufa94'
;;
03n | 03d)
icon='\ue33d'
;;
04n | 04d)
icon='\ue312'
;;
09n | 09d)
icon='\ufa95'
;;
10n | 10d)
icon='\ue308'
;;
11n | 11d)
icon='\ue31d'
;;
13n | 13d)
icon='\ue31a'
;;
50n | 50d)
icon='\ue313'
;;
*)
icon=''
;;
esac
weather="$icon - $temp_s $feels_string"
echo -e "$now $weather"
echo -e "{\"minute\": \"$(date +'%M')\", \"weather\": \"$weather\"}" >/tmp/weather.w
style: diamond style: diamond
template: "{{ .Output }}" template: "{{ .Output }}"
trailing_diamond: trailing_diamond: