首页 > Script > 天气墙纸脚本:wallther

天气墙纸脚本:wallther

[总点击:1098次]
2009年5月11日

RT
点击下载 wallther 脚本及 icon 素材
看LinuxToy时发现了一个天气墙纸的程序,貌似依赖不少的Gnome组件,很不方便,于是就萌生了用bash脚本写一个相同功能的脚本的想法,经过几个小时的奋战终于搞定了,呵呵,主要是convert等处理图片的软件用得不熟。
这个东东可以自动去获取天气信息(你甚至都不用管城市代码,当然可能有些地方会不准),然后根据获得的天气找到对应的图标,然后将图标和天气情况的文字(是否绘制文字信息可选)合成到背景图片中去,然后将这个合成的图片设置为壁纸。
初次使用需要配置一些东西,主要就是字体,因为我用的雅黑你可能没有。
这里有一张生成的图片大家可以感受一下:

发件人 xiooli

代码见下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
#Copyright (c) 2009 xiooli (xioooli[at]yahoo.com.cn, http://joolix.com)
#Name wallther
#License: GPLv3
#Version 20090511
 
#此脚本需要安装 w3m 和 imagemagick
#城市代码,留空可自动检测(自动检测不一定精确)
#城市代码可在 http://weather.265.com 上查询,是个5位的数字
#受bones7456和wenbob的天气脚本启发。
 
#这儿是城市代码,须自己改,未赋值会自动查询。
Wid=56294
#天气图标的位置
Icondir="`dirname $0`/icons"
#欲用作背景的图片
BackPic="$Icondir/background.jpg"
#最终输出的图片位置
OutPic="/dev/shm/wallpapertmp.png"
#壁纸路径(此为 OutPic 的副本)
Wallpaper="/dev/shm/wallpaper.png"
#是否在图片上绘制文字天气信息,yes/no
DrawText="yes"
#文字的字体,若不是中文字体则中文可能无法正常显示
Font="/usr/share/fonts/winfonts/msyh.ttf"
#文字的大小
FontSize=24
#文字的颜色
TxtColor="white"
#文字信息绘制的位置
TxtPosX=700
TxtPosY=350
#隔多大距离绘制下一行(此距离包括本行的宽度)
TxtYIncr=35
#天气图标绘制的位置
PicGeometry="+650+80"
#壁纸更换的时间间隔(默认 30 分钟)
ChangeTime="30m"
 
WeatherCN=("晴" "多云" "阴" "雨" "雷阵雨" "雾" "雪" "雨夹雪")
WeatherEN=("sun" "suncloud" "cloud" "rain" "storm" "fog" "snow" "snowrain")
 
GET_WEATHER() {
	[ -z "${Wid}" ] && \
	if [ -f "/dev/shm/city" ] ;then
		Wid="$(cat /dev/shm/city)"
	else
		Wid="`wget -q -O - 'http://www.265.com/lookupcity'|awk -F "'" '{print $2}'`"
		echo ${Wid}>/dev/shm/city
	fi
	[ ! -z "${Wid}" ] && WeatherTxt="`w3m -dump "http://wap.weather.com.cn/wap/${Wid}/h24/"`"
}
 
GEN_DRAW_TEXT() {
	[ -z "${WeatherTxt}" ] && GET_WEATHER
	if [ -z "${WeatherTxt}" ]; then
		echo "未能获取天气 :("
	else
		echo "${WeatherTxt}"|sed -n "4p"|sed 's/\ .*$//'
		echo "${WeatherTxt}"|sed -n "5,9p"
	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}"|sed -n "5p"`"
	j=0; k=0
	for i in ${WeatherCN[@]}; do
		[ "${tmp//$i}" != "$tmp" ] && weathercn[$j]="$i" && index[$j]="$k" && ((j++))
		((k++))
	done
	[ "${#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
	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
	[ "${WeatherTxt}" != "$tmp" ] && GEN_WALLPAPWE && tmp="${WeatherTxt}"
	if [ -f "$OutPic" ];then
		mv "$OutPic" "$Wallpaper"
		gconftool-2 -s /desktop/gnome/background/picture_filename --type=string "$Wallpaper"
	fi
	sleep "$ChangeTime"
done

Script , ,

  1. daze
    2009年5月12日05:06 | #1

    看起来很不错,只是我用 fluxbox,墙纸是用 feh 设的,不知能不能直接用这个脚本,下下来慢慢看,呵呵!

  2. chisiyuan
    2009年5月12日08:15 | #2

    不错哦!

  3. 2009年5月12日08:55 | #3

    很强大啊,哈哈~脚本党又胜利了~

  4. 2009年5月16日12:34 | #4

    真是强大的说…

  5. wangtwo
    2009年5月20日16:49 | #5

    太帅了

本文的评论功能被关闭了.