#!/bin/bash # Copyright (C) 2008 Lonnie Abelbeck # This is free software, licensed under the GNU General Public License # version 3 as published by the Free Software Foundation; you can # redistribute it and/or modify it under the terms of the GNU # General Public License; and comes with ABSOLUTELY NO WARRANTY. # # weather.agi # # Speak Weather from XML data from arg 1 url # Using only asterisk "extras" sound files # # 01-06-2007, created # 03-03-2007, qualify windchill and heat_index by temp_f # 04-29-2007, require windchill_f < temp_f < heat_index_f # Usage: (Replace "KMLE" using http://www.weather.gov/data/current_obs/) #exten => s,1,Answer #exten => s,n,Playback(extras/weather) #exten => s,n,AGI(weather.agi,http://www.weather.gov/data/current_obs/KMLE.xml) #exten => s,n,Hangup # # [read AGI variables] while read -e ARG && [ "${ARG}" ] ; do :; done # ignore agi_ variables # Copy url for XML host and clean it URL=`echo $1 | sed -e 's/"//g'` # Define the location of the "extras" sound files. # Use an empty string (EXTRAS="") if they are not in a separate directory. # A person could also define a custom set of sounds just for weather.agi. EXTRAS="custom-sounds/extras/" # [results function] checkresults() { while read line do case ${line:0:4} in "200 " ) return;; "510 " ) return;; "520 " ) return;; * ) ;; #keep on reading those Invlid command #command syntax until "520 End ..." esac done } # [main] # Get XML elements # Note: the data values must be on the same line as the opening tag. # curl --silent "$URL" should also work. DATA=`wget -q -O - "$URL" | \ grep -E '(]|]|]|]|]|]|])'` if [ "${DATA}" = "" ]; then echo "STREAM FILE ${EXTRAS}were-sorry #" checkresults exit 0 fi # Define a default prefix to define any missing tags DATA="NA"`echo $DATA` # Text Values WEATHER=`echo $DATA | \ sed -e 's/^.*]//' -e 's/<.*$//' -e 's/^.*>//'` DATA=`echo $DATA | sed -e 's/ //g'` # Numeric Values (truncate floats to integers) TEMP_F=`echo $DATA | \ sed -e 's/^.*]//' -e 's/<.*$//' -e 's/\..*$//' -e 's/^.*>//'` WINDCHILL_F=`echo $DATA | \ sed -e 's/^.*]//' -e 's/<.*$//' -e 's/\..*$//' -e 's/^.*>//'` HEAT_INDEX_F=`echo $DATA | \ sed -e 's/^.*]//' -e 's/<.*$//' -e 's/\..*$//' -e 's/^.*>//'` WIND_DEGREES=`echo $DATA | \ sed -e 's/^.*]//' -e 's/<.*$//' -e 's/\..*$//' -e 's/^.*>//'` WIND_MPH=`echo $DATA | \ sed -e 's/^.*]//' -e 's/<.*$//' -e 's/\..*$//' -e 's/^.*>//'` WIND_GUST_MPH=`echo $DATA | \ sed -e 's/^.*]//' -e 's/<.*$//' -e 's/\..*$//' -e 's/^.*>//'` if [ `echo $WEATHER | grep -c -i "thunderstorm"` -ne 0 ]; then echo "STREAM FILE ${EXTRAS}thunderstorm #" checkresults sleep 1 elif [ `echo $WEATHER | grep -c -i "rain"` -ne 0 ]; then if [ `echo $WEATHER | grep -c -i "freezing"` -ne 0 ]; then echo "STREAM FILE ${EXTRAS}freezing #" checkresults fi echo "STREAM FILE ${EXTRAS}rain #" checkresults if [ `echo $WEATHER | grep -c -i "snow"` -ne 0 ]; then echo "STREAM FILE ${EXTRAS}snow #" checkresults fi sleep 1 elif [ `echo $WEATHER | grep -c -i "snow"` -ne 0 ]; then echo "STREAM FILE ${EXTRAS}snow #" checkresults sleep 1 elif [ `echo $WEATHER | grep -c -i "mist"` -ne 0 ]; then echo "STREAM FILE ${EXTRAS}wx/mist #" checkresults sleep 1 elif [ `echo $WEATHER | grep -c -i "fog"` -ne 0 ]; then echo "STREAM FILE ${EXTRAS}foggy #" checkresults sleep 1 elif [ `echo $WEATHER | grep -c -i "windy"` -ne 0 ]; then echo "STREAM FILE ${EXTRAS}windy #" checkresults sleep 1 fi if [[ "${TEMP_F:0:1}" == [-0-9] ]]; then echo "STREAM FILE ${EXTRAS}wx/temperature #" checkresults echo "SAY NUMBER ${TEMP_F} #" checkresults echo "STREAM FILE ${EXTRAS}degrees #" checkresults fi if [[ "${WINDCHILL_F:0:1}" == [-0-9] && "${TEMP_F}" -lt 50 ]]; then if [[ "${WINDCHILL_F}" -lt "${TEMP_F}" ]]; then sleep 1 echo "STREAM FILE ${EXTRAS}wx/wind-chill #" checkresults echo "SAY NUMBER ${WINDCHILL_F} #" checkresults fi elif [[ "${HEAT_INDEX_F:0:1}" == [0-9] && "${TEMP_F}" -gt 80 ]]; then if [[ "${HEAT_INDEX_F}" -gt "${TEMP_F}" ]]; then sleep 1 echo "STREAM FILE ${EXTRAS}wx/heat-index #" checkresults echo "SAY NUMBER ${HEAT_INDEX_F} #" checkresults fi fi sleep 1 # # Note: the octet of directions are not evenly spaced at 45 degrees. # North and South are 70 degrees wide # East and West are 50 degrees wide # The remaining are 30 degrees wide # # This arangement gives me a better mental image of the prevailing winds. # Different locations might want to augment this. # if [[ "${WIND_DEGREES:0:1}" != [0-9] ]]; then : # ignore NA wind direction elif [[ "${WIND_DEGREES}" -gt 35 && "${WIND_DEGREES}" -lt 65 ]]; then echo "STREAM FILE ${EXTRAS}wx/northeast #" checkresults elif [[ "${WIND_DEGREES}" -ge 65 && "${WIND_DEGREES}" -le 115 ]]; then echo "STREAM FILE ${EXTRAS}east #" checkresults elif [[ "${WIND_DEGREES}" -gt 115 && "${WIND_DEGREES}" -lt 145 ]]; then echo "STREAM FILE ${EXTRAS}wx/southeast #" checkresults elif [[ "${WIND_DEGREES}" -gt 215 && "${WIND_DEGREES}" -lt 245 ]]; then echo "STREAM FILE ${EXTRAS}wx/southwest #" checkresults elif [[ "${WIND_DEGREES}" -ge 245 && "${WIND_DEGREES}" -le 295 ]]; then echo "STREAM FILE ${EXTRAS}west #" checkresults elif [[ "${WIND_DEGREES}" -gt 295 && "${WIND_DEGREES}" -lt 325 ]]; then echo "STREAM FILE ${EXTRAS}wx/northwest #" checkresults elif [[ "${WIND_DEGREES}" -ge 90 && "${WIND_DEGREES}" -le 270 ]]; then echo "STREAM FILE ${EXTRAS}south #" checkresults else echo "STREAM FILE ${EXTRAS}north #" checkresults fi echo "STREAM FILE ${EXTRAS}wx/winds #" checkresults if [[ "${WIND_MPH:0:1}" == [0-9] ]]; then echo "SAY NUMBER ${WIND_MPH} #" checkresults else echo "SAY NUMBER 0 #" checkresults fi echo "STREAM FILE ${EXTRAS}miles-per-hour #" checkresults if [[ "${WIND_GUST_MPH:0:1}" == [0-9] ]]; then sleep 1 echo "STREAM FILE ${EXTRAS}wx/gusting-to #" checkresults echo "SAY NUMBER ${WIND_GUST_MPH} #" checkresults fi exit 0