歡迎您光臨本站 註冊首頁

用shell寫的腳本:牆紙上放天氣預報

←手機掃碼閱讀     火星人 @ 2014-03-26 , reply:0

分享一個shell腳本,功能是下載當地天氣信息,並生成含對應信息的桌面背景圖片,把它作為牆紙。
本是我從網上下的,結果不能直接用(提供天氣信息的地址我這連不上去),於是我修改了一番,可以用了。
這裡把兩個(修改前後的)都放上來。
這是我的修改後版本。附件是所用的圖片。
CODE:
#!/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的天氣腳本啟發。

#因為原有的查詢地址無效,我就做了修改,使用了www.weather.com.cn。因為天氣信息顯示情況不同,所以我花了些時間修改sed和awk的語句。
#使用的天氣圖標仍為原有的,但是背景圖片我替換成了我更喜歡的火狐背景
#alickee alick9188#163.com
#許可仍為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 "http://www.weather.com.cn/${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


icons.zip

[火星人 ] 用shell寫的腳本:牆紙上放天氣預報已經有550次圍觀

http://coctec.com/docs/linux/show-post-184478.html