python之nntp服务器组

来源:互联网 发布:夜来风雨声花落知多少 编辑:程序博客网 时间:2024/05/17 02:14

 

  1. http://www.newzbot.com/serverlist.php?since=ALL&orderby=kps&sortorder=desc&show_maxgroup=on&show_post=on&show_kps=on&show_created=on 
这是可以找到当前有哪些服务器的地方网址,

这个项目的目的就是收集信息,并且将其生成一个html的报告(当然也可以是其他的形式的报告),完成代码如下

  1. '''''  
  2. Created on 2012-7-18  
  3.  
  4. @author: mars  
  5. ''' 
  6. import nntplib  
  7. from nntplib import NNTP  
  8. from time import time,strftime,localtime  
  9. from email import message_from_string  
  10. from urllib import urlopen  
  11. import textwrap  
  12. import re  
  13.  
  14. day=24*60*60 
  15. def wrap(string,max=70):  
  16.     #make the string to the max linewidth  
  17.     return '\n'.join(textwrap.wrap(string))+'\n' 
  18. class NewsAgent:  
  19.     #can get the new project and announce to the object fo the new from the souuce of the news  
  20.     def __init__(self):  
  21.         self.sources=[]  
  22.         self.destinations=[]  
  23.     def addSource(self,source):  
  24.         self.sources.append(source)  
  25.     def addDestination(self,dest):  
  26.         self.destinations.append(dest)  
  27.     def distribute(self):  
  28.         items=[]  
  29.         for source in self.sources:  
  30.             items.extend(source.getItems())  
  31.         for dest in self.destinations:  
  32.             dest.receiveItems(items)  
  33. class NewsItem:  
  34.     #simle news project including tile and text  
  35.     def __init__(self,title,body):  
  36.         self.title=title  
  37.         self.body=body  
  38.  
  39. class NNTPSource:  
  40.     #the nntp source   
  41.     def __init__(self,servername,group,window):  
  42.         self.servername=servername  
  43.         self.group=group  
  44.         self.window=window  
  45.     def getItems(self):  
  46.         start=localtime(time()-self.window*day)  
  47.         date=strftime('%y%m%d',start)  
  48.         hour=strftime('%H%M%S',start)  
  49.           
  50.         server=NNTP(self.servername)  
  51.           
  52.         ids=server.group(self.group)[2]  
  53.         #ids=server.newnews(self.group, date, hour)[1]  
  54.           
  55.         for id in ids:  
  56.             lines=server.article(id)[3]  
  57.             message=message_from_string('\n'.join(lines))  
  58.               
  59.             title=message['subject']  
  60.             body=message.get_payload()  
  61.             if message.is_multipart():  
  62.                 body=body[0]  
  63.                   
  64.             yield NewsItem(title,body)  
  65.               
  66.         server.quit()  
  67.  
  68. class SimpleWebSource:  
  69.     #user the re  to fetch thr source from the webpage  
  70.     def __init__(self,url,titlePattern,bodyPattern):  
  71.         self.url=url  
  72.         self.titlePattern=re.compile(titlePattern)  
  73.         self.bodyPattern=re.compile(bodyPattern)  
  74.     def getItems(self):  
  75.         text=urlopen(self.url).read()  
  76.         titles=self.titlePattern.findall(text)  
  77.         bodies=self.bodyPattern.findall(text)  
  78.         for title,body in zip(titles,bodies):  
  79.             yield NewsItem(title.wrap(body))  
  80. class PlainDestination:  
  81.     #make it to the pure text  
  82.     def receiveItems(self,items):  
  83.         for item in items:  
  84.             print item.title  
  85.             #print '-'*len(subject)  
  86.             #print '-'*len(item.title)  
  87.             print item.body  
  88.             #print 'fuck&&&&&&&bitch'  
  89. class HTMLDestination:  
  90.     # make it to the html   
  91.     def __init__(self, filename):  
  92.         self.filename = filename  
  93.           
  94.     def receiveItems(self, items):  
  95.         out = open(self.filename, 'w')  
  96.         print >> out, """  
  97.         <html>  
  98.             <head>  
  99.                 <title>Today's News</title>  
  100.             </head>  
  101.             <body>  
  102.             <h1>Today's News</h1>  
  103.         """ 
  104.       
  105.         print >> out, '<ul>' 
  106.         id = 0 
  107.         for item in items:  
  108.             id += 1 
  109.             print >> out, '<li><a href="#%i">%s</a></li>' % (id, item.title)  
  110.         print >> out, '</ul>' 
  111.           
  112.         id = 0 
  113.         for item in items:  
  114.             id += 1 
  115.             print >> out, '<h2><a name="%i">%s</a></h2>' % (id, item.title)  
  116.             print >> out, '<pre>%s</pre>' % item.body  
  117.               
  118.         print >> out, """  
  119.             </body>  
  120.         </html>  
  121.         """ 
  122.           
  123. class runDefaultSetup():  
  124.     #the souce  can modify by yourself  
  125.     agent=NewsAgent()  
  126.     #bbc_url='http://www.chinanews.com/'  
  127.     bbc_url='http://www.bbc.co.uk/news/' 
  128.     #bbc_url='http://www.bbc.co.uk/text_only.stm'  
  129.     bbc_title=r'(?s)a href="[^"]*>\s*<b>\s*(.*?)\s*</b>' 
  130.     bbc_body=r'(?s)</a>\s*<br/>\s*(.*?)\s*<' 
  131.     bbc=SimpleWebSource(bbc_url,bbc_title,bbc_body)  
  132.       
  133.     agent.addSource(bbc)  
  134.     #cong gmane.comp.python.announce get the nntpsource  
  135.       
  136.     clpa_server='news.gmane.org' 
  137.     clpa_group='gmane.comp.python.apple' 
  138.       
  139.     clpa_window=1 
  140.     clpa=NNTPSource(clpa_server,clpa_group,clpa_window)  
  141.     agent.addSource(clpa)  
  142.       
  143.       
  144.     #add the text and html target  
  145.     agent.addDestination(PlainDestination())  
  146.       
  147.     agent.addDestination(HTMLDestination('news.html'))  
  148.       
  149.     #public  
  150.     agent.distribute()  
  151. if __name__=='__main__':  
  152.     runDefaultSetup()  
  153.           
  154.  
  155.  

其实这个程序呢 在第二版的教程上有,不过呢 那个给出的服务器不能用,所以在文章的开始的时候我就给出了 可以找到服务器地址的地方,比如我这里用的就是

    clpa_server='news.gmane.org'
    clpa_group='gmane.comp.python.apple'
    这个!

当然这段代码我也稍微说下,最开始的类NewsAgent,接着是NewsItem,NNTPSource,SimpleWebSource,PlainDestination,HTMLDestination和runDefaultSetup

程序一运行就开始跑的是runDefaultSetup,这里就将NewsAgent实例化为agent,SimpleWebSource的3个参数分别是url,  title和body,然后将其实例化为bbc!

随后将bbc作为参数,调用agent的addsource。同样的道理完成了nntpsouce这一块。

最后就是就是调用agent.addDestionation。最后HTMLDestionation以news.html作为生成报告的html文本!

本文出自 “LuoZhengWu” 博客,请务必保留此出处http://brucemars.blog.51cto.com/5288106/963931

0 0
原创粉丝点击