-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.py
108 lines (87 loc) · 2.73 KB
/
simple.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: itabas <[email protected]>
from nntplib import NNTP
from time import strftime, time, localtime
import datetime, io, os, sys
class StringBuilder:
_file_str = None
def __init__(self):
self._file_str = io.StringIO()
def Append(self, str):
self._file_str.write(str)
def __str__(self):
return self._file_str.getvalue()
week = 7 * 24 * 60 * 60 # Number of seconds in one day
start = localtime(time() - week)
date = strftime('%y%m%d', start)
time = strftime('%H%M%S', start)
full_date = date + time
servername = 'news.aioe.org'
group = 'comp.lang.python.announce'
server = NNTP(servername)
ids = server.newnews(group, datetime.datetime.strptime(full_date, '%y%m%d%H%M%S'))[1]
content = StringBuilder()
content.Append(
'''
<html>
<head>
<title>Week's News - Python Announce</title>
</head>
<body>
<h1>Week's News - Python Announce</h1>
''')
content.Append('''
<ul>
''')
for id in ids:
article = StringBuilder()
subject = ''
publish_time = ''
head = server.head(id)
if len(head) < 1: break
for line in head[1].lines:
line = line.decode('utf-8')
if line.lower().startswith('from:'):
mail_from = line[6:]
if line.lower().startswith('date:'):
publish_time = line[6:]
if line.lower().startswith('subject:'):
subject = line[9:]
if line.lower().startswith('organization:'):
organization = line[14:]
break
content.Append(' <li><a href="%s.html">%s</a></li>\n' % (datetime.datetime.strptime(publish_time, '%a, %d %b %Y %H:%M:%S %z').strftime('%Y%m%d%H%M'), subject))
body = server.body(id)[1].lines
article.Append(
'''
<html>
<head>
<title>%s</title>
</head>
<body>
<h1>%s</h1>
<ul>
<li>from: %s</li>
<li>organization: %s</li>
<li>date: %s</li>
</ul>
<p>%s</p>
</body>
<html>
''' % (subject, subject, mail_from, organization, publish_time, body))
article_file = '%s\\sample\\%s.html' % (sys.path[0], datetime.datetime.strptime(publish_time, '%a, %d %b %Y %H:%M:%S %z').strftime('%Y%m%d%H%M'))
os.makedirs(os.path.dirname(article_file), exist_ok=True)
with open(article_file, 'w') as f:
f.write(article.__str__())
content.Append('''
</ul>
''')
content.Append('''
</body>
</html>''')
index_file = '%s\\sample\\index.html' % sys.path[0]
os.makedirs(os.path.dirname(index_file), exist_ok=True)
with open(index_file, 'w') as f:
f.write(content.__str__())
server.quit()