自动下载openstack社区邮件的脚本

社区邮件要怎么样才能最快速的浏览?来用这个脚本吧,自动下载到本地,去掉广告,留下内容。


import os
import time
import subprocess

def execute(cmd):
    p = subprocess.Popen(cmd, stdin = subprocess.PIPE,
                         stdout = subprocess.PIPE,
                         stderr = subprocess.PIPE,
                         shell = True)
    result = p.stdout.read()
    return result

def markData(input):
    input = input.replace("neutron", "<font color='#00EEEE'>neutron</font>")
    return input

def formatMail(input):
    if input.find(" ") != -1:
        input = input.strip()
        array = input.split(" ")
        newInput = " "
        count = 0
        for data in array:
            count = count + 1
            newInput = newInput + " " + data
            if count % 9 == 0 and count != len(array):
                newInput = newInput + "<br>"
        return newInput
    return input

def downloadOneMail(project, year, month, mailID):
    fileName = "mail/" + mailID
    if os.path.isfile(fileName):
        return
    url = "http://lists.openstack.org/pipermail/%s/%s-%s/%s" % (project, year, month, mailID)
    result = execute("curl " + url)
    if result.find("<!--beginarticle-->") == -1:
        return
    array = result.split("\n")
    wf = open(fileName, 'w')
    wf.write("<html><head><title>%s</title></head>" % fileName)
    wf.write("\n<style>body{font-size:12px;text-decoration:none;}a{text-decoration:none;}</style><body>")
    dataFlag = 0
    for input in array:
        if dataFlag == 0:
            if input.find("<!--beginarticle-->") != -1:
                dataFlag = 1
        elif dataFlag == 1:
            if input.find("<!--endarticle-->") != -1:
                dataFlag = 2
                break
            input = formatMail(input)
            wf.write(input + "\n")
    wf.write("</body></html>")
    wf.close()
    print fileName + " download"
   
def downloadMail(project, year, month):
    os.system("mkdir mail")
    url = "http://lists.openstack.org/pipermail/%s/%s-%s/thread.html" % (project, year, month)
    result = execute("curl " + url)
    array = result.split("\n")
    wf = open("%s-%s.html" % (year, month), 'w')
    wf.write("<html><head><title>Mail list</title></head>")
    wf.write("<style>body{font-size:12px;text-decoration:none;}a{text-decoration:none;}</style><body><a target=\"left\" href=\"" + year + "-" + month + ".html#finally\">Go to end</a>")
    dataFlag = 0
    for input in array:
        if dataFlag == 0:
            if input.find("Messages:") != -1:
                dataFlag = 1
                wf.write(input + "\n")
        elif dataFlag == 1:
            if input.find("Last message date:") != -1:
                dataFlag = 2
                break
            start = input.find("<A HREF=\"")
            if start != -1:
                end = input.find("\">", start)
                mailID = input[start + 9:end]
                downloadOneMail(project, year, month, mailID)
                input = input.replace("<A HREF=\"", "<A target=\"right\" HREF=\"mail/")
            input = markData(input)
            wf.write(input + "\n")
    wf.write("<a name=\"finally\"></a></body></html>")
    wf.close()
    wf = open("index.html", 'w')
    wf.write("<html><head><title>mail</title></head>\n")
    wf.write("<frameset cols=\"60%,*\"> <frame name=\"left\" src=\"" + year + "-" + month + ".html\">")
    wf.write("<frame name=\"right\" src=\"list.html\"></frameset></html>")
    wf.close()
    wf = open("list.html", 'w')
    wf.write("<html><head><title>list</title></head><body>\n")
    listfiles = os.listdir(".")
    for htmlfile in listfiles:
        if os.path.isfile(htmlfile):
            if htmlfile.find(".html") != -1 and htmlfile != "index.html" and htmlfile != "list.html":
                wf.write("<a target=\"left\" href=\"" + htmlfile + "\">" + htmlfile + "</a><br>")
    wf.write("</body></html>")
    wf.close()

downloadMail("openstack-dev", "2014", "August")


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值