Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- ARG_LOCATION=
- ARG_UNIT=1 #use 1 for F 0 for C
- ARG_ALL=1
- ARG_CURRENT=1
- ARG_FORECAST=0
- ARG_SUN=1
- ARG_DEFAULT_ALL=1
- help(){
- echo "Usage: [-h] [-l locations] [-u unit] [-a|-c|-d days|-s]"
- echo "-h Print this page"
- echo "-l Set locations"
- echo "-u Set unit c or C or f or F"
- echo "-a Equal to -c -d5 -s"
- echo "-c Current Condition"
- echo "-d Set forecast days"
- echo "-s Print Sunrise/Sunset times"
- }
- readconfig(){
- if [ ! -f ~/.weather.conf ]; then
- return
- fi
- ARG_LOCATION=`cat ~/.weather.conf | grep -i 'location=' | tail -n 1 | awk '{ FS = "=" } ; {print $2}'`
- if [ $(cat ~/.weather.conf | grep -i 'unit=c') ]; then
- ARG_UNIT=0
- fi
- local temp=`cat ~/.weather.conf | grep -i 'display=all' | tail -n 1 | awk '{ FS = "=" } ; {print $2}'`
- if [ "$temp" != "" ];then
- ARG_CURRENT=0
- ARG_FORECAST=5
- ARG_SUN=0
- fi
- }
- checkargs(){
- if [ ! $ARG_LOCATION ]; then
- >&2 echo "No location specified"
- exit 1
- fi
- if [ $ARG_ALL -eq 1 ] && [ $ARG_CURRENT -eq 1 ] && [ $ARG_FORECAST -eq 0 ] && [ $ARG_SUN -eq 1 ] ; then
- >&2 echo "Nothing to print"
- exit 1
- fi
- }
- getxml(){
- local queryhead="https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20"
- local querytail=")&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
- local query="text%3D%22"`echo -n $ARG_LOCATION | sed 's/,/%22%20or%20text%3D%22/'`"%22"
- local query="$queryhead$query$querytail"
- xml=`curl -sS "$query" | sed 's/yweather://'`
- }
- transtemp(){
- if [ $ARG_UNIT -eq 0 ];then
- local temp=`echo "($1-32)*5/9" | bc`
- local temp="$temp°C"
- echo -n $temp
- else
- local temp="$1°F"
- echo -n $temp
- fi
- }
- docurrent(){
- local xpath="/query/results/channel[$i]/item/condition/"
- local temp=`echo -n "$xml" |xmllint --xpath "${xpath}@temp" - | awk '{ FS = "\"" } ; { print $2 }'`
- local temp=`transtemp $temp`
- local weather=`echo -n "$xml" |xmllint --xpath "${xpath}@text" - | awk '{ FS = "\"" } ; { print $2 }'`
- echo "$loc, $weather, $temp"
- }
- doforecast(){
- for j in `seq 1 $ARG_FORECAST`; do
- local xpath="/query/results/channel[$i]/item/forecast[$j]/"
- local date=`echo -n "$xml" |xmllint --xpath "${xpath}@date" - | awk '{ FS = "\"" } ; { print $2 }'`
- local day=`echo -n "$xml" |xmllint --xpath "${xpath}@day" - | awk '{ FS = "\"" } ; { print $2 }'`
- local lowtemp=`echo -n "$xml" |xmllint --xpath "${xpath}@low" - | awk '{ FS = "\"" } ; { print $2 }'`
- local lowtemp=`transtemp $lowtemp`
- local hightemp=`echo -n "$xml" |xmllint --xpath "${xpath}@high" - | awk '{ FS = "\"" } ; { print $2 }'`
- local hightemp=`transtemp $hightemp`
- local weather=`echo -n "$xml" |xmllint --xpath "${xpath}@text" - | awk '{ FS = "\"" } ; { print $2 }'`
- echo "$date $day $lowtemp ~ $hightemp $weather"
- done
- }
- dosun(){
- local xpath="/query/results/channel[$i]/astronomy/"
- local sunrise=`echo -n "$xml" |xmllint --xpath "${xpath}@sunrise" - | awk '{ FS = "\"" } ; { print $2 }'`
- local sunset=`echo -n "$xml" |xmllint --xpath "${xpath}@sunset" - | awk '{ FS = "\"" } ; { print $2 }'`
- echo "sunrise $sunrise, sunset $sunset"
- }
- printweather(){
- ARG_LOCATION=`echo -n "$ARG_LOCATION" | tr ',' ' '`
- i=1
- for loc in ${ARG_LOCATION}
- do
- if [ $ARG_CURRENT -eq 0 ];then
- docurrent
- fi
- if [ $ARG_FORECAST -ne 0 ];then
- doforecast
- fi
- if [ $ARG_SUN -eq 0 ];then
- dosun
- fi
- i=`echo "$i + 1" | bc`
- done
- }
- readconfig
- while getopts hl:u:acd:s var ; do
- case $var in
- \?)
- exit 1
- ;;
- h)
- help
- exit 0
- ;;
- l)
- ARG_LOCATION=$OPTARG
- ;;
- u)
- if [ $OPTARG = "c" ] || [ $OPTARG = "C" ]; then
- ARG_UNIT=0
- elif [ $OPTARG != "f" ] && [ $OPTARG != "F" ];then
- exit 1
- else
- ARG_UNIT=1
- fi
- ;;
- a)
- ARG_ALL=0
- ARG_CURRENT=0
- ARG_FORECAST=5
- ARG_SUN=0
- ;;
- c)
- if [ $ARG_ALL -eq 0 ]; then
- >&2 echo "Cannot use -a with -c -d -s at same time"
- exit 1
- fi
- ARG_CURRENT=0
- ;;
- d)
- if [ $ARG_ALL -eq 0 ]; then
- >&2 echo "Cannot use -a with -c -d -s at same time"
- exit 1
- fi
- if [ $OPTARG -lt 1 ] || [ $OPTARG -gt 5 ]; then
- >&2 echo "Forecast day number need to between 1 to 5"
- exit 1
- fi
- ARG_FORECAST=$OPTARG
- ;;
- s)
- if [ $ARG_ALL -eq 0 ]; then
- >&2 echo "Cannot use -a with -c -d -s at same time"
- exit 1
- fi
- ARG_SUN=0
- ;;
- esac
- done
- checkargs
- getxml
- printweather
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement