脚本显示当地天气信息"/>
一个shell脚本显示当地天气信息
一个shell脚本,功能是下载当地天气信息,并生成含对应信息的桌面背景图片,把它作为墙纸。- #!/bin/bash
- #Copyright (c) 2009 xiooli (xioooli[at]yahoo, )
- #Name wallther
- #License: GPLv3
- #Version 20090511
- #此脚本需要安装 w3m 和 imagemagick
- #城市代码,留空可自动检测(自动检测不一定精确)
- #城市代码可在 上查询,是个5位的数字
- #受bones7456和wenbob的天气脚本启发。
- #因为原有的查询地址无效,我就做了修改,使用了www.weather。因为天气信息显示情况不同,所以我花了些时间修改sed和awk的语句。
- #使用的天气图标仍为原有的,但是背景图片我替换成了我更喜欢的火狐背景
- #alickee alick9188#163
- #许可仍为GPLv3
- #10/31/2009
- #这儿是城市代码,须自己改,未赋值会自动查询。暂时看来没什么用了。
- #由于更换了查询网站,这个代号就用不上了
- Wid=54511
- #地点拼音
- place=beijing
- #地点名称,后跟空格(用于匹配)
- place_cn="北京 "
- #天气图标的位置
- Icondir="/home/alickee/Program/shell_script/icons"
- #欲用作背景的图片
- BackPic="$Icondir/background.jpg"
- #最终输出的图片位置
- OutPic="/dev/shm/wallpapertmp.png"
- #壁纸路径(此为 OutPic 的副本)
- Wallpaper="/dev/shm/wallpaper.png"
- #是否在图片上绘制文字天气信息,yes/no
- DrawText="yes"
- #文字的字体,若不是中文字体则中文可能无法正常显示
- Font="/usr/share/fonts/zh_CN/TrueType/zysong.ttf"
- #文字的大小
- FontSize=24
- #文字的颜色
- TxtColor="white"
- #文字信息绘制的位置
- TxtPosX=700
- TxtPosY=350
- #隔多大距离绘制下一行(此距离包括本行的宽度)
- TxtYIncr=35
- #天气图标绘制的位置
- PicGeometry="+650+75"
- #壁纸更换的时间间隔(默认 30 分钟)
- ChangeTime="30m"
- WeatherCN=("晴" "多云" "阴" "雨" "雷阵雨" "雾" "雪" "雨夹雪")
- WeatherEN=("sun" "suncloud" "cloud" "rain" "storm" "fog" "snow" "snowrain")
- GET_WEATHER() {
- echo "获取天气中"
- #将获取天气信息的地址修改了
- WeatherTxt="`w3m -dump "/${place}/index.shtml" \
- | grep "${place_cn}" | head -n 1 `"
- }
- GEN_DRAW_TEXT() {
- [ -z "${WeatherTxt}" ] && GET_WEATHER
- if [ -z "${WeatherTxt}" ]; then
- echo '未能获取天气 :( '
- else
- echo "${WeatherTxt}" | sed 's/[ ][ ]*/ /g' | awk '{print $1 " " $2 " " $3 "℃" "\n" $4 "风" $5 "\n降水" $6 " 湿度" $7}'
- fi \
- |sed "s/^.*$/-draw \\\'text POSITION \\\"&\\\"\\\'/" \
- |while read line; do
- echo "$line"|sed "s/POSITION/$TxtPosX,$TxtPosY/"
- ((TxtPosY+=$TxtYIncr))
- done|tr "\n" " "
- }
- GEN_WEATHER_ICON() {
- local tmp weathercn index
- [ -z "${WeatherTxt}" ] && GET_WEATHER
- [ -z "${WeatherTxt}" ] || tmp="`echo "${WeatherTxt}" | awk '{print $2}'`"
- j=0; k=0
- for i in ${WeatherCN[@]}; do
- [ "${tmp//$i}" != "$tmp" ] && weathercn[$j]="$i" && index[$j]="$k" && ((j++))
- ((k++))
- done
- # ${#parameter}
- # The length in characters of the value of parameter is substi-
- # tuted. If parameter is * or @, the value substituted is the
- # number of positional parameters. If parameter is an array name
- # subscripted by * or @, the value substituted is the number of
- # elements in the array.
- #调试时输出weathercn数组元素的个数
- #echo "weathercn num=" "${#weathercn[@]}"
- [ "${#weathercn[@]}" -eq 0 ] && Weather[0]="unknown"
- [ "${#weathercn[@]}" -eq 1 ] && Weather[0]="${WeatherEN[${index[0]}]}"
- [ "${#weathercn[@]}" -eq 2 ] && \
- if [ "`echo $tmp|grep "${weathercn[0]}转"`" ];then
- Weather[0]="${WeatherEN[${index[0]}]}"
- Weather[1]="${WeatherEN[${index[1]}]}"
- else
- Weather[0]="${WeatherEN[${index[1]}]}"
- Weather[1]="${WeatherEN[${index[0]}]}"
- fi
- [ "${#weathercn[@]}" -eq 3 ] && \
- {
- Weather[0]="${WeatherEN[${index[0]}]}"
- Weather[1]="${WeatherEN[${index[2]}]}"
- }
- if [ "${#Weather[@]}" -ge 2 ]; then
- convert +append "$Icondir/${Weather[0]}.png" "$Icondir/${Weather[1]}.png" /dev/shm/weathericon.png
- #因为生成的图片宽度变为原来的两倍,故再更改下大小
- convert -resize 50%x100% /dev/shm/weathericon.png /dev/shm/weathericon.tmp.png
- mv /dev/shm/weathericon.tmp.png /dev/shm/weathericon.png
- else
- ln -sf "$Icondir/${Weather[0]}.png" /dev/shm/weathericon.png
- fi
- }
- GEN_WALLPAPWE() {
- GEN_WEATHER_ICON
- [ -f $BackPic ] || BackPic="$Icondir/background.jpg"
- if [ "$DrawText" = "yes" ]; then
- draw="convert -font \"$Font\" -fill $TxtColor -pointsize $FontSize `GEN_DRAW_TEXT` \"$BackPic\" \"/dev/shm/backpictmp.png\""
- eval "$draw"
- composite -geometry "$PicGeometry" /dev/shm/weathericon.png /dev/shm/backpictmp.png "$OutPic"
- else
- composite -geometry "$PicGeometry" /dev/shm/weathericon.png "$BackPic" "$OutPic"
- fi
- }
- while :; do
- GET_WEATHER
- echo "站名 天气 气温 风向 风速 降水 湿度"
- echo "$WeatherTxt"
- GEN_WALLPAPWE
- #下面一行显示通知区域小图标
- #zenity --notification --window-icon=/usr/share/icons/gnome/48x48/filesystems/gnome-fs-bookmark.png --timeout=30 --text="${WeatherTxt}"
- if [ -f "$OutPic" ];then
- mv "$OutPic" "$Wallpaper"
- #更换桌面背景
- gconftool-2 -s /desktop/gnome/background/picture_filename --type=string "$Wallpaper"
- fi
- sleep "$ChangeTime"
- done
更多推荐
一个shell脚本显示当地天气信息
发布评论