conky 是一个轻量级的系统监视器,可以实时反映出系统的相关信息。
参考: http://www.linuxidc.com/Linux/2011-02/32508.htm
有图有真相
代码主要和参考中的一致,只是天气那块我是自己用 python 写的,因为原来的 weather.com 已经不能用了,所以也就不需要安装 conkyforecase 这个东西 。
首先当然是安装 conky:
# pacman -S conky
在 Archlinux 只需要安装 conky 这一个包,并且这个包可以支持显示图片,其他发行版可能需要装名字类似于 conky-image(也许叫这个,不一定)这个的包。
天气预报的信息是从 google 的天气 API 中得到的。数据是 XML 格式的,所以只需要调用 python 解析 XML 文件的库就可以了。
#!/usr/bin/python
import xml.dom.minidom
import urllib.request
import sys
CITY = 'Lanzhou'
URL = 'http://www.google.com/ig/api?weather=' + CITY
TMPFILE = '/tmp/myconkyweather'
XMLFILE = '/tmp/googleweather.xml'
##### get the content of the google weather page ###
response = urllib.request.urlopen(URL)
content = response.read()
f = open(XMLFILE, 'w')
f.write(str(content).lstrip('b').strip("'"))
f.close()
fd = open(TMPFILE, 'w')
##### get the root element ###
dom = xml.dom.minidom.parse(XMLFILE)
root = dom.documentElement
##### get city name ###
node = root.getElementsByTagName("city")
city = node[0].getAttribute("data")
fd.write(city + '\n')
##### get current condition ###
node = root.getElementsByTagName("condition")
cur_condition = node[0].getAttribute("data")
fd.write(cur_condition + '\n')
##### get current temperature ###
node = root.getElementsByTagName("temp_c")
cur_temp = node[0].getAttribute("data")
fd.write(cur_temp + '\n')
##### get current humidity ###
node = root.getElementsByTagName("humidity")
cur_humi = node[0].getAttribute("data")
fd.write(cur_humi + '\n')
##### get current wind speed ###
node = root.getElementsByTagName("wind_condition")
cur_wind = node[0].getAttribute("data")
fd.write(cur_wind + '\n')
##### get 4 days forecast ###
node = root.getElementsByTagName("day_of_week")
today = node[0].getAttribute("data")
the_2nd_day = node[1].getAttribute("data")
the_3rd_day = node[2].getAttribute("data")
the_4th_day = node[3].getAttribute("data")
node = root.getElementsByTagName("low")
today_low = node[0].getAttribute("data")
the_2nd_day_low = node[1].getAttribute("data")
the_3rd_day_low = node[2].getAttribute("data")
the_4th_day_low = node[3].getAttribute("data")
node = root.getElementsByTagName("high")
today_hi = node[0].getAttribute("data")
the_2nd_day_hi = node[1].getAttribute("data")
the_3rd_day_hi = node[2].getAttribute("data")
the_4th_day_hi = node[3].getAttribute("data")
node = root.getElementsByTagName("condition")
today_con = node[1].getAttribute("data")
the_2nd_day_con = node[2].getAttribute("data")
the_3rd_day_con = node[3].getAttribute("data")
the_4th_day_con = node[4].getAttribute("data")
fd.write(today + '\n' + today_low + '\n' + today_hi + '\n' + today_con + '\n')
fd.write(the_2nd_day + '\n' + the_2nd_day_low + '\n' + the_2nd_day_hi + '\n' + the_2nd_day_con + '\n')
fd.write(the_3rd_day + '\n' + the_3rd_day_low + '\n' + the_3rd_day_hi + '\n' + the_3rd_day_con + '\n')
fd.write(the_4th_day + '\n' + the_4th_day_low + '\n' + the_4th_day_hi + '\n' + the_4th_day_con + '\n')
fd.close()
上面这个脚本叫做 weather.py,从 google 获得天气的 XML 文件,然后用 DOM tree 的方法去读这个 XML 的内容,并从中抽取有用的信息。
#!/usr/bin/python
import sys, os
TMPFILE = '/tmp/myconkyweather'
IMAGEPATH = '/home/shyodx/.conkygreen/images/weathericons/'
conditions_text = {
"Tornado" : IMAGEPATH + '00.png',
"Tropical Storm" : IMAGEPATH + '01.png',
"Hurricane" : IMAGEPATH + '02.png',
"Severe Thunderstorms" : IMAGEPATH + '03.png',
"Thunderstorms" : IMAGEPATH + '04.png',
"Mixed Rain and Snow" : IMAGEPATH + '05.png',
"Mixed Rain and Sleet" : IMAGEPATH + '06.png',
"Mixed Precipitation" : IMAGEPATH + '07.png',
"Freezing Drizzle" : IMAGEPATH + '08.png',
"Drizzle" : IMAGEPATH + '09.png',
"Freezing Rain" : IMAGEPATH + '10.png',
"Light Rain" : IMAGEPATH + '11.png',
"Rain" : IMAGEPATH + '12.png',
"Snow Flurries" : IMAGEPATH + '13.png',
"Light Snow Showers" : IMAGEPATH + '14.png',
"Drifting Snow" : IMAGEPATH + '15.png',
"Snow" : IMAGEPATH + '16.png',
"Chance of Snow" : IMAGEPATH + '16.png',
"Hail" : IMAGEPATH + '17.png',
"Sleet" : IMAGEPATH + '18.png',
"Dust" : IMAGEPATH + '19.png',
"Fog" : IMAGEPATH + '20.png',
"Haze" : IMAGEPATH + '21.png',
"Mostly Sunny" : IMAGEPATH + '21.png',
"Smoke" : IMAGEPATH + '22.png',
"Blustery" : IMAGEPATH + '23.png',
"Windy" : IMAGEPATH + '24.png',
"N/A" : IMAGEPATH + '25.png',
"Cloudy" : IMAGEPATH + '26.png',
# "Mostly Cloudy" : IMAGEPATH + '27.png',
"Mostly Cloudy" : IMAGEPATH + '28.png',
# "Partly Cloudy" : IMAGEPATH + '29.png',
"Partly Cloudy" : IMAGEPATH + '30.png',
# "Clear" : IMAGEPATH + '31.png',
"Clear" : IMAGEPATH + '32.png',
# "Fair" : IMAGEPATH + '33.png',
"Fair" : IMAGEPATH + '34.png',
"Partly Sunny" : IMAGEPATH + '34.png',
"Mixed Rain and Hail" : IMAGEPATH + '35.png',
"Hot" : IMAGEPATH + '36.png',
"Isolated Thunderstorms": IMAGEPATH + '37.png',
"Scattered Thunderstorms": IMAGEPATH + '38.png',
"Scattered Showers" : IMAGEPATH + '39.png',
"Heavy Rain" : IMAGEPATH + '40.png',
"Scattered Snow Showers": IMAGEPATH + '41.png',
# "Heavy Snow" : IMAGEPATH + '42.png',
"Heavy Snow" : IMAGEPATH + '43.png',
"N/A" : IMAGEPATH + '44.png',
# "Scattered Showers" : IMAGEPATH + '45.png',
"Snow Showers" : IMAGEPATH + '46.png',
"Isolated Thunderstorms": IMAGEPATH + '47.png'
}
fd = open(TMPFILE, 'r')
weather_dict = {
'location' : fd.readline().rstrip('\n'),
'cur_cond' : fd.readline().rstrip('\n'),
'cur_temp' : fd.readline().rstrip('\n') + ' °C',
'cur_humi' : fd.readline().rstrip('\n'),
'cur_wind' : fd.readline().rstrip('\n'),
'today' : fd.readline().rstrip('\n'),
'today_lo' : fd.readline().rstrip('\n'),
'today_hi' : fd.readline().rstrip('\n'),
'today_cond' : fd.readline().rstrip('\n'),
'secd_day' : fd.readline().rstrip('\n'),
'secd_day_lo' : fd.readline().rstrip('\n'),
'secd_day_hi' : fd.readline().rstrip('\n'),
'secd_day_cond' : fd.readline().rstrip('\n'),
'thrd_day' : fd.readline().rstrip('\n'),
'thrd_day_lo' : fd.readline().rstrip('\n'),
'thrd_day_hi' : fd.readline().rstrip('\n'),
'thrd_day_cond' : fd.readline().rstrip('\n'),
'foth_day' : fd.readline().rstrip('\n'),
'foth_day_lo' : fd.readline().rstrip('\n'),
'foth_day_hi' : fd.readline().rstrip('\n'),
'foth_day_cond' : fd.readline().rstrip('\n'),
'cur_cond_icon' : None,
'today_cond_icon' : None,
'secd_day_cond_icon' : None,
'thrd_day_cond_icon' : None,
'foth_day_cond_icon' : None
}
fd.close()
tmplist = ('cur_cond', 'today_cond', 'secd_day_cond',
'thrd_day_cond', 'foth_day_cond')
for tmp in tmplist:
weather_dict[tmp + '_icon'] = conditions_text[weather_dict[tmp]]
switchlist = ('today_lo', 'today_hi', 'secd_day_lo', 'secd_day_hi',
'thrd_day_lo', 'thrd_day_hi', 'foth_day_lo', 'foth_day_hi')
for tmp in switchlist:
fahrenhite = int(weather_dict[tmp])
celsius = 5 * (fahrenhite - 32) // 9
weather_dict[tmp] = str(celsius) + '°C'
os.system('cp ' + weather_dict['cur_cond_icon'] + ' /tmp/cur_cond.png')
os.system('cp ' + weather_dict['today_cond_icon'] + ' /tmp/today_cond.png')
os.system('cp ' + weather_dict['secd_day_cond_icon'] + ' /tmp/secd_day_cond.png')
os.system('cp ' + weather_dict['thrd_day_cond_icon'] + ' /tmp/thrd_day_cond.png')
os.system('cp ' + weather_dict['foth_day_cond_icon'] + ' /tmp/foth_day_cond.png')
for key in weather_dict:
if sys.argv[1] == key:
print(weather_dict[key])
以上这个脚本叫做 getweather.py,读存放在 /tmp 下的 myconkyweather 文件,并根据用户指定的命令行参数,打印出相应的信息。并把当前使用到的天气情况的相关图片放在 /tmp 下,并指定好图片的名字,方便 conky 配置文件使用。命令行参数是 weather_dict 的索引部分。conditions_text 指出各种天气对应的图片的路径和名称。
这样,在 conky 的配置文件中,只需要按照如下的格式写,就可以得到天气的相关信息了。
${execi 10800 python /home/shyodx/.conkygreen/weather.py}
${color white}${font :bold:size=12}WEATHER $font$color $alignr ${execi 1080 python /home/shyodx/.conkygreen/getweather.py location}
${image /tmp/cur_cond.png -p 127,624 -s 85x85}
${font :bold:size=9}Condition: $font$alignr${execi 1080 python /home/shyodx/.conkygreen/getweather.py today_cond}
${font :bold:size=9}Temperature: $font$alignr${execi 1080 python /home/shyodx/.conkygreen/getweather.py cur_temp}
${font :bold:size=9}Humidity: $font$alignr${execi 1080 python /home/shyodx/.conkygreen/getweather.py cur_humi}
${font :bold:size=9}Wind speed: $font$alignr${execi 1080 python /home/shyodx/.conkygreen/getweather.py cur_wind}
我的配置文件已经上传到 http://115.com/file/bep46gv1#myconky.tar.gz2。其中包括我所用到的几个 conky 配置文件,也包括天气预报的两个 python 脚本和天气情况的图片。
配置文件和脚本中的有些路径可能需要修改成你自己的,配置文件中的一些布局可能也需要针对不同的显示情况修改。