⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listing23-2.py

📁 《Beginning Python--From Novice to Professional》 的源码
💻 PY
字号:
from nntplib import NNTPfrom time import strftime, time, localtimefrom email import message_from_stringfrom urllib import urlopenimport textwrapimport reday = 24 * 60 * 60 # Number of seconds in one daydef wrap(string, max=70):    """    Wraps a string to a maximum line width.    """    return '\n'.join(textwrap.wrap(string)) + '\n'class NewsAgent:    """    An object that can distribute news items from news    sources to news destinations.    """    def __init__(self):        self.sources = []        self.destinations = []    def addSource(self, source):        self.sources.append(source)    def addDestination(self, dest):        self.destinations.append(dest)    def distribute(self):        """        Retrieve all news items from all sources, and        Distribute them to all destinations.        """        items = []        for source in self.sources:            items.extend(source.getItems())        for dest in self.destinations:            dest.receiveItems(items)class NewsItem:    """    A simple news item consisting of a title and a body text.    """    def __init__(self, title, body):        self.title = title        self.body = bodyclass NNTPSource:    """    A news source that retrieves news items from an NNTP group.    """    def __init__(self, servername, group, window):        self.servername = servername        self.group = group        self.window = window    def getItems(self):        start = localtime(time() - self.window*day)        date = strftime('%y%m%d', start)        hour = strftime('%H%M%S', start)        server = NNTP(self.servername)        ids = server.newnews(self.group, date, hour)[1]        for id in ids:            lines = server.article(id)[3]            message = message_from_string('\n'.join(lines))            title = message['subject']            body = message.get_payload()            if message.is_multipart():                body = body[0]            yield NewsItem(title, body)        server.quit()class SimpleWebSource:    """    A news source that extracts news items from a Web page using    regular expressions.    """    def __init__(self, url, titlePattern, bodyPattern):        self.url = url        self.titlePattern = re.compile(titlePattern)        self.bodyPattern = re.compile(bodyPattern)    def getItems(self):        text = urlopen(self.url).read()        titles = self.titlePattern.findall(text)        bodies = self.bodyPattern.findall(text)        for title, body in zip(titles, bodies):            yield NewsItem(title, wrap(body))class PlainDestination:    """    A news destination that formats all its news items as    plain text.    """    def receiveItems(self, items):        for item in items:            print item.title            print '-'*len(item.title)            print item.bodyclass HTMLDestination:    """    A news destination that formats all its news items    as HTML.    """    def __init__(self, filename):        self.filename = filename    def receiveItems(self, items):        out = open(self.filename, 'w')        print >> out, """        <html>          <head>            <title>Today's News</title>          </head>          <body>          <h1>Today's News</h1>        """        print >> out, '<ul>'        id = 0        for item in items:            id += 1            print >> out, '  <li><a href="#%i">%s</a></li>' % (id, item.title)        print >> out, '</ul>'        id = 0        for item in items:            id += 1            print >> out, '<h2><a name="%i">%s</a></h2>' % (id, item.title)            print >> out, '<pre>%s</pre>' % item.body        print >> out, """          </body>        </html>        """def runDefaultSetup():    """    A default setup of sources and destination. Modify to taste.    """    agent = NewsAgent()    # A SimpleWebSource that retrieves news from the    # BBC news site:    bbc_url = 'http://news.bbc.co.uk/text_only.stm'    bbc_title = r'(?s)a href="[^"]*">\s*<b>\s*(.*?)\s*</b>'    bbc_body = r'(?s)</a>\s*<br />\s*(.*?)\s*<'    bbc = SimpleWebSource(bbc_url, bbc_title, bbc_body)    agent.addSource(bbc)    # An NNTPSource that retrieves news from comp.lang.python.announce:    clpa_server = 'news.foo.bar' # Insert real server name    clpa_group = 'comp.lang.python.announce'    clpa_window = 1    clpa = NNTPSource(clpa_server, clpa_group, clpa_window)    agent.addSource(clpa)    # Add plain text destination and an HTML destination:    agent.addDestination(PlainDestination())    agent.addDestination(HTMLDestination('news.html'))    # Distribute the news items:    agent.distribute()if __name__ == '__main__': runDefaultSetup()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -