Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
python-test
Project
Project
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
Graph
比较
统计图
议题
0
议题
0
列表
看板
标记
Milestones
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
songchuancai
python-test
Commits
e6b59665
提交
e6b59665
authored
7月 12, 2022
作者:
songchuancai
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
更新 main.py
上级
fbbc8a4d
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
33 行增加
和
148 行删除
+33
-148
main.py
main.py
+33
-148
没有找到文件。
main.py
浏览文件 @
e6b59665
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论