dotfiles

Torpy's handcrafted dootfiles.
Log | Files | Refs | README

sb-forecast (1754B)


      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() { timeout --signal=1 2s curl -sf "$url/$LOCATION" > "$weatherreport" || exit 1; }
     11 
     12 # Forecast should be updated only once a day.
     13 checkforecast() {
     14 	[ -s "$weatherreport" ] && [ "$(stat -c %y "$weatherreport" 2>/dev/null |
     15 		cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
     16 }
     17 
     18 getprecipchance() {
     19 	echo "$weatherdata" | sed '16q;d' |    # Extract line 16 from file
     20 		grep -wo "[0-9]*%" |           # Find a sequence of digits followed by '%'
     21 		sort -rn |                     # Sort in descending order
     22 		head -1q                       # Extract first line
     23 }
     24 
     25 getdailyhighlow() {
     26 	echo "$weatherdata" | sed '13q;d' |      # Extract line 13 from file
     27 		grep -o "m\\([-+]\\)*[0-9]\\+" | # Find temperatures in the format "m<signed number>"
     28 		sed 's/[+m]//g' |                # Remove '+' and 'm'
     29 		sort -g |                        # Sort in ascending order
     30 		sed -e 1b -e '$!d'               # Extract the first and last lines
     31 }
     32 
     33 readfile() { weatherdata="$(cat "$weatherreport")" ;}
     34 
     35 showweather() {
     36 	readfile
     37 	printf "ā˜”%s 🄶%s° šŸŒž%s°\n" "$(getprecipchance)" $(getdailyhighlow)
     38 }
     39 
     40 case $BLOCK_BUTTON in
     41 	1) setsid -f "$TERMINAL" -e less -Sf "$weatherreport" ;;
     42 	2) getforecast && showweather ;;
     43 	3) notify-send "🌈 Weather module" "\- Left click for full forecast.
     44 - Middle click to update forecast.
     45 ā˜”: Chance of rain/snow
     46 🄶: Daily low
     47 šŸŒž: Daily high" ;;
     48 	6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
     49 esac
     50 
     51 checkforecast || getforecast
     52 
     53 showweather