-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnntp.py
246 lines (189 loc) · 6.54 KB
/
nntp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import nntplib
from urllib.request import urlopen
import textwrap
import re
from email.header import decode_header
import chardet
import base64
import configparser
import webbrowser
import time
import os
# 读取文件获得服务器地址
def getAddress():
config = configparser.ConfigParser()
config.read('config.ini')
nntps = []
for value in config.items('serverAddress'):
nntps.append(value[1])
return nntps
# 添加新的服务器地址
def addNewAddress(newAddress, nntps):
count = len(nntps)
nntps.append(newAddress)
with open('config.ini', 'a', encoding='utf-8') as configFile:
configFile.write(f"web{count+1} = {newAddress}\n")
def DeleteAddress(Num, nntps):
# 创建 ConfigParser 对象
config = configparser.ConfigParser()
# 读取 config.ini 文件
config.read('config.ini')
# 指定要删除选项的节名称和选项名称
section = 'serverAddress'
option_to_delete = 'Web{}'.format(Num)
if config.has_section(section) and config.has_option(section, option_to_delete):
config.remove_option(section, option_to_delete)
with open('config.ini', 'w') as configfile:
config.write(configfile)
nntps.pop(Num - 1)
def is_base64(str):
if ':' in str:
return False
else:
return True
def coding(str):
if is_base64(str):
decoded_bytes = base64.b64decode(str)
decoded_string = decoded_bytes.decode('UTF-8', errors = 'replace')
return decoded_string
else:
return str
class NewsAgent:
"""
可将新闻源中的新闻分发到新闻目的地的对象
"""
def __init__(self):
self.sources = []
self.destinations = []
def add_source(self, source):
self.sources.append(source)
def addDestiantion(self, Dest):
self.destinations.append(Dest)
def distribute(self):
"""
从所有新闻源获取所有的新闻,并将其分发到所有的新闻目的地
"""
items = []
for source in self.sources:
items.extend(source.get_items())
for dest in self.destinations:
dest.receive_items(items)
class NewsItem:
"""
由标题和正文组成的简单新闻
"""
def __init__(self, title, body):
self.title = title
self.body = body
class NNTPSource:
"""
从NNTP新闻源获取新闻的类
"""
def __init__(self, servername, group, howmany):
self.servername = servername
self.group = group
self.howmany = howmany
def get_items(self):
server = nntplib.NNTP(self.servername)
resp, count, first, last, name = server.group(self.group)
start = last - self.howmany + 1
resp, overviews = server.over((start, last))
for id, over in overviews:
title = decode_header(over['subject'])
resp, info = server.body(id)
body = '\n'.join(line.decode('latin')
for line in info.lines) + '\n\n'
yield NewsItem(title, body)
server.quit()
class SimpleWebSource:
"""
使用正则表达式从网页提取新闻的新闻源
"""
def __init__(self, url, title_pattern, body_pattern, encoding = 'utf-8'):
self.url = url
self.title_pattern = re.compile(title_pattern)
self.body_pattern = re.compile(body_pattern)
self.encoding = encoding
def get_items(self):
raw_data = urlopen(self.url).read()
result = chardet.detect(raw_data)
encoding = result['encoding']
text = raw_data.decode(encoding)
titles = self.title_pattern.findall(text)
bodies = self.body_pattern.findall(text)
for title, body in zip(titles, bodies):
yield NewsItem(title, textwrap.fill(body) + '\n')
class PlainDestination:
"""
以纯文本方式显示所有新闻的新闻目的地
"""
def receive_items(self, items):
for item in items:
print(item.title)
print('-' * len(item.title))
print(item.body)
class HTMLDestination:
"""
以HTML格式显示所有新闻的新闻目的地
"""
def __init__(self, filename):
self.filename = filename
def receive_items(self, items):
out = open(self.filename, 'w', encoding = 'utf-8', errors = 'replace')
print("""
<html>
<head>
<title>Today's News</title>
</head>
<body>
<h1>Today's News</h1>
""", file = out)
print('<ul>', file = out)
id = 0
for item in items:
id +=1
print(' <li><a href="#{}">{}</a></li>'
.format(id, item.title), file = out)
print('</ul>', file = out)
id = 0
for item in items:
id += 1
print('<h2><a name="{}">{}</a></h2>'
.format(id, item.title), file = out)
print('<pre>{}</pre>'.format(coding(item.body)), file = out)
print("""
</body>
</html>
""", file = out)
def runDefaultSetup(servername, groupName):
"""
默认的新闻源和目的地设施,根据偏好修改
"""
agent = NewsAgent()
# 从人民网获取新闻的SimpleWebSource对象:
people_url = 'http://www.people.com.cn'
people_title = r'<h2><a href="[^"]*"\s*>(.*?)</a>'
people_body = r'</h2><p>(.*?)</p>'
people = SimpleWebSource(people_url, people_title, people_body)
agent.add_source(people)
# 从comp.lang.python.announce获取新闻的NNTPSource对象:
clpa_server = servername
# comp.lang.python.announce
clpa_group = groupName
clpa_howmany = 10
clpa = NNTPSource(clpa_server, clpa_group, clpa_howmany)
fileName = 'news({}).html'.format(clpa_server.replace('.', '-'))
agent.add_source(clpa)
# 添加纯文本目的地和HTML目的地:
agent.addDestiantion(PlainDestination())
agent.addDestiantion(HTMLDestination(fileName))
# 分发新闻
agent.distribute()
#展示新闻
time.sleep(3)
current_dir = os.path.abspath(os.getcwd())
file_path = os.path.join(current_dir, fileName)
file_url = 'file://' + file_path
webbrowser.open(file_url)
if __name__ == '__main__':
runDefaultSetup('freenews.netfront.net', 'comp.lang.python.announce')