ow 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env bash
  2. # drobban@sdfeu
  3. source '../say.sh'
  4. nick="$1"
  5. chan="$2"
  6. arg="${*:3}"
  7. site="${arg// /}"
  8. function get {
  9. awk -v n="${1}" 'BEGIN { FS=n"\":" }; { split($2,s,",\"");
  10. gsub(/"/,"",s[1]); gsub(/}/,"",s[1]); print s[1] }' <<< "$report"
  11. }
  12. if [[ -n "$site" && ! "$site" =~ [^[:alpha:],] ]]; then
  13. url="api.openweathermap.org/data/2.5/weather?q=$site&units=metric&appid=ef4b86aedd2b2645317ac3b7a22e56f4"
  14. report="$(curl -s "$url")"
  15. if [[ "$(get 'cod')" = '200' ]]; then
  16. city="$(get 'name')"
  17. country="$(get 'country')"
  18. temp="$(get 'temp')"
  19. humid="$(get 'humidity')"
  20. wind="$(get 'speed')"
  21. cond="$(get 'description')"
  22. msg="Weather for $city, $country: $temp C, $humid% humidity, $cond, $wind m/s wind"
  23. else
  24. msg='The requested location is not in the database or the server is down.'
  25. fi
  26. else
  27. chan="$nick"
  28. msg=(
  29. 'The command !ow prints observations reported to http://openweathermap.org'
  30. 'Argument format: <city name>[, <country code>]'
  31. 'Use ISO 3166-2 country codes: https://en.wikipedia.org/wiki/ISO_3166'
  32. )
  33. fi
  34. say "$chan" "${msg[@]}"