注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Allen小笔记

有时会忘记努力...

 
 
 

日志

 
 

手机获取天气预报  

2009-12-15 20:05:50|  分类: Linux |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
                                                                                            Linux,Python,Ubuntu,Parser,Fetion

前一段时间,一直在CMMI,直到LA interview完ENG工作后,我突然“又”清闲了起来!作了两个小工具,来帮助完成我的工作.
其实,确切的说,这一个和我的工作不相关.
PC上定时向我的手机上发送天气预报。我是参考《定制自己的免费天气预报短信》这篇文章的。
Solrex Shuffling 似乎一直在更新shell脚本,但是没有更新python脚本,我呢,也不会python,就只是依葫芦画瓢,所以有了下面的Python script(我不知道怎么把文件放上来,所以就貼出来吧). 然后,还需要一个发送飞信的程序,去这里下载,编译时需要curl的dev库,然而我怎么的ubuntu 9.10怎么都装不上, 几经波折,原来是我的/etc/apt/sources.list中的源有问题,更新之就可以使用apt-get install libcurl4-gnutls-dev 安装啦.而LibFetion说的很清楚,不要用libcurl4-openssl-dev.
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
import urllib
import time
import shutil

city_list = {
    '南昌': '101240101',# the city I am living NOW
    '上饶': '101240301',#my home town
    '进贤': '101240105',#my girl friend's home
}
my_cities = ['南昌','上饶']
weather_users = {
    'Fetion 号码 or 手机号码': '地名'
}
time_H= "08:00"
class weather:
    urlbase = "http://www.weather.com.cn/html/weather/"
    def __init__(self):
        pass

    def clear_html(self):
        for city in city_list.keys():
            cmd = "rm -f %s.htm" % (city)
            os.popen(cmd)

    def get_html(self):
        for city in city_list.keys():
            url = self.urlbase + city_list[city] + '.shtml'
            city_path=os.path.abspath(os.curdir)+'/'+city
            cmd = "wget --no-verbose -O %s.htm %s" % (city, url)
            os.popen(cmd)
        return 0

    def parse_html(self):
        filename = "上饶.htm"
        cmd = "grep -q ' 08:00发布)' %s" %(filename)
        result = os.popen(cmd)
        if result == None:
            time_H="18:00"
        for city in city_list:
            filename = city + ".htm"
            #Remove senseless things
            cmd="sed -i -e '1,/\"tableTop\"/d;/weatherYubao2/,$d;1,/风力/d;' %s" %(filename)
            os.popen(cmd)          
            # Remove html tag
            cmd = "sed -i -e 's/<[^>]*>//g;/<!--/d' %s" % (filename)
            os.popen(cmd)
            # Remove empty line, including &nbsp, &deg; from html
            cmd = "sed -i -e 's/&nbsp;//g;s/&deg;C//g;s/^\s*//g;/^$/d' %s" % (filename)
            os.popen(cmd)
            cmd = "sed -i -e 's/℃/度/g;s/\r//g;' %s" % (filename)
            os.popen(cmd)

    def generate_msg(self,city):
        msg = ''
        filename = city + ".htm"
        fp = open(filename)
        now = time.strftime('%H')
        msg += city + time_H+"(来自阿飞的Ubuntu9.10,不收取任何费用)"
        if (time_H == "18:00"):
            for i in [1, 2, 3, 4]:
                for j in [1, 2]:
                    if (i==1 and j==2) :
                        continue;
                    msg +='\n'+fp.readline().replace('\n',':' )
                    #temp = fp.readline()
                    msg += fp.readline().replace('\n', '')
                    msg += fp.readline().replace('\n', ',')
                    msg += fp.readline().replace(' ', '').replace('\n', ',')
                    msg += fp.readline().replace('\n', '')
                    msg += fp.readline().replace('\n', '.')
        else :
            for i in [1, 2, 3]:
                msg += '\n'+fp.readline().replace('\n', ':')
                msg += fp.readline().replace('\n', '')
                msg += fp.readline().replace('\n', ',')
                msg += fp.readline().replace(' ', '').replace('\n', ',')#temperature
                msg += fp.readline().replace('\n', '')
                msg += fp.readline().replace('\n', ',')
                msg += fp.readline().replace('\n', '')
                msg += fp.readline().replace('\n', ',')
                msg += fp.readline().replace(' ', '').replace('\n', ',')#temperature
                msg += fp.readline().replace('\n', '')
                msg += fp.readline().replace('\n', '.')

        fp.close()
        print msg
        return msg

if __name__=="__main__":
    start = weather()
    start.clear_html()
    start.get_html()
    start.parse_html()
    message = ""
    # Send the weather report to myself
    for city in my_cities:
        if city=="南昌" or city == "上饶":# I JUST NEED THESE CITIES
            message = start.generate_msg(city)
            cmd = 'sendsms -wlf 137xxxxxxxx -p xxxxxxxxx -t 137xxxxxxxx "%s"' %(message)
            os.system(cmd)
    # Send the focast to my hearts
    for user in weather_users.keys():
        message = start.generate_msg(weather_users[user])
        #print message
        cmd = 'sendsms -wlf 137xxxxxxxx -p xxxxxxxxx -t %s "%s"' % (user, message)
        os.system(cmd)
    start.clear_html()
第二个呢,这是减少劳动量的工具,说实话,IE7&8(当然你可以不用IE,我本身使用Chrome, firefox)本身就运行的很慢,我还要满世界的去找链接,会很痛苦的,所以才有这个小工具的诞生.
这个工具,可以帮助我parser,我们公司KM系统中的数据,然后分析出可以读的doc(x),ppt(x),pdf档,随机打开其中的一个.
这里要记录下:如何阻止IE弹出保存或者打开的对话框?文件夹选项--文件类型--找到.Doc类型--高级(编辑文件类型)--去掉下载后确认打开复选框.
如何在IE内嵌View中打开office?
  1. Start Notepad.
  2. Copy and then paste the following text to Notepad:
    Windows Registry Editor Version 5.00

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.Document.8]
    "BrowserFlags"=dword:80000024

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.RTF.8]
    "BrowserFlags"=dword:80000024

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.Document.12]
    "BrowserFlags"=dword:80000024

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.DocumentMacroEnabled.12]
    "BrowserFlags"=dword:80000024

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Sheet.8]
    "BrowserFlags"=dword:80000A00

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Sheet.12]
    "BrowserFlags"=dword:80000A00

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.SheetMacroEnabled.12]
    "BrowserFlags"=dword:80000A00

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.SheetBinaryMacroEnabled.12]
    "BrowserFlags"=dword:80000A00

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.Show.8]
    "BrowserFlags"=dword:800000A0

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.Show.12]
    "BrowserFlags"=dword:800000A0

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.ShowMacroEnabled.12]
    "BrowserFlags"=dword:800000A0

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.SlideShow.8]
    "BrowserFlags"=dword:800000A0

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.SlideShow.12]
    "BrowserFlags"=dword:800000A0

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.SlideShowMacroEnabled.12]
    "BrowserFlags"=dword:800000A0
  3. Save the file as FileName.reg.
  4. Double-click FileName.reg to run this registry script.
  5. Start Internet Explorer 7&8.
原文地址:http://support.microsoft.com/default.aspx?scid=kb;EN-US;927009

  评论这张
 
阅读(311)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017