提交 e6b59665 authored 作者: songchuancai's avatar songchuancai

更新 main.py

上级 fbbc8a4d
from nntplib import NNTP
from time import strftime,time,localtime
from email import message_from_string
from urllib import urlopen from urllib import urlopen
import textwrap from reportlab.graphics.shapes import *
import re from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.charts.textlabels import Label
day = 24*60*60 from reportlab.graphics import renderPDF
def wrap(string,max=70): URL = 'http://www.swpc.noaa.gov/ftpdir/weekly/Predict.txt'
''' COMMENT_CHARS = '#:'
''' drawing = Drawing(400, 200)
return '\n'.join(textwrap.wrap(string)) + '\n' data = []
for line in urlopen(URL).readlines():
class NewsAgent: if not line.isspace() and not line[0] in COMMENT_CHARS:
''' data.append([float(n) for n in line.split()])
'''
def __init__(self): pred = [row[2] for row in data]
self.sources = [] high = [row[3] for row in data]
self.destinations = [] low = [row[4] for row in data]
times = [row[0] + row[1]/12.0 for row in data]
def addSource(self,source): lp = LinePlot()
self.sources.append(source) lp.x = 50
lp.y = 50
def addDestination(self,dest): lp.height = 125
self.destinations.append(dest) lp.width = 300
lp.data = [zip(times, pred),zip(times,high),zip(times, low)]
def distribute(self): lp.lines[0].strokeColor = colors.blue
lp.lines[1].strokeColor = colors.red
items = [] lp.lines[2].strokeColor = colors.green
for source in self.sources:
items.extend(source.getItems()) drawing.add(lp)
for dest in self.destinations: drawing.add(String(250,150, 'Sunspots',fontSize=14,fillColor=colors.red))
dest.receiveItems(items)
renderPDF.drawToFile(drawing, 'report3.pdf','Sunspots')
class NewsItem: \ No newline at end of file
def __init__(self,title,body):
self.title = title
self.body = body
class NNTPSource:
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:
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:
def receiveItems(self,items):
for item in items:
print item.title
print '-'*len(item.title)
print item.body
class HTMLDestination:
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</hi>
'''
print >> out, '<ul>'
id = 0
for item in items:
id += 1
print >> out, '<li><a href="#">%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():
agent = NewsAgent()
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)
clpa_server = 'news2.neva.ru'
clpa_group = 'alt.sex.telephone'
clpa_window = 1
clpa = NNTPSource(clpa_server,clpa_group,clpa_window)
agent.addSource(clpa)
agent.addDestination(PlainDestination())
agent.addDestination(HTMLDestination('news.html'))
agent.distribute()
if __name__ == '__main__':
runDefaultSetup()
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论