python版本的cairo-weather~~~

软件和网站开发以及相关技术探讨
头像
leeaman
帖子: 30702
注册时间: 2007-02-02 18:14
系统: debian sid

Re: python版本的cairo-weather~~~

#31

帖子 leeaman » 2012-05-10 0:42

好吧,有空我把要改的全部做成变量写前面,注解好什么改,行了吧 :em06
醉了星星,醉月亮●●●●●The Long Way To Go(*^_^*)
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re: python版本的cairo-weather~~~

#32

帖子 tusooa » 2012-06-18 16:10

吾的都是放在专门的配置文件里的。

代码: 全选

] ls -ld //
头像
leeaman
帖子: 30702
注册时间: 2007-02-02 18:14
系统: debian sid

Re: python版本的cairo-weather~~~

#33

帖子 leeaman » 2012-06-18 16:57

晕,挖坟 :em20 真用估计也会改的,不打包做配置文件多余,再说文件io我还没去研究
醉了星星,醉月亮●●●●●The Long Way To Go(*^_^*)
ubuntu606
帖子: 624
注册时间: 2007-07-14 17:04

Re: python版本的cairo-weather~~~

#34

帖子 ubuntu606 » 2012-06-20 13:55

整个程序写下来花了多长时间?
头像
leeaman
帖子: 30702
注册时间: 2007-02-02 18:14
系统: debian sid

Re: python版本的cairo-weather~~~

#35

帖子 leeaman » 2012-06-20 16:20

这都要八啊....去年五一前几天在irc和ee吹水说要写,然后五一前收集资料看看能不能写,五一放假开工写,发帖就是第一个完成的,后面陆陆续续做了许多小修改,不过程序的大体结构都是这样的了
醉了星星,醉月亮●●●●●The Long Way To Go(*^_^*)
ubuntu606
帖子: 624
注册时间: 2007-07-14 17:04

Re: python版本的cairo-weather~~~

#36

帖子 ubuntu606 » 2012-06-20 18:08

佩服佩服 :em01

一直在学c++,依我目前的水平用c++写不出来这个,看来只有寄托在脚本语言身上了
leeaman 写了:这都要八啊....去年五一前几天在irc和ee吹水说要写,然后五一前收集资料看看能不能写,五一放假开工写,发帖就是第一个完成的,后面陆陆续续做了许多小修改,不过程序的大体结构都是这样的了
头像
leeaman
帖子: 30702
注册时间: 2007-02-02 18:14
系统: debian sid

Re: python版本的cairo-weather~~~

#37

帖子 leeaman » 2012-06-20 18:50

c++写这个有点蛋疼了,不过我支持,然后移植到win下面去,哈哈
醉了星星,醉月亮●●●●●The Long Way To Go(*^_^*)
头像
leeaman
帖子: 30702
注册时间: 2007-02-02 18:14
系统: debian sid

Re: python版本的cairo-weather~~~

#38

帖子 leeaman » 2012-06-20 19:00

再放一个python3的,其实就是urllib换成urllib.request,改了主程序前面几句,别的python2 3都通用的
[python]#!/usr/bin/python3
#coding:utf-8

import urllib.request,cairo,os,datetime,re

##变量列表,参照自己的修改
##你想显示的地方的天气预报的网址,具体字体上 http://wap.weather.com.cn/wap/ 查询,然后替换下面的网址
My_Location="http://wap.weather.com.cn/wap/weather/101280905.shtml"
##自己改标题,一般就是换"广东德庆",名字太长的话可能显示不完全
sTitle="广东德庆天气预报"
##天气图标的所在目录,自己放那里自己改,要用绝对路径
png_dir="/home/missing/cairo-weather/"
##背景图片路径,图片要求和下面的w_bg,h_bg大小一致
bg_png="/home/missing/cairo-weather/cc.png"
##显示器分辨率
w_bg,h_bg=1366,768
##天气显示的坐标(相对屏幕左上角为0,0,要小于w_bg-896和h_bg-256,否则显示不完全)
x_offset,y_offset=450,300
##星期字符列表,这个不用改
s_week=['星期一','星期二','星期三','星期四','星期五','星期六','星期天']

##########draw rounded rectangles###############################################
def draw_round_rectangles(context,x,y,w,h,r):
context.move_to(x+r,y)
context.line_to(x+w-r,y)
context.curve_to(x+w,y,x+w,y,x+w,y+r)
context.line_to(x+w,y+h-r)
context.curve_to(x+w,y+h,x+w,y+h,x+w-r,y+h)
context.line_to(x+r,y+h)
context.curve_to(x,y+h,x,y+h,x,y+h-r)
context.line_to(x,y+r)
context.curve_to(x,y,x,y,x+r,y)
context.fill()
return
################################################################################

############draw weather date week weather message etc.#########################
def draw_weather_message(context,n,m,bool_text):
context.set_font_size(15)
context.move_to(n*128+10+x_offset,30+y_offset)
n_day=datetime.timedelta(days=n)
m_day=today+n_day
str_day=str(m_day.date())
context.text_path(str_day)
context.move_to(n*128+20+x_offset,60+y_offset)
n_week=num_week_today+n
if n_week<=6 :
m_week=n_week
else:
m_week=n_week-7
context.text_path(s_week[m_week])
d1=re.sub('<dt> |<br />&nbsp|</dt>','',msg[22+4*n])
w_msg=d1.split(';')
msg_weather=re.sub('[0-9]|/|℃','',w_msg[0])
msg_weather_out=re.sub("大到|中到|小到","",msg_weather)
msg_temp=re.sub(msg_weather,'',w_msg[0])
context.move_to(n*128+10+x_offset,180+y_offset)
context.text_path(msg_weather_out)
context.move_to(n*128+10+x_offset,200+y_offset)
context.text_path(msg_temp)
bool_match=re.search('/',w_msg[1])
if bool_match:
message_split=w_msg[1].split('/')
context.move_to(n*128+10+x_offset,220+y_offset)
context.text_path(message_split[0])
context.move_to(n*128+10+x_offset,240+y_offset)
context.text_path(message_split[1])
else:
context.move_to(n*128+10+x_offset,230+y_offset)
context.text_path(w_msg[1])
if bool_text:
context.stroke()
else:
context.fill()
return
################################################################################

##########draw weather png icon#################################################
def draw_weather_png(context,m):
d1=re.sub('<dt> |<br />&nbsp|</dt>','',msg[22+4*m])
w_msg=d1.split(';')
tp_msg=re.sub('[0-9]|/|℃','',w_msg[0])
str_message=re.sub("大到|中到|小到","",tp_msg)
bool_match=re.search('转',str_message)
if bool_match:
file_name_list=str_message.split('转')
if os.path.exists(png_dir+file_name_list[0]+'.png') and os.path.exists(png_dir+file_name_list[1]+".png"):
img1=cairo.ImageSurface.create_from_png(png_dir+file_name_list[0]+'.png')
img2=cairo.ImageSurface.create_from_png(png_dir+file_name_list[1]+'.png')
context.set_source_surface(img1,5+128*m+x_offset,75+y_offset)
context.paint()
context.set_source_surface(img2,50+128*m+x_offset,85+y_offset)
context.paint()
else:
if os.path.isfile(png_dir+str_message+'.png'):
img=cairo.ImageSurface.create_from_png(png_dir+str_message+'.png')
context.set_source_surface(img,20+128*m+x_offset,80+y_offset)
context.paint()
return
################################################################################
#################main program ##################################################

################get weather message,week numeber,and format message#############

response=urllib.request.urlopen(My_Location)
byte_page_text=response.read()
string_page_text=byte_page_text.decode("utf8")
response.close()
msg=string_page_text.split('\n')
date_mark=re.sub('<h3>|</h3>','',msg[17])
s1=date_mark.split(' ')
s2=s1[0].split('-')
s_year,s_month,s_day=int(s2[0]),int(s2[1]),int(s2[2])
today=datetime.datetime(s_year,s_month,s_day)
num_week_today=today.weekday()
################################################################################

##################initial surface###############################################
WIDTH,HEIGHT=w_bg,h_bg
surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context (surface)
################################################################################

##################drawing address message#######################################
ctx.set_source_rgba(0,0.75,0.4,0.6)
ctx.select_font_face("Sans",cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_BOLD)
################################################################################

##########################Load background#######################################
img_bg=cairo.ImageSurface.create_from_png(bg_png)
ctx.set_source_surface(img_bg,0,0)
ctx.paint()
img_logo=cairo.ImageSurface.create_from_png(png_dir+'logo.png')
ctx.set_source_surface(img_logo,75+128*5+x_offset,30+y_offset)
ctx.paint()
################################################################################

##################draw round rectangles#########################################
ctx.set_source_rgba(0,0,0,0.6)
for n in range(7):
draw_round_rectangles(ctx,128*n+x_offset,y_offset,128,256,20)
ctx.set_source_rgba(0.5,0,0,0.2)
draw_round_rectangles(ctx,x_offset,y_offset,128,256,20)
ctx.set_source_rgba(0,0,0.5,0.2)
i=num_week_today
if i==1:
draw_round_rectangles(ctx,128*4+x_offset,0+y_offset,128,256,20)
elif i==2:
draw_round_rectangles(ctx,128*4+x_offset,0+y_offset,128,256,20)
draw_round_rectangles(ctx,128*3+x_offset,0+y_offset,128,256,20)
elif i==3:
draw_round_rectangles(ctx,128*3+x_offset,0+y_offset,128,256,20)
draw_round_rectangles(ctx,128*2+x_offset,0+y_offset,128,256,20)
elif i==4:
draw_round_rectangles(ctx,128*2+x_offset,0+y_offset,128,256,20)
draw_round_rectangles(ctx,128*1+x_offset,0+y_offset,128,256,20)
elif i==5:
draw_round_rectangles(ctx,128*1+x_offset,0+y_offset,128,256,20)
################################################################################

###################draw weather message#########################################
ctx.set_source_rgba(0,0,0,0.8)
ctx.set_line_width(1)
for i in range(5):
draw_weather_message(ctx,i,i,1)
ctx.set_source_rgba(0.855,0.255,0,1)
for i in range(5):
draw_weather_message(ctx,i,i,0)
ctx.move_to(128*6-40+x_offset,250+y_offset)
ctx.text_path(date_mark)
ctx.move_to(128*6-40+x_offset,20+y_offset)
ctx.text_path(sTitle)
ctx.fill()
################################################################################

##################draw weather icon#############################################
for m in range(5):
draw_weather_png(ctx,m)
################################################################################
del ctx
surface.write_to_png ("/tmp/bg.png") # Output to PNG
################################################################################

##change the gnome3's wallpaper,also you can use feh xloadimage etc command to change wallpaper
os.system('xloadimage -fullscreen -onroot /tmp/bg.png')
#########the end of main program################################################

[/python]
附件
cairo-weather.zip
包括python2 3两个版本的
(1.06 MiB) 已下载 107 次
醉了星星,醉月亮●●●●●The Long Way To Go(*^_^*)
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

Re: python版本的cairo-weather~~~

#39

帖子 YeLee » 2012-06-20 19:12

只要不是perl我都会支持一个的。 :em04
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
回复