现在的位置: 首页 > 操作系统 > 正文

conky配置(转自forum.ubuntu.org.cn)

2019年10月02日 操作系统 ⁄ 共 24282字 ⁄ 字号 评论关闭

原文章地址:http://forum.ubuntu.org.cn/viewtopic.php?t=372261

Conky是一个建设在X-window之上的系统监视工具,它占用系统资源非常小而功能却非常强大,如果你懂得shell编程,那么conky的功能可

以说是无所不能了。同时,Conky是在根窗口上绘制界面,因此本身就支持透明,可以和桌面完美地结合在一起,如果你愿意,Conky也可以以窗口的形式

运行。

当然,即使你不懂shell,根据下面的教程,你也能一步步学会使用conkey。

1、获取conky。

[mw_shl_code=shell,true]sudo apt-get install conky[/mw_shl_code]

2、配置conky

Conky 安装后不会为当前用户创建配置文件,而是使用系统级默认配置文件,这时候需要使用下列命令手动生成当前用户的配置

[mw_shl_code=shell,true]zcat /usr/share/doc/conky/examples/conkyrc.sample.gz > ~/.conkyrc[/mw_shl_code]

但 Conky 默认的配置也许并不能满足我们的需要,可以根据自己的需要去掉了一些不适合的元素,增加了几个需要的参数。

首先,Conky的配置文件分为两个部分,第一部分对Conky的全局属性做设定,比如字体、嵌入桌面等等;第二部分是定义Conky的输出格式,也就是Conky呈现在桌面上的样式。

background no:是否嵌入桌面,“no”表示不嵌入;“yes”表示嵌入。

use_xft yes:是否使用xft字体(全局字体),一般为“yes”。

定义字体:使用 xftfont 温泉驿微米黑:size=8 这行可以定义 Conky 的全局字体,其格式为字体名称:大小,也可使用字体样式,如粗体可用 style=Bold 来表示。

out_to_console no:是否输出结果到终端,主要在判断Conky在哪里出错时使用,一般情况下为“no”即可。

显示位置: gap_y 10:以上两条设定Conky输出范围距离屏幕边界的像素数量,最小为10像素。 alignment top_right:设定Conky输出范围在屏幕上的位置,“top_right”表示右上方,依此类推,“bottom_left”是左下角。

大小及边框:分别由 minimum_size 和 border_width 来确定,前者的设置格式为宽x高,不要边框的话,可将后者设置为 0。如minimum_size 280 5:设定Conky的边界范围,最小宽度为280像素,最小高度为5像素。maximum_width 308:Conky边界范围最大宽度308像素,很多时候用这个参数来限制Conky的边界。

显示信息:在 Conky 配置文件的最后部分可以定义具体显示什么内容。这里主要是了解一些变量的使用,比如,显示系统从开机到现在所运行的总共时间,可以使用 uptime 变量,变量前面要用 $ 表示。更多变量的含义可以查看 Conky 主页的 Conky Variables。

update_interval 1:刷新时间。Conky需要每隔一段时间读取一次要监视的系统状态并把结果输出到屏幕上,设定的时间间隔越短,监控的实时性越好,但系统负担也越重;间隔时间越长则系统负担越轻,但是监控的实时性越差。我这里设定为1秒。

使用温泉驿作全局字体的好处就是 可以防止中文乱码。如果你在配置好之后发现很多口口,那就是字体设置出错了,更改以${font 开头 后面的字体样式可以解决乱码问题。不过很遗憾的发现在linux的中文字体中很少 支持特殊字符的,比如℃。由于这个原因在后面,我配置天气里浪费了不少功夫。


Conky样式定义的语法和编写网页有点相似,首先,以字符“TEXT”标志样式定义的开始。Conky样式的定义以行为单位,每一行对应Conky输出到屏幕时的一行。 每一行可以分为样式属性、文字和元素。其中样式属性和元素都以“$”开头,并包含在“{}”里面,“${}”应该放在应用目标的前面。

样式属性如颜色、字体大小、停靠位置等,其中,颜色的定义格式为:${color #5000a0}

其中的16位值色可以用X中定义的颜色名称代替,如:${color snow}

你可以在/etc/X11/rgb.txt中找到全部颜色的列表。

字体属性的定义格式如下:${font Monospace:style=Bold:size=10}

上面三个字体属性中任何一个不设定则使用上面定义的默认值。

${alignr}表示以该参数定义的元素向右对齐,${alignl}表示左对齐,${alignc}表示居中对齐。

需要注意的是,${}中定义的颜色和字体属性将一直影响到Conky配置文件的最后,即如果你在前面定义了第一行的颜色为“white”,那么以后各行的颜色都是白色。所以原则上你需要为每一行甚至每一行的各个元素设定颜色或者字体属性。

文字即Conky显示在屏幕上的文字,它不需要特别的定义,你希望Conky显示什么文字就添加什么文字即可。

元素即呈现在屏幕上的那些进度条、你要监视的系统状态等等,格式为:${监视目标 参数}

如果没有参数,也可以直接使用 $监视目标 的格式。


###############################TEXT ##################################################

下面介绍一些常用的元素和其参数的格式:

1、time:采用strftime的参数格式,其参数有好几个,样例:${color white}${font 文泉驿微米黑:style=Bold:size=12}${time %b%d日 星期%a}${alignr}${time %p%l:%M:%S}

即输出字体为文泉驿微米黑、字体大小为12像素、粗体、颜色为白色,依次输出月、日、星期几,后面以12小时格式输出时间并向右对齐。

2、nodename:本机在网络上的名称。

sysname:如Linux。

kernel:内核版本。

machine:硬件系统类型,如i686。

3、stippled_hr:在本行余下的空间输出虚线,多用于分隔区块。

4、uptime:系统持续运行时间。

5、cpu:CPU实时占用百分比。

cpubar:CPU占用的进度条样式。

cpugraph:CPU占用的频谱图样式。该元素可以加两个颜色值作参数,实现颜色渐变:${cpugraph 000000 5000a0}

6、mem:内存实时占用大小。

memmax:内存总量。

memperc:内存实时占用百分比。

swap、swapmax、swapperc是虚拟内存的相应参数。

7、processes:正在运行的进程数。

running_processes:活跃的进程数。

8、addr ppp0:显示ADSL拨号建立的连接的IP地址,“addr eth0”表示显示第一块网卡的IP地址。

9、offset 70:表示相对于当前位置向右偏移70像素。

10、downspeed eth0:第一块网卡下载数据的实时速度。

upspeed eth0:第一块网卡上传数据的实时速度。

downspeedgraph eth0 32,150 ff0000 0000ff:以频谱图的样式显示第一块网卡下载速度,高度为32像素,宽度为150像素,后面是两种颜色,用于渐变色。

upspeedgraph eth0 …:用法同上。

11、fs_used /home:显示挂载到/home文件夹下的硬盘分区的使用量。

fs_size /home:显示挂载到/home文件夹下的硬盘分区的总量。

fs_bar /home:以进度条样式显示挂载到/home文件夹下的硬盘分区的占用量。

12、top name 1:按CPU占用从大到小排序当前进程并显示第一个的名字。

top mem 2:按CPU占用从大到小排序当前进程并显示第二个的内存占用量。

top pid 3:按CPU占用从大到小排序当前进程并显示第三个的pid号。

top cpu 4:按CPU占用从大到小排序当前进程并显示第四个的CPU占用量。

top_mem按内存占用从大到小排序当前进程。

13、mpd_status:监视mpd的状态。

mpd_elapsed:歌曲的已播放时间。

mpd_length:当前播放歌曲的长度。

mpd_bar:当前播放歌曲的进度条。

mpd_smart:智能判断当前歌曲的输出信息,如果id3可用,则输出”歌手-歌名“的格式,如果id3不可用,则输出当前歌曲的文件名。

除mpd外,Conky支持对audacious、bmp、xmms的监视。

14、rss:订阅一个feed并显示其最新的几个条目的标题等信息,如

${rss http://feed.feedsky.com/my_cnbeta 5 item_titles 10}

即每隔五分钟获取该源的最新条目,并输出最新的十个条目的标题。

15、tcp_portmon:监视当前使用的端口,目前只支持IPv4。使用格式为:

tcp_portmon port_begin port_end item (index)

16、exec:执行一个shell命令并把结果输出到屏幕上。后面天气获取的三种方法中有两个用到这个函数。

17、acpitemp:监视并输出CPU温度,摄氏温标。Conky支持很多种CPU温度监视方案。不过acpitemp不支持多核,要显示另一个核心的温度得用 hwmon temp 2

18、diskio:监视当前硬盘读写速度。 

19、diskiograph:以频谱形式输出硬盘读写频度,用法如cpugraph。


#######################################################################################


我所做的就是收集conky各种主题,选择自认为实用好看的part,截选、组合、更改和调配,达到自己满意的效果。


这里主要介绍天气获取和时钟轮(网速监测)实现的方法与过程。

##################################mark#########################################

最先找到的是一段监测有线网卡的主题程序(图3),如果是台式机,只有有线网卡,用这段程序足以保证网速的正确检测,


[mw_shl_code=powershell,true]${font 文泉驿微米黑:style=Bold:size=11}下载速度: ${font Impact:size=9}${downspeed eth0} k/s ${font 文泉驿微米黑:style=Bold:size=11}上传速度:${alignr}${font Impact:size=9}${upspeed eth0} k/s

${color #88aadd}${downspeedgraph eth0 15,105 ff0000 0000ff} $alignr${color #88aadd}${upspeedgraph eth0 15,105 0000ff ff0000}


${color white}${font 文泉驿微米黑:style=Bold:size=11}下载总量 ${font Impact:size=9}${totaldown eth0} ${font 文泉驿微米黑:style=Bold:size=11}${alignr}上传总量 ${font Impact:size=9}${totalup eth0}[/mw_shl_code]

# eth0是有线网卡的接口

但本人喜欢经常用无线啊,网速那总是0,那不是废材么,肿么办,怎样才能让其自适应的判断工作网卡并正常显示网速咧?

这就得使用if语句来作出选择了,


[mw_shl_code=powershell,true]# |--WLAN0

${if_up wlan0}

${voffset 5}${color FFFFFF}${goto 125}↓${downspeed wlan0}${color FF6600}/${totaldown wlan0}

${color FFFFFF}${goto 125}↑${upspeed wlan0}${color FF6600}${color FF6600}/${totalup wlan0}

${color FF6600}${goto 125}Net

${voffset -2}${goto 55} Signal: ${color1}${wireless_link_qual wlan0}% ${font Liberation Sans:style=Bold:size=8}${color #dcff82}${wireless_essid wlan0} 

#${color}${font} ${alignr}${color2}${wireless_link_bar 8,60 wlan0}${color}

${voffset 2}${goto 55} ${color FF6600} LocalIP: ${color #FFDC35}${addr wlan0}

# |--ETH0

${else}${if_up eth0}

${voffset 15}${color FFFFFF}${goto 125}↓${downspeed eth0}${color FF6600}/${totaldown eth0}

${color FFFFFF}${goto 125}↑${upspeed eth0}${color FF6600}${color FF6600}/${totalup eth0}

${color FF6600}${goto 125}Net

${voffset 4}${font}${color FF6600}${goto 105}${voffset -4}LocalIP: ${color #FFDC35}${addr eth0}${color}

# |--PPP0

${endif}${else}${if_up ppp0}

${voffset 15}${color FFFFFF}${goto 125}↓${downspeed ppp0}${color FF6600}/${totaldown ppp0}

${color FFFFFF}${goto 125}↑${upspeed ppp0}${color FF6600}${color FF6600}/${totalup ppp0}

${color FF6600}${goto 125}Net

${font}${color FF6600}${goto 32}${voffset -4}LocalIP: ${alignr}${color2}${addr ppp0}${color}

${endif}${else}${voffset 4}${color0}${font PizzaDude Bullets:size=12}4${font}${color}${goto 32}Network Unavailable${endif}${endif}[/mw_shl_code]

无线网卡接口:wlan0


如果只是显示速度,那么OK,问题解决了。可咱是个追求完美的人,再给它加一个图形窗口吧,普通的条状bar不好看,那还得继续努力才行不是{:11_1402:}

经过不懈查找,终于让我发现一种时钟型的实现方式。外号:蛋蛋conky。{:11_1365:}

这个配置需要另外加载脚本:clock_rings.lua。复制以下代码保存为clock_rings.lua

注解:在网络一段 如 

name='downspeedf',

arg='wlan0',

max=1000,

中1000的单位为KB,请根据自己所在网络的实际带宽修改,否则会出现变化不明显(带宽小设置过大)或动则满速(设置太小)


[mw_shl_code=bash,true]--[[

Clock Rings by Linux Mint (2011) reEdited by despot77


This script draws percentage meters as rings, and also draws clock hands if you want! It is fully customisable; all options are described in the script. This script is based off a combination of my clock.lua script and my rings.lua script.


IMPORTANT: if you are using the 'cpu' function, it will cause a segmentation fault if it tries to draw a ring straight away. The if statement on line 145 uses a delay to make sure that this doesn't happen. It calculates the length of the delay by the number
of updates since Conky started. Generally, a value of 5s is long enough, so if you update Conky every 1s, use update_num>5 in that if statement (the default). If you only update Conky every 2s, you should change it to update_num>3; conversely if you update
Conky every 0.5s, you should use update_num>10. ALSO, if you change your Conky, is it best to use "killall conky; conky" to update it, otherwise the update_num will not be reset and you will get an error.


To call this script in Conky, use the following (assuming that you save this script to ~/scripts/rings.lua):

lua_load ~/scripts/clock_rings.lua

lua_draw_hook_pre clock_rings


Changelog:

+ v1.0 -- Original release (30.09.2009)

v1.1p -- Jpope edit londonali1010 (05.10.2009)

*v 2011mint -- reEdit despot77 (18.02.2011)

]]


settings_table = {

{

-- Edit this table to customise your rings.

-- You can create more rings simply by adding more elements to settings_table.

-- "name" is the type of stat to display; you can choose from 'cpu', 'memperc', 'fs_used_perc', 'battery_used_perc'.

name='time',

-- "arg" is the argument to the stat type, e.g. if in Conky you would write ${cpu cpu0}, 'cpu0' would be the argument. If you would not use an argument in the Conky variable, use ''.

arg='%I.%M',

-- "max" is the maximum value of the ring. If the Conky variable outputs a percentage, use 100.

max=12,

-- "bg_colour" is the colour of the base ring.

bg_colour=0xffffff,

-- "bg_alpha" is the alpha value of the base ring.

bg_alpha=0.1,

-- "fg_colour" is the colour of the indicator part of the ring.

fg_colour=0xFF6600,

-- "fg_alpha" is the alpha value of the indicator part of the ring.

fg_alpha=0.2,

-- "x" and "y" are the x and y coordinates of the centre of the ring, relative to the top left corner of the Conky window.

x=100, y=150,

-- "radius" is the radius of the ring.

radius=50,

-- "thickness" is the thickness of the ring, centred around the radius.

thickness=5,

-- "start_angle" is the starting angle of the ring, in degrees, clockwise from top. Value can be either positive or negative.

start_angle=0,

-- "end_angle" is the ending angle of the ring, in degrees, clockwise from top. Value can be either positive or negative, but must be larger than start_angle.

end_angle=360

},

{

name='time',

arg='%M.%S',

max=60,

bg_colour=0xffffff,

bg_alpha=0.1,

fg_colour=0xFF6600,

fg_alpha=0.4,

x=100, y=150,

radius=56,

thickness=5,

start_angle=0,

end_angle=360

},

{

name='time',

arg='%S',

max=60,

bg_colour=0xffffff,

bg_alpha=0.1,

fg_colour=0xFF6600,

fg_alpha=0.6,

x=100, y=150,

radius=62,

thickness=5,

start_angle=0,

end_angle=360

},

{

name='time',

arg='%d',

max=31,

bg_colour=0xffffff,

bg_alpha=0.1,

fg_colour=0xFF6600,

fg_alpha=0.8,

x=100, y=150,

radius=70,

thickness=5,

start_angle=-90,

end_angle=90

},

{

name='time',

arg='%m',

max=12,

bg_colour=0xffffff,

bg_alpha=0.1,

fg_colour=0xFF6600,

fg_alpha=1,

x=100, y=150,

radius=76,

thickness=5,

start_angle=-90,

end_angle=90

},

{

name='cpu',

arg='cpu0',

max=100,

bg_colour=0xffffff,

bg_alpha=0.2,

fg_colour=0xFF6600,

fg_alpha=0.8,

x=50, y=300,

radius=25,

thickness=5,

start_angle=-90,

end_angle=180

},

{

name='cpu',

arg='cpu1',

max=100,

bg_colour=0xFFFFFF,

bg_alpha=0.2,

fg_colour=0xffff00,

fg_alpha=0.8,

x=110, y=300,

radius=25,

thickness=5,

start_angle=-90,

end_angle=180

},

{

name='memperc',

arg='',

max=100,

bg_colour=0xffffff,

bg_alpha=0.2,

fg_colour=0x33ccff,

fg_alpha=0.8,

x=75, y=350,

radius=25,

thickness=5,

start_angle=-90,

end_angle=180

},

{

name='swapperc',

arg='',

max=100,

bg_colour=0xffffff,

bg_alpha=0.2,

fg_colour=0xFF6600,

fg_alpha=0.8,

x=100, y=400,

radius=25,

thickness=5,

start_angle=-90,

end_angle=180

},

{

name='fs_used_perc',

arg='/',

max=100,

bg_colour=0xffffff,

bg_alpha=0.2,

fg_colour=0x306ACD,

fg_alpha=0.8,

x=125, y=450,

radius=25,

thickness=5,

start_angle=-90,

end_angle=180

},

{

name='downspeedf',

arg='wlan0',

max=1000,

bg_colour=0xffffff,

bg_alpha=0.2,

fg_colour=0x339900,

fg_alpha=0.8,

x=145, y=500,

radius=25,

thickness=4,

start_angle=-90,

end_angle=180

},

--很遗憾这里的自适应我还没找到办法实现,只能使用手动修改的办法。我用的无线,如果你用有线请将接口wlan0改为eth0或其他(需要查看网卡接口),下同。

{

name='upspeedf',

arg='wlan0',

max=100,

bg_colour=0xffffff,

bg_alpha=0.2,

fg_colour=0xff6600,

fg_alpha=0.8,

x=145, y=500,

radius=20,

thickness=4,

start_angle=-90,

end_angle=180

},

}


-- Use these settings to define the origin and extent of your clock.


clock_r=65


-- "clock_x" and "clock_y" are the coordinates of the centre of the clock, in pixels, from the top left of the Conky window.


clock_x=100

clock_y=150


show_seconds=true


require 'cairo'


function rgb_to_r_g_b(colour,alpha)

return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha

end


function draw_ring(cr,t,pt)

local w,h=conky_window.width,conky_window.height


local xc,yc,ring_r,ring_w,sa,ea=pt['x'],pt['y'],pt['radius'],pt['thickness'],pt['start_angle'],pt['end_angle']

local bgc, bga, fgc, fga=pt['bg_colour'], pt['bg_alpha'], pt['fg_colour'], pt['fg_alpha']


local angle_0=sa*(2*math.pi/360)-math.pi/2

local angle_f=ea*(2*math.pi/360)-math.pi/2

local t_arc=t*(angle_f-angle_0)


-- Draw background ring


cairo_arc(cr,xc,yc,ring_r,angle_0,angle_f)

cairo_set_source_rgba(cr,rgb_to_r_g_b(bgc,bga))

cairo_set_line_width(cr,ring_w)

cairo_stroke(cr)


-- Draw indicator ring


cairo_arc(cr,xc,yc,ring_r,angle_0,angle_0+t_arc)

cairo_set_source_rgba(cr,rgb_to_r_g_b(fgc,fga))

cairo_stroke(cr) 

end


function draw_clock_hands(cr,xc,yc)

local secs,mins,hours,secs_arc,mins_arc,hours_arc

local xh,yh,xm,ym,xs,ys


secs=os.date("%S") 

mins=os.date("%M")

hours=os.date("%I")


secs_arc=(2*math.pi/60)*secs

mins_arc=(2*math.pi/60)*mins+secs_arc/60

hours_arc=(2*math.pi/12)*hours+mins_arc/12

-- 12 short lines to indicate 0-11

-- change the step(the last number) to control the num of short lines,

-- say, change to 3

for i=0,11,3 do

xh=xc+0.74*clock_r*math.sin(2*math.pi/360*i*30)

yh=yc-0.74*clock_r*math.cos(2*math.pi/360*i*30)

cairo_move_to(cr,xh,yh)


--emphasize 0,3,6,9

local scale = 0.73

if (i % 3) == 0 then

scale = 0.70

end

xh=xc+scale*clock_r*math.sin(2*math.pi/360*i*30)

yh=yc-scale*clock_r*math.cos(2*math.pi/360*i*30)


cairo_line_to(cr,xh,yh)


cairo_set_line_width(cr,2)

cairo_set_source_rgba(cr,0.2,0.6,1,1.0)

cairo_stroke(cr)

end 


-- Draw hour hand


xh=xc+0.7*clock_r*math.sin(hours_arc)

yh=yc-0.7*clock_r*math.cos(hours_arc)

cairo_move_to(cr,xc,yc)

cairo_line_to(cr,xh,yh)


cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND)

cairo_set_line_width(cr,5)

cairo_set_source_rgba(cr,1.0,1.0,1.0,1.0)

cairo_stroke(cr)


-- Draw minute hand


xm=xc+clock_r*math.sin(mins_arc)

ym=yc-clock_r*math.cos(mins_arc)

cairo_move_to(cr,xc,yc)

cairo_line_to(cr,xm,ym)


cairo_set_line_width(cr,3)

cairo_stroke(cr)


-- Draw seconds hand


if show_seconds then

xs=xc+clock_r*math.sin(secs_arc)

ys=yc-clock_r*math.cos(secs_arc)

cairo_move_to(cr,xc,yc)

cairo_line_to(cr,xs,ys)


cairo_set_line_width(cr,1)

cairo_stroke(cr)

end

end


function conky_clock_rings()

local function setup_rings(cr,pt)

local str=''

local value=0


str=string.format('${%s %s}',pt['name'],pt['arg'])

str=conky_parse(str)


value=tonumber(str)

pct=value/pt['max']


draw_ring(cr,pct,pt)

end


-- Check that Conky has been running for at least 5s


if conky_window==nil then return end

local cs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)


local cr=cairo_create(cs) 


local updates=conky_parse('${updates}')

update_num=tonumber(updates)


if update_num>5 then

for i in pairs(settings_table) do

setup_rings(cr,settings_table)

end

end


draw_clock_hands(cr,clock_x,clock_y)

end[/mw_shl_code]


网速监测的问题解决了还有一个令人头疼的天气没有解决呢,可不能高兴的太早。

都这样了还不满足还要要折腾,这不是要人老命么{:11_1404:}

有句话说的好,生命子啊与运动,生活在于折腾!{:11_1411:}


折腾的结果就是从最初无从下手,到最后有三种方法显示天气问题,这也算一大进步是不是{:11_1397:}


#################################Weather Forecast##########################################


实现方式一

也是最早找到最简单但没引起注意的方式,直到后两种通过sh脚本成功实现后我才回国来审视这种简单的方法。

of course,方法简单实现结果也就比较粗糙,功能简单。

直接利用本身函数weather来获取某个气象网站(多是国外的,米办法,国内网站功能。。。你懂的)

复制代码直接添加在 .conkyrc中即可


[mw_shl_code=bash,true]weather http://weather.noaa.gov/pub/data/observ
... /stations/
 ZBTJ weather 30[/mw_shl_code]

ZBTJ是天津的代码,如果是其他城市需要到http://weather.noaa.gov按提示操作查找。比如选择昆明 ,最后的网页地址栏就是 http://weather.noaa.gov/weather/current/ZPPP.html 那么昆明的代码就是ZPPP。当然气象结果还有其他参数(温度
风力 湿度。。。)可以点前面这个连接查看,要想在conky中显示 只需将参数weather改为相应的在添加到.conkyrc中即可。



实现方式二


利用百度搜索天气 第一项默认是你所使用IP所在城市的天气(嘿嘿,不信,你可以试试)

将网页以txt文本形式保存下来,再通过关键词检索,截取天气部分。这个关键词需要根据实际搜索结果更改。

获取百度天气的脚本:复制保存为 weatherimg.sh


[mw_shl_code=shell,true]#!/bin/bash

URL=http://www.baidu.com/s?wd=%CC%EC%C6%F8

baidutq=/tmp/mypogoda

weather=/tmp/pogoda.txt


w3m -dump $URL > $baidutq

end=`cat -n $baidutq | grep "中国气象局201" | cut -f1 | sed 's/ *//'`


head -n $end $baidutq | tail -n 6 > $weather

# iconv -f gbk -t utf-8 $weather > $weather # 转化为utf-8编码

囗囗囗=`~/.conky/scripts/conkytq.sh 1 | cut -d " " -f1`


if echo "$囗囗囗" | grep -E -i -q '转'; then

囗囗囗=$(echo "$囗囗囗" | awk -F "转" '{print $1}')

fi


case $囗囗囗 in

'多云')

cond='CloudyPartly.png' ;;

'晴')

cond='sunny.png' ;;

'晴间多云')

cond='CloudyLightPartly.png' ;;

'阴')

cond='Cloud.png' ;;

'小雨')

cond='RainLight.png' ;;

'中雨')

cond='Rain.png' ;;

'大雨')

cond='RainHeavy.png' ;;

'暴雨')

cond='TorrentialRain.png' ;;

'阵雨')

cond='Shower.png' ;;

'雷阵雨')

cond='tstorms.png' ;;

'大雪')

cond='SnowHeavy.png' ;;

'雨加雪')

cond='RainMixedSnow.png' ;;

'中雪')

cond='Snow.png' ;;

'小雪')

cond='SnowLight.png' ;;

esac


prefstr='${image ~/QQ Files/weather_images/'

poststr=' -s 40x40 -p 150,600 }'

echo $prefstr$cond$poststr[/mw_shl_code]


关键词“中国气象局201”为实际搜索结果气象报告结束后的开头词。即截取“中国气象局201”以前的一部分再保存到/tmp/pogoda.txt中。

读取气象参数脚本:复制保存为conkytq.sh


[mw_shl_code=bash,true]#!/bin/bash

weather=/tmp/pogoda.txt

linenum=$(awk '{print $'$1'}' /tmp/pogoda.txt | sed -n '/℃/=')

line1=$[$linenum+1]

line2=$[$linenum+2]

tianqi=$(awk '{print $'$1'}' /tmp/pogoda.txt | sed -n ''$line1'p' )

temper=$(awk '{print $'$1'}' /tmp/pogoda.txt | sed -n ''$linenum'P' )

wind=$(awk '{print $'$1'}' /tmp/pogoda.txt | sed -n ''$line2'p' )

echo $tianqi $temper $wind

[/mw_shl_code]


℃为特殊参考坐标,以此为标准向前一格读取天气 向后一格读取风力。


这些做好后,在.conkyrc中调用这两个脚本即可得到天气。

调用命令如下:

${execpi 10800 ~/.conky/scripts/weatherimg.sh}${font Droid Sans Fallback:size=10}${color green}天气预报: $color

${goto 12}今天:${execi 10800 ~/.conky/scripts/conkytq.sh 1}


goto 12是行至X轴的第12个像数点。是为了保持整齐一致美观。ececi是前面提到过的调用执行外部脚本程序。

如果你不满足只显示一天的天气,可以在后面添加

${goto 12}明天:${execi 10800 ~/.conky/scripts/conkytq.sh 2}

${goto 12}后天:${execi 10800 ~/.conky/scripts/conkytq.sh 3}

多次执行,显示3天内天气(百度天气最多只有3天)。



实现方式三

个人觉得功能最强大,实现结果最漂亮的方法!嘿嘿,但是咱不告诉你,{:11_1397:}


{:2_114:}。

最后实现效果如图:
附件:
1118417qqnczcr7nlbkoc9.png 
1118417qqnczcr7nlbkoc9.png [ 195.16 KiB | 被浏览 9060 次 ]

附件:
111841kgzihc5997tx86xh.png 
111841kgzihc5997tx86xh.png [ 201.86 KiB | 被浏览 9060 次 ]





开个玩笑,好东西当然要大家一起分享啦。 



获取网站的气象报告文件脚本 conky_api.sh 天津代码:CHXX0133 请根据所在城市更改city_code

获取城市代码请前往[url=: http://www.weather.com/common/welcomepage/world.html]: http://www.weather.com/common/welcomepage/world.html[/url]


[mw_shl_code=bash,true]#!/usr/bin/env bash


conky_dir=~/.conky/

icons_dir=${conky_dir}icons/

weather_xslt=${conky_dir}weather.xslt

weather_xml=/tmp/conky_weather.xml

city_code=CHXX0133


function xsltproc_run() {

day_number=$1

field=$2

moment='d'

result=""

if [ $# -eq 3 ]; then

moment=$3

fi

result=$(xsltproc --stringparam day $day_number --stringparam moment $moment $weather_xslt $weather_xml| cut -d "|" -f$field)

echo $result

}



function get_weather_xml() {

c=$city_code

w=$weather_xml

if [ $# -ge 1 ]; then

c=$1

fi

if [ $# -ge 2 ]; then

w=$2

fi

wget "http://xml.weather.com/weather/local/${c}?cc=*&unit=m&dayf=3" -O ${w}

return 0

}


function get_weather_day() {

echo $(xsltproc_run $1 3)

}


function get_weather_icons() {

day=$1

moment='d'

if [ $# -eq 2 ]; then

moment=$2

fi

if [ $moment == 'd' ]; then

cp $icons_dir$(xsltproc_run $day 6 $moment).png /tmp/weather$day.png

elif [ $moment == 'n' ]; then

cp $icons_dir$(xsltproc_run $day 6 $moment).png /tmp/weathern$day.png

else

echo "Usage: get_weather_icons day [moment]"

echo "day is a number beatween 0 and 6"

echo "moment is the letter 'd' or 'n'"

fi

return 0

}


function get_weather_min() {

day=$1

moment='d'

result=""

if [ $# -eq 2 ]; then

moment=$2

fi

if [[ ( $# -ge 1 ) && ( $# -le 2 ) ]]; then

result=$(xsltproc_run $day 4 $moment)

else

echo "Usage: get_weather_icons day [moment]"

echo "day is a number beatween 0 and 6"

echo "moment is the letter 'd' or 'n'"

fi

echo $result

}


function get_weather_max() {

day=$1

moment='d'

result=""

if [ $# -eq 2 ]; then

moment=$2

fi

if [[ ( $# -ge 1 ) && ( $# -le 2 ) ]]; then

result=$(xsltproc_run $day 5 $moment)

else

echo "Usage: get_weather_icons day [moment]"

echo "day is a number beatween 0 and 6"

echo "moment is the letter 'd' or 'n'"

fi

echo $result

}[/mw_shl_code]


wget "http://xml.weather.com/weather/local/${c}?cc=*&unit=m&dayf=n" 参数n可以在1-7间更改,即获取n天的气象报告。可以在浏览器中直接打开http://xml.weather.com/weather/local/CHXX0133?cc=*&unit=m&dayf=3 查看3天的气象报高



获取气象图片脚本install.sh:


[mw_shl_code=bash,true]#!/usr/bin/env bash

conky_dir=`expr ~/.conky/`

icons_dir=$conky_dir"icons/"

files=(clock_rings.lua conkyrc new-ubuntu-logo.png weather.conky weather.xslt conky_api.sh)


function get_icons {

if [ ! -e "${icons_dir}" ]; then

mkdir "${icons_dir}"

fi

for ((i = 1; i <= 47; i += 1 )); do

wget --no-verbose http://bs.imwx.com/v.20100719.135915/im
... /72/$i.png
 -O "${icons_dir}${i}.png"

done

}


function usage {

echo "$0 <city code location>"

echo "for know your city code go to: http://www.weather.com/common/welcomepage/world.html"

}


if [ $# -lt 1 ]; then

usage

else

# create conky dir if do not exist

if [ ! -e "${conky_dir}" ]; then

mkdir "${conky_dir}"

fi

# install files into conky dir

for f in ${files}; do

install -m 0644 "${f}" "${conky_dir}${f}"

done

# set city location in conky file

if [ $1 != "SZXX0017" ]; then

sed -i "s/SZXX0017/$1/" "${conky_dir}${files[5]}"

fi

# add line for start conky

echo -e "\n# run conky\npkill conky\nconky -c ~/.conky/weather.conky &\nconky -c ~/.conky/conkyrc &\n" >> ~/.bash_profile

# get icons from web

get_icons

# start conky

source ~/.bash_profile

fi[/mw_shl_code]



.conkyrc中的调用命令:


[mw_shl_code=bash,true]${font wqy-microhei:size=9}${color 3399ff}${goto 32}Weather Forecast${execi 36000 . ~/.conky/scripts/conky_api.sh; get_weather_xml}

${goto 32}day${execi 36000 . ~/.conky/scripts/conky_api.sh; get_weather_icons 0}${goto 120}night${execi 36000 . ~/.conky/scripts/conky_api.sh; get_weather_icons 0 n}

${goto 32}${voffset 35}min: ${execi 36000 . ~/.conky/scripts/conky_api.sh; get_weather_min 0}${goto 125}min: ${execi 36000 . ~/.conky/scripts/conky_api.sh; get_weather_min 0 n}

${goto 32}max: ${execi 36000 . ~/.conky/scripts/conky_api.sh; get_weather_max 0}${goto 125}max: ${execi 36000 . ~/.conky/scripts/conky_api.sh; get_weather_max 0 n}

${image /tmp/weather0.png -p 47,650}${image /tmp/weathern0.png -p 135,650}[/mw_shl_code]


#####################################O V E R########################################

附上最后的配置代码:

通用.conkyrc:

需要显示天气的在将天气代码附加到最后即可。

[mw_shl_code=bash,true]# Conky settings #

background no

update_interval 1


cpu_avg_samples 2

net_avg_samples 2


override_utf8_locale yes


double_buffer yes

no_buffers yes


text_buffer_size 2048

#imlib_cache_size 0


temperature_unit fahrenheit


# Window specifications #


own_window yes

#own_window_type override

own_window_type normal

own_window_transparent yes

#own_window_hints undecorate,sticky,skip_taskbar,skip_pager,below

own_window_hints undecorated,below,skip_taskbar,sticky


border_inner_margin 0

border_outer_margin 0


minimum_size 200 250

maximum_width 200


alignment tr

gap_x 35

gap_y 


# Graphics settings #

draw_shades no

draw_outline no

draw_borders no

draw_graph_borders no


# Text settings #

use_xft yes

xftfont 文泉驿微米黑:size=8

xftalpha 0.5


uppercase no


temperature_unit celsius



default_color FFFFFF


# Lua Load #

lua_load ~/.conky/clock_rings.lua

lua_draw_hook_pre clock_rings


TEXT

${voffset 8}${color 9D57B6}${font 文泉驿微米黑:size=16}${time %A}${font}${voffset -8}${alignr 50}${color FFFFFF}${font 文泉驿微米黑:size=38}${time %e}${font}

${color FFFFFF}${voffset -30}${color 339933}${font 文泉驿微米黑:size=18}${time %b}${font}${voffset -3} ${color FFFF50}${font 文泉驿微米黑:size=20}${time %Y}年${font}${color FF6600}${hr}

${image ~/.conky/Arch_logo.png -p 74,125 -s 50x50}

${voffset 125}${font :style=Bold:size=8}${alignr}WEATHER${font}

${color FF6600}${font Droid Sans:style=Bold:size=8}Processes: ${font Droid Sans:style=Regular:size=8}$processes ( ${color}${running_processes}${color FF6600} running) ${font Droid Sans:style=Regular:size=10}${color FFFFFF}${alignr}${weather http://weather.noaa.gov/pub/data/observ
... /stations/
 ZBTJ weather 30} ${font}

${color FF6600}${voffset 6}${font Droid Sans:style=Regular:size=10}${nodename}'s Uptime:${color2}${uptime_short}${color}${alignr}${weather http://weather.noaa.gov/pub/data/observ
... /stations/
 ZBTJ temperature 70} °C

${color FFFFFF}${goto 25}${voffset 35}${cpu cpu0}% ${font Droid Sans:style=Bold:size=8}${color1}${acpitemp}°C${color}${font}${color FFFFFF}${goto 85}${cpu cpu1}% ${font Droid Sans:style=Bold:size=8}${color1}${hwmon temp 2}°C${color}${font}

${color FF6600}${goto 25}CPU1${goto 85}CPU2 频率: ${freq} MHz

${color FFFFFF}${goto 50}${voffset 23}${memperc}%

${color FF6600}${goto 50}RAM

${color FFFFFF}${goto 75}${voffset 23}${swapperc}%

${color FF6600}${goto 75}SWAP

${color FFFFFF}${goto 100}${voffset 23}${fs_used_perc /}%

${color FF6600}${goto 100}Disk

# |--WLAN0

${if_up wlan0}

${voffset 5}${color FFFFFF}${goto 125}↓${downspeed wlan0}${color FF6600}/${totaldown wlan0}

${color FFFFFF}${goto 125}↑${upspeed wlan0}${color FF6600}${color FF6600}/${totalup wlan0}

${color FF6600}${goto 125}Net

${voffset -2}${goto 55} Signal: ${color1}${wireless_link_qual wlan0}% ${font Liberation Sans:style=Bold:size=8}${color #dcff82}${wireless_essid wlan0} 

#${color}${font} ${alignr}${color2}${wireless_link_bar 8,60 wlan0}${color}

${voffset 2}${goto 55} ${color FF6600} LocalIP: ${color #FFDC35}${addr wlan0}

# |--ETH0

${else}${if_up eth0}

${voffset 15}${color FFFFFF}${goto 125}↓${downspeed eth0}${color FF6600}/${totaldown eth0}

${color FFFFFF}${goto 125}↑${upspeed eth0}${color FF6600}${color FF6600}/${totalup eth0}

${color FF6600}${goto 125}Net

${voffset 4}${font}${color FF6600}${goto 105}${voffset -4}LocalIP: ${color #FFDC35}${addr eth0}${color}

# |--PPP0

${endif}${else}${if_up ppp0}

${voffset 15}${color FFFFFF}${goto 125}↓${downspeed ppp0}${color FF6600}/${totaldown ppp0}

${color FFFFFF}${goto 125}↑${upspeed ppp0}${color FF6600}${color FF6600}/${totalup ppp0}

${color FF6600}${goto 125}Net

${font}${color FF6600}${goto 32}${voffset -4}LocalIP: ${alignr}${color2}${addr ppp0}${color}

${endif}${else}${voffset 4}${color0}${font PizzaDude Bullets:size=12}4${font}${color}${goto 32}Network Unavailable${endif}${endif}

${voffset -10}${goto 32}${color #61265F}${font Narkisim:style=Bold:size=8}Processes ${font Impact:size=8}$alignr CPU ${font Narkisim:size=8} RAM PID

${goto 32}${font Narkisim:size=8:style=Regular}${top name 1}$alignr${top cpu 1}${top mem 1}${top pid 1}

${goto 32}${font Narkisim:size=8:style=Regular}${top name 2}$alignr${top cpu 2}${top mem 2}${top pid 2}

${goto 32}${font Narkisim:size=8:style=Regular}${top name 3}$alignr${top cpu 3}${top mem 3}${top pid 3}

${goto 32}${font Narkisim:size=8:style=Regular}${top_mem name 1}$alignr${top_mem cpu 1}${top_mem mem 1}${top_mem pid 1}

${goto 32}${font Narkisim:size=8:style=Regular}${top_mem name 2}$alignr${top_mem cpu 2}${top_mem mem 2}${top_mem pid 2}

${goto 32}${color #cacaca} Battery:${battery BAT0} ${battery_time BAT0}

[/mw_shl_code]



[




附件:
111840wrc3cj38hgrkusgf.png

抱歉!评论已关闭.