Weather (shell)

This script could be used easily with conky or whatever you want !
- Code: Select all
#!/bin/bash
#Weather
#
#USAGE: Weather <locationcode>
#http://www.accuweather.com/ => to find your location code
#1 hour cache-validity
TIMEVALID="3600"
#Saved forecast directory
SAVEDIR="/tmp/"
#Should be 0 or 1; 0 for F, 1 for C
METRIC=1
#Connect timeout
CONTIMEOUT=5
[[ $# -ne 1 && $# -ne 2 ]] && echo "ERROR(1)" && exit 1
LOCATION=$1
[ -e ${SAVEDIR}weather ] && [ ! -w ${SAVEDIR}weather ] && echo "ERROR(2)" && exit 2
[ $# -eq 2 ] && [ $2 = "--force" ] && rm ${SAVEDIR}weather
# Anything UP except lo (lo is always UP)
if [ `ifconfig | grep -c "UP"` -gt "1" ]; then
DATENOW=`date +%s`
if [ -e ${SAVEDIR}weather ]; then
DATEFILE=`stat -c "%Y" ${SAVEDIR}weather`
else
DATEFILE=0
fi
DIFFTIME=$(($DATENOW - $DATEFILE))
if [ $DIFFTIME -le $TIMEVALID ]; then
cat ${SAVEDIR}weather && echo "(${DIFFTIME})"
else
WEATHER=`curl -s http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=${METRIC}\&locCode\=$1 --connect-timeout $CONTIMEOUT 2> /dev/null | perl -ne 'if (/Currently/) {chomp;/\<title\>Currently: (.*)?\<\/title\>/; print "$1"; }'`
if [ -z "$WEATHER" ]; then
echo "N/A"
echo -n "N/A*" > ${SAVEDIR}weather
else
echo "$WEATHER"
echo -n "$WEATHER*" > ${SAVEDIR}weather
logger -t $0 -i -p user.info "Weather for location $1 is $WEATHER."
fi
fi
else
echo "No Network detected"
#echo -n "OFF*" > ${SAVEDIR}weather
fi
[ -w ${SAVEDIR}weather ] && chmod 666 /tmp/weather* 2> /dev/null
exit 0