#!/bin/sh source ~/.config.env weatherreport="${XDG_DATA_HOME:-$HOME/.local/share}/weatherreport" weatherreportjson="${XDG_DATA_HOME:-$HOME/.local/share}/weatherreportjson" getforecast() { curl -sf "wttr.in/$LOCATION" > "$weatherreport" || exit 1 ;} getforecastjson() { curl -sf "wttr.in/$LOCATION?format=j1" > "$weatherreportjson" || exit 1 ;} # Some very particular and terse stream manipulation. We get the maximum # precipitation chance and the daily high and low from the downloaded file and # display them with coresponding emojis. showweather() { jq -r ".weather|.[0]|.hourly|.[$(expr $(date '+%H') / 4)]|.FeelsLikeC,.chanceofrain,.chanceofsnow,.WindGustKmph" "$weatherreportjson" |sed ':a;N;$!ba;s/\n/ /g' \ | awk -F' ' '{ if ($4 > 30) print "^c#ffffff^ ^d^" $4 "kph ^c#ebcb8b^ 盛 ^d^ " $1 "°" else if ($2 < $3) print "^c#ffffff^ ^d^" $3 "% ^c#ebcb8b^ 盛 ^d^ " $1 "°" else print "^c#81a1c1^ ^d^" $2 "% ^c#ebcb8b^ 盛 ^d^ " $1 "°"}' } case $BLOCK_BUTTON in 1) setsid -f st -c weather -n weather -e less -Srf "$weatherreport" ;; 2) getforecast && showweather && kill -50 $(pidof dwmblocks) ;; 3) notify-send " Weather module" "\- Left click for full forecast. - Middle click to update forecast. : Chance of rain/snow ﰕ: Daily low 滛: Daily high" ;; 6) "$TERMINAL" -e "$EDITOR" "$0" ;; esac # The test if our forcecast is updated to the day. If it isn't download a new # weather report from wttr.in with the above function. [ "$(date -r "/home/yigit/.local/share/weatherreport" "+%d-%m-%Y")" = "$(date '+%d-%m-%Y')" ] || getforecast [ "$(date -r "/home/yigit/.local/share/weatherreportjson" "+%d-%m-%Y %H")" = "$(date '+%d-%m-%Y %H')" ] || getforecastjson showweather