Skip to content

Instantly share code, notes, and snippets.

@PhirePhly
Created January 20, 2012 04:54
Show Gist options
  • Save PhirePhly/1645297 to your computer and use it in GitHub Desktop.
Save PhirePhly/1645297 to your computer and use it in GitHub Desktop.
Hourly script to crawl weather.gov and send Prowl push alerts for severe weather
#!/bin/sh
# Kenneth Finnegan, 2012
# kennethfinnegan.blogspot.com
#
# Query weather.gov and send severe weather alerts via Prowl
#
# To have run hourly by cron:
# 00 * * * * /mnt/storage/scripts/weatheralert.sh
#
# 2012 01 18: Initial revision
# 2012 01 19: Added support for alert URL
# Change queryWeather() to use $3 as APIKEY
# Pull in definition for personal Prowl APIKEY
source /mnt/storage/scripts/variables
# Call with weather.gov zone/county code, name string, and
# comma-seperated list of interested APIKEYs, so i.e.
# "CAC113" "Yolo" "XXXX,YYYY"
# sends Yolo alerts to both user XXXX and YYYY
# Zone codes can be found: http://alerts.weather.gov/
queryWeather () {
if [ -z "$1" ]; then
echo "ERROR: zero length zone code"
exit 1
fi
if [ -z "$2" ]; then
echo "ERROR: zero length zone name"
exit 1
fi
if [ -z "$3" ]; then
echo "ERROR: zero length API keys"
exit 1
fi
# Backup the old alert summary
mv /tmp/weather.$1.txt /tmp/weather.$1.old.txt
# Build the URL for the alert XML based on zone code
ALERTURL="http://alerts.weather.gov/cap/wwaatmget.php?x=$1"
# Note that ... seperator between sentences is sometimes mangled
# which is why the awk field seperator is so crazy
curl -s "$ALERTURL" | tee /tmp/weather.$1.full.txt | \
grep '<summary>' | \
awk -F '\.\.\.|>\.|</s' '{print $2}' | \
tr -d '.' >/tmp/weather.$1.txt
# Check to make sure there's a warning at all
if [ -z "`cat /tmp/weather.$1.txt`" ] ; then
return 0
fi
# Check to see if the alert has changed
if ! diff /tmp/weather.$1.old.txt /tmp/weather.$1.txt ; then
# Pull event title out of cap:event tag
EVENT="`grep 'cap:event' < /tmp/weather.$1.full.txt \
| awk -F '<|>' '{print $3}' | head -n 1`"
# Magic curl call to Prowl API
curl https://api.prowlapp.com/publicapi/add \
-F apikey="$3" \
-F priority="1" \
-F url="$ALERTURL" \
-F application="$2" \
-F event="$EVENT" \
-F description="`cat /tmp/weather.$1.txt`"
fi
}
# Now call queryWeather with every zone code & name pair of interest to APIKEY
queryWeather "CAC113" "Yolo" "$APIKEY"
queryWeather "CAC085" "Santa Clara" "$APIKEY"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment