什么是libfetion?请访问http://www.libfetion.cn/查看
以下都是在ubuntu下进行的
参考
http://www.libfetion.cn/Docs-dve/Build-LibFx-on-ubuntu.txt
请使用svn客户端下载libfetion-gui的源码
http://libfetion-gui.googlecode.com/svn/
安装开发过程中需要的软件
1 sudo apt-get install libc-dev
2 sudo apt-get install g++
3 sudo apt-get install libcurl4-openssl-dev
在trunk/qt4_src/libfetion/lib目录下找到静态库libfetion_32.a,由于代码打算是用python来写,所以制作了一个动态库libfetion.so
具体步骤如下:
#解压静态库为*.o
ar -x libfetion_32.a
#重新封装为so
g++ -shared -Wall -fPIC -lcurl -pthread *.o -o libfetion.so
#查看so[可选步骤]
nm libfetion.so
发送天气预报的python代码如下,libfetion.so中具体的方法请查看trunk/qt4_src/libfetion/include/libfetion/libfetion.h
1 #!/usr/bin/env python
2 #coding=utf-8
3 #only can run at linux
4 import os
5 import ctypes
6 import urllib2
7
8 #城市代码列表
9 city_codes = {
10 '合肥' : 'CHXX0448',
11 '安庆' : 'CHXX0452',
12 '天津' : 'CHXX0133',
13 '南昌' : 'CHXX0097',
14 '上海' : 'CHXX0097',
15 '北京' : 'CHXX0097',
16 '长沙' : 'CHXX0013',
17 '常德' : 'CHXX0416',
18 '北京' : 'CHXX0008',
19 '银川' : 'CHXX0259'
20 }
21
22 #用户定制城市
23 weather_users = {
24 '天津':['138*******3','159*******7','159*******2','150*******6','135*******1'],
25 '北京':['159*******2'],
26 '银川':['159*******2']
27 }
28
29 #自己手机
30 myself_city_list = ['天津','安庆']
31
32 class weather:
33 weatherBaseUrl = "http://www.thinkpage.cn/weather/weather.aspx?uid=&l=zh-CN&p=CMA&a=0&u=C&s=4&m=0&x=1&d=2&fc=&bgc=&bc=&ti=1&in=1&li=2&c="
34 #初始化
35 def __init__(self):
36 pass
37
38 #清空html
39 def clear_html(self):
40 cmd = 'rm -f *.htm'
41 os.popen(cmd)
42
43 #获得需要发送的城市代码
44 def __getCityToSend(self):
45 self.city_all = {}
46 for key in weather_users.keys():
47 self.city_all[key] = 1
48 for key in myself_city_list:
49 self.city_all[key] = 1
50
51 #获取html
52 def get_html(self):
53 self.__getCityToSend();
54 for key in self.city_all.keys():
55 weatherUrl = self.weatherBaseUrl + city_codes[key]
56 req = urllib2.Request(weatherUrl)
57 res = urllib2.urlopen(req)
58 weather_content = res.read()
59 res.close()
60 file_html = open(city_codes[key] + '.htm','w')
61 file_html.write(weather_content)
62 file_html.close()
63
64 #过滤无用信息
65 def parse_html(self):
66 for key in self.city_all.keys():
67 file_name = city_codes[key] + '.htm'
68
69 #获得信息所在行
70 cmd = 'cat %s.htm |grep -E \'ltl|forecastDay|temp\'|grep -v spanDate > %s.htm' % (city_codes[key],city_codes[key])
71 os.popen(cmd)
72
73 #去掉html代码
74 cmd = 'sed -i -e \'s/<[^>]*>//g\' %s.htm' % city_codes[key]
75 os.popen(cmd)
76
77 #去掉不相关字符
78 cmd = 'sed -i -e \'s/°/°/g;s/ //g\' %s.htm' % city_codes[key]
79 os.popen(cmd)
80
81 #根据城市生成消息
82 def __generate_msg(self,city_key):
83 #打开文件
84 file_html = open(city_codes[city_key] + '.htm')
85
86 #读取信息
87 weather_lines = file_html.readlines()
88 file_html.close()
89 weather_content = ''
90 for line in weather_lines:
91 weather_content = weather_content + line.replace('\r\n',' ')
92 return weather_content
93
94 #登录飞信
95 def fetion_login(self, your_mobile_no, your_pwd):
96 self.libc = ctypes.cdll.LoadLibrary('/home/loh/weather/libfetion.so')
97 self.libc.fx_init()
98 self.libc.fs_login(your_mobile_no,your_pwd)
99 self.libc.fx_set_longsms(True)
100
101 #退出飞信
102 def fetion_logout(self):
103 self.libc.fx_loginout()
104 self.libc.fx_terminate()
105
106 #给自己发信息
107 def send_msg_to_myself(self):
108 for city_key in myself_city_list:
109 msg = self.__generate_msg(city_key)
110 self.libc.fs_send_sms_to_self(msg)
111
112 #给用户发信息
113 def send_msg(self):
114 for city_key in weather_users.keys():
115 msg = self.__generate_msg(city_key)
116 for user in weather_users[city_key]:
117 self.libc.fs_send_sms_by_mobile_no(user,msg)
118
119 #给用户发送欢迎信息
120 def send_welcome_msg(self, msg):
121 user_all = {}
122 for user_list in weather_users.values():
123 for user in user_list:
124 user_all[user] = 1
125
126 for user in user_all.keys():
127 self.libc.fs_send_sms_by_mobile_no(user,msg)
128
129
130 if __name__ == '__main__':
131 weather = weather()
132 weather.get_html()
133 weather.parse_html()
134 weather.fetion_login('136*******3', 'password')
135 weather.send_msg_to_myself()
136 weather.send_msg()
137 #weather.send_welcome_msg('您好!天气预报全线升级,支持国内所有城市以及国外主要大城市,此外您还可以定制多个城市')
138 weather.fetion_logout()
139 weather.clear_html()
编写脚本和任务计划
编写脚本:
vim weather
写入以下内容
1 cd /home/loh/weather#你的脚本所在目录
2 python weather.py
chmod +x weather
任务计划:
crontab -e
写入以下内容(每天早上7点1分执行脚本)
1 # m h dom mon dow command
2 1 7 * * * /home/loh/weather/weather
天气预报内容是这样的:
天津 阴 9.3°C 感觉8°C 风力南1级 湿度60% 今天晴转雾 9/2°C 明天雾转多云 9/0°C