小碼哥

          誰謂河廣,一葦杭之

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            7 隨筆 :: 17 文章 :: 74 評論 :: 0 Trackbacks

          常用鏈接

          留言簿(21)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          訂閱Canvas

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          什么是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/&deg;/°/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


          posted on 2009-11-23 14:56 小碼哥 閱讀(1430) 評論(0)  編輯  收藏 所屬分類: linuxpython

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 达拉特旗| 天峨县| 富源县| 台江县| 镇江市| 万全县| 龙泉市| 岳西县| 阳信县| 平度市| 灯塔市| 安图县| 富蕴县| 桑日县| 景德镇市| 清远市| 长治县| 九龙城区| 新野县| 乌拉特后旗| 安徽省| 奇台县| 淳安县| 比如县| 三明市| 威远县| 新和县| 大丰市| 五大连池市| 抚顺县| 新建县| 淮安市| 宜春市| 河南省| 垦利县| 根河市| 苗栗县| 永登县| 公安县| 盘锦市| 哈巴河县|