weather 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env bash
  2. # mlaine@sdfeu
  3. source '../say.sh'
  4. nick="$1"
  5. chan="$2"
  6. arg="$3"
  7. if [[ "${#arg}" -eq 4 && ! "$arg" =~ [^[:alnum:]] ]]; then
  8. site="${arg^^}"
  9. url="ftp://tgftp.nws.noaa.gov/data/observations/metar/decoded/$site.TXT"
  10. report="$(curl --connect-timeout 5 -s "$url")"
  11. if [[ -z "$report" ]]; then
  12. msg='The requested location is not in the database or the server is down.'
  13. else
  14. if [[ "$report" =~ "($site)" ]]; then
  15. location="$(egrep "\($site\)" <<< "$report")"
  16. else
  17. location="$site"
  18. fi
  19. weather="$(
  20. egrep 'Temperature:|Relative Humidity:|Weather:|Sky conditions:' <<< "$report"
  21. )"
  22. printf -v msg '%s\n%s' "Location: $location" "$weather"
  23. readarray -t msg <<< "$msg"
  24. fi
  25. else
  26. chan="$nick"
  27. msg=(
  28. 'The command !weather prints NWS METAR observations for the requested location (1st arg).'
  29. 'You must use the ICAO location format, see https://www.notams.faa.gov/common/icao/ for a list.'
  30. 'Observations are available for locations listed in http://tgftp.nws.noaa.gov/data/observations/metar/decoded/.'
  31. )
  32. fi
  33. say "$chan" "${msg[@]}"