获取天气脚本

放到了google code(http://code.google.com/p/get-weather/)上了,但是google code被墙,无法直接使用git管理,唉。

#! /bin/bash

#

# Get weather.raychou.com's rss result

#

# usage: ./getweather [CITYID] [DAYS]

#

# updated by comet on 2011-07-28

 

SHPATH="/root/shells"

TASKLOG="${SHPATH}/task.log"

ALERTLOG="${SHPATH}/alert.log"

SHTIME=`date +%F %T`

 

#curl --connect-timeout 5 -s -S

#s: slient , S: Show error

CURL=`which curl`

USERAGENT='Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.6) Gecko/20110728 Ubuntu/10.04 (lucid) Firefox/5.0'

DAYS=3

 

#-n: length not null

if [ -n "$1" ]; then

case "$1" in

gz)

CITYID=59287;;

nh)

CITYID=59288;;

zj)

CITYID=59658;;

lz)

CITYID=59750;;

*)

CITYID="$1";;

esac

else

CITYID=59287

fi

 

# DAYS max is 7, default is 3 .

if [ -n "$2" ]; then

# $2 is integer or not

ISDAY=`echo "$2"|sed 's/[0-9]//g'|wc -c`

if  [ "${ISDAY}" -eq 1 ] && [ "$2" -ge 1 ] && [ "$2" -le 7 ]; then

DAYS=$2

fi

fi

 

CITYURL="http://weather.raychou.com/?/detail/${CITYID}/rss"

 

# sed  must be with curl result in one line, otherwise will not work.

# delete html tag, TAB(ctrl+v+tab), alpha AND numeric, numeric-date, FREE AD,rn

FIXSTRING=`"${CURL}" -A "${USERAGENT}" "${CITYURL}"|sed 's/<[^>]*>//g;s/t//g;/[:alnum:]/d;/^[0-9]{5}/d;7d'|tr 'rn' ' '`

#FIXSTRING=`cat rss|sed 's/<[^>]*>//g;s/t//g;/[:alnum:]/d;/^[0-9]{5}/d;7d'|tr 'rn' ' '`

 

WEATHER=(`echo ${FIXSTRING}`)

if [ 0 -eq ${#WEATHER[@]} ]; then

WS="Fail :-("

else

case "${DAYS}" in

1)

COUNTS="2";;

2)

COUNTS="2 7";;

4)

COUNTS="2 7 12 17";;

5)

COUNTS="2 7 12 17 22";;

6)

COUNTS="2 7 12 17 22 27";;

7)

COUNTS="2 7 12 17 22 27 32";;

*)

COUNTS="2 7 12";;

esac

for i in ${COUNTS}

do

if [ -z "${WS}" ]; then

WS=${WEATHER[i]}","${WEATHER[i+1]}","${WEATHER[i+2]}

else

WS=${WS}"."${WEATHER[i]}","${WEATHER[i+1]}","${WEATHER[i+2]}

fi

done

WS=${WEATHER[0]}" "${WEATHER[1]}","${WS}

fi

 

echo ${WS}

 

exit 0

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据