From hekuran, 2 Weeks ago, written in Bash.
Embed
  1. #!/bin/sh
  2.  
  3. # Shows the current moon phase.
  4.  
  5. city="/germany"
  6. state="/berlin"
  7. website="https://www.timeanddate.com/moon"
  8. moondata="${XDG_CACHE_HOME:-$HOME/.cache}/moondata"
  9. moonfile="${XDG_DATA_HOME:-$HOME/.local/share}/moonfile"
  10. moonpng="${XDG_DATA_HOME:-$HOME/.local/share}/moon.png"
  11.  
  12. getmoon() {
  13. curl -sf "${website}${state}${city}" | \
  14.         grep -o -m1 "<img id="cur-moon".\{900\}" | \
  15.                 sed 's|<th>|<th>\n|g' |\
  16.                 sed -n "1p ; 3p ; 8p"  > "$moondata" #| sed '2,3s .\{20\}  '
  17.  
  18. grep -o 'New\|Waxing Crescent\|First Quarter\|Waxing Gibbous\|Full\|Waning Gibbous\|Last Quarter\|Waning Crescent' "$moondata" | \
  19.         grep -m1 ".*" > "$moonfile"
  20. grep -o "[0-9.0-9]*%" "$moondata" >> "$moonfile"
  21. printf "%s %s\n" $(grep -o "direction.\{10\}" "$moondata" | sed 's/direction // ; s|">.*||') \
  22.         $(grep -o "[0-9]*°" "$moondata") >> "$moonfile"
  23. grep -o "[[:digit:]]""[[:digit:]]":"[[:digit:]]""[[:digit:]]" "$moondata" >> "$moonfile"
  24.  
  25. # Get Picture/Icon
  26. curl -sf "${website%/*}$(grep -o "/scripts/moon.php.*r=.\\...." ${moondata})" > "$moonpng"
  27. }
  28.  
  29. [ "$(stat -c %y "$moondata" 2>/dev/null | cut -d':' -f1)" = "$(date '+%Y-%m-%d %H')" ] ||
  30.         { getmoon || exit 1 ;}
  31.  
  32. mnphs="$(sed -n 1p "$moonfile")"
  33.  
  34. case "$mnphs" in
  35.         "New")                          icon="?" ;;
  36.         "Waxing Crescent")      icon="?" ;;
  37.         "First Quarter")        icon="?" ;;
  38.         "Waxing Gibbous")       icon="?" ;;
  39.         "Full")                         icon="?" ;;
  40.         "Waning Gibbous")       icon="?" ;;
  41.         "Last Quarter")         icon="?" ;;
  42.         "Waning Crescent")      icon="?" ;;
  43.         *) exit 1 ;;
  44. esac
  45.  
  46. case $BLOCK_BUTTON in
  47.         3) notify-send -i "$moonpng" "$(cat "$moonfile")" ;;
  48.         6) "$TERMINAL" -e "$EDITOR" "$0" ;;
  49. esac
  50.  
  51. printf "%s%s\n" "$icon" "$( sed -n 2p "$moonfile" )"