浠ヤ笅閮芥槸鍦╱buntu涓嬭繘琛岀殑
鍙傝?br />
http://www.libfetion.cn/Docs-dve/Build-LibFx-on-ubuntu.txt
璇蜂嬌鐢╯vn瀹㈡埛绔笅杞絣ibfetion-gui鐨勬簮鐮?br />
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
鍦╰runk/qt4_src/libfetion/lib鐩綍涓嬫壘鍒伴潤鎬佸簱libfetion_32.a錛岀敱浜庝唬鐮佹墦綆楁槸鐢╬ython鏉ュ啓錛屾墍浠ュ埗浣滀簡涓涓姩鎬佸簱libfetion.so
鍏蜂綋姝ラ濡備笅錛?br />
#瑙e帇闈欐佸簱涓?.o
ar -x libfetion_32.a
#閲嶆柊灝佽涓簊o
g++ -shared -Wall -fPIC -lcurl -pthread *.o -o libfetion.so
#鏌ョ湅so[鍙夋楠
nm libfetion.so
鍙戦佸ぉ姘旈鎶ョ殑python浠g爜濡備笅錛宭ibfetion.so涓叿浣撶殑鏂規硶璇鋒煡鐪媡runk/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 #鍩庡競浠g爜鍒楄〃
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 #鍒濆鍖?/span>
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 #鑾峰緱闇瑕佸彂閫佺殑鍩庡競浠g爜
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浠g爜
74 cmd = 'sed -i -e \'s/<[^>]*>//g\' %s.htm' % city_codes[key]
75 os.popen(cmd)
76
77 #鍘繪帀涓嶇浉鍏沖瓧絎?/span>
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 #閫鍑洪淇?nbsp;
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 #緇欑敤鎴峰彂閫佹榪庝俊鎭?/span>
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()
緙栧啓鑴氭湰鍜屼換鍔¤鍒?br />
緙栧啓鑴氭湰錛?br />
vim weather
鍐欏叆浠ヤ笅鍐呭
1 cd /home/loh/weather#浣犵殑鑴氭湰鎵鍦ㄧ洰褰?/span>
2 python weather.py
chmod +x weather
浠誨姟璁″垝錛?br />
crontab -e
鍐欏叆浠ヤ笅鍐呭錛堟瘡澶╂棭涓?鐐?鍒嗘墽琛岃剼鏈級
1 # m h dom mon dow command
2 1 7 * * * /home/loh/weather/weather
澶╂皵棰勬姤鍐呭鏄繖鏍風殑錛?br />
澶╂觸 闃?nbsp;9.3°C 鎰熻8°C 椋庡姏鍗?綰?nbsp;婀垮害60% 浠婂ぉ鏅磋漿闆?nbsp;9/2°C 鏄庡ぉ闆捐漿澶氫簯 9/0°C

]]>