hyprdots

my dotfiles
git clone https://git.awy.one/hyprdots.git
Log | Files | Refs | README | LICENSE

sb-forecast (2162B)


      1 #!/bin/sh
      2 
      3 # Displays today's precipication chance (ā˜”), and daily low (šŸ„¶) and high (šŸŒž).
      4 # Usually intended for the statusbar.
      5 
      6 url="${WTTRURL:-wttr.in}"
      7 weatherreport="${XDG_CACHE_HOME:-$HOME/.cache}/weatherreport"
      8 
      9 # Get a weather report from 'wttr.in' and save it locally.
     10 getforecast() { { grep -q -m1 '^up$' /sys/class/net/w*/operstate || grep -q -m1 '^up$' /sys/class/net/e*/operstate; } &&
     11 	curl -sf "$url/$LOCATION" --output "$weatherreport" && touch "$weatherreport"
     12 }
     13 
     14 # Forecast should be updated only once a day.
     15 checkforecast() {
     16 	[ "$(stat -c %y "$weatherreport" 2>/dev/null |
     17 		cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
     18 }
     19 
     20 getprecipchance() {
     21 	echo "$weatherdata" | sed '16q;d' |    # Extract line 16 from file
     22 		grep -wo "[0-9]*%" |           # Find a sequence of digits followed by '%'
     23 		sort -rn |                     # Sort in descending order
     24 		head -1q                       # Extract first line
     25 }
     26 
     27 getdailyhighlow() {
     28 	echo "$weatherdata" | sed '13q;d' |      # Extract line 13 from file
     29 		grep -o "m\\([-+]\\)*[0-9]\\+" | # Find temperatures in the format "m<signed number>"
     30 		sed 's/[+m]//g' |                # Remove '+' and 'm'
     31 		sort -g |                        # Sort in ascending order
     32 		sed -e 1b -e '$!d'               # Extract the first and last lines
     33 }
     34 
     35 readfile() { weatherdata="$(cat "$weatherreport")" ;}
     36 
     37 showweather() {
     38 	readfile
     39 	# shellcheck disable=SC2046,SC2183
     40 	printf "ā˜”%s šŸ„¶%sĀ° šŸŒž%sĀ°\n" "$(getprecipchance)" $(getdailyhighlow)
     41 }
     42 
     43 case $1 in
     44 	1) setsid -f "$TERMINAL" -e less -Sf "$weatherreport" ;;
     45 	2) getforecast && showweather ;;
     46 	3) notify-send "šŸŒˆ Weather module" "\- Left click for full forecast.
     47 - Middle click to update forecast.
     48 ā˜”: Chance of rain/snow
     49 šŸ„¶: Daily low
     50 šŸŒž: Daily high" ;;
     51 	8) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
     52 esac
     53 
     54 # shellcheck disable=SC2015
     55 checkforecast && showweather ||
     56 	( flock -n 9 &&
     57 		( tries=0; while [ $tries -ne 100 ]; do
     58 			getforecast && break ||
     59 				{ tries=$((tries+1)); sleep .1; }
     60 		done
     61 		! checkforecast &&
     62 			until getforecast; do sleep 60; done
     63 		pkill -RTMIN+5 "${STATUSBAR:-waybar}"
     64 		) &
     65 	echo ) 9>"${XDG_RUNTIME_DIR}/sb-forecast.lock"