2 #######################################################################
8 # This is a program to create a directory full of HTML pages
9 # which between them contain links to all the newsgroups available
12 # The -a option causes a complete list of all groups to be read from
13 # the server rather than just the ones which have appeared since last
14 # execution. This recreates the local list from scratch. Use this on
15 # the first invocation of the program, and from time to time thereafter.
16 # When new groups are first created they may appear on your server as
17 # empty groups. By default, empty groups are ignored by the -a option.
18 # However, these new groups will not be created again, and so will not
19 # appear in the server's list of 'new groups' at a later date. Hence it
20 # won't appear until you do a '-a' after some articles have appeared.
22 # I should really keep a list of ignored empty groups and re-check them
23 # for articles on every run, but I haven't got around to it yet.
25 # This assumes an NNTP news feed.
27 # Feel free to copy, distribute and modify this code for
28 # non-commercial use. If you make any useful modifications, let me
31 # (c) Quentin Stafford-Fraser 1994
32 # fraser@europarc.xerox.com qs101@cl.cam.ac.uk
34 #######################################################################
35 import sys
, nntplib
, marshal
, time
, os
37 #######################################################################
38 # Check these variables before running! #
41 # Filenames which don't start with / are taken as being relative to this.
42 topdir
= os
.path
.expanduser('~/newspage')
44 # The name of your NNTP host
46 # newshost = 'nntp-serv.cl.cam.ac.uk'
47 # or use following to get the name from the NNTPSERVER environment
49 # newshost = os.environ['NNTPSERVER']
50 newshost
= 'news.example.com'
52 # The filename for a local cache of the newsgroup list
53 treefile
= 'grouptree'
55 # The filename for descriptions of newsgroups
56 # I found a suitable one at ftp.uu.net in /uunet-info/newgroups.gz
57 # You can set this to '' if you don't wish to use one.
58 descfile
= 'newsgroups'
60 # The directory in which HTML pages should be created
62 # pagedir = '/usr/local/lib/html/newspage'
66 # The html prefix which will refer to this directory
68 # httppref = '/newspage/',
69 # or leave blank for relative links between pages: (Recommended)
73 # The name of the 'root' news page in this directory.
74 # A .html suffix will be added.
77 # Set skipempty to 0 if you wish to see links to empty groups as well.
78 # Only affects the -a option.
81 # pagelinkicon can contain html to put an icon after links to
82 # further pages. This helps to make important links stand out.
83 # Set to '' if not wanted, or '...' is quite a good one.
84 pagelinkicon
= '... <img src="http://pelican.cl.cam.ac.uk/icons/page.xbm"> '
86 # ---------------------------------------------------------------------
87 # Less important personal preferences:
89 # Sublistsize controls the maximum number of items the will appear as
90 # an indented sub-list before the whole thing is moved onto a different
91 # page. The smaller this is, the more pages you will have, but the
92 # shorter each will be.
95 # That should be all. #
96 #######################################################################
98 for dir in os
.curdir
, os
.environ
['HOME']:
99 rcfile
= os
.path
.join(dir, '.newslistrc.py')
100 if os
.path
.exists(rcfile
):
105 from nntplib
import NNTP
108 rcsrev
= '$Revision$'
109 rcsrev
= ' '.join(filter(lambda s
: '$' not in s
, rcsrev
.split()))
112 # Make (possibly) relative filenames into absolute ones
113 treefile
= os
.path
.join(topdir
,treefile
)
114 descfile
= os
.path
.join(topdir
,descfile
)
115 page
= os
.path
.join(topdir
,pagedir
)
117 # First the bits for creating trees ---------------------------
119 # Addtotree creates/augments a tree from a list of group names
120 def addtotree(tree
, groups
):
121 print 'Updating tree...'
124 makeleaf(tree
, parts
)
126 # Makeleaf makes a leaf and the branch leading to it if necessary
127 def makeleaf(tree
,path
):
136 makeleaf(tree
[j
],path
[1:])
138 # Then the bits for outputting trees as pages ----------------
140 # Createpage creates an HTML file named <root>.html containing links
141 # to those groups beginning with <root>.
143 def createpage(root
, tree
, p
):
144 filename
= os
.path
.join(pagedir
, root
+'.html')
148 detail
= ' under ' + root
149 with
open(filename
, 'w') as f
:
150 # f.write('Content-Type: text/html\n')
151 f
.write('<html>\n<head>\n')
152 f
.write('<title>Newsgroups available%s</title>\n' % detail
)
153 f
.write('</head>\n<body>\n')
154 f
.write('<h1>Newsgroups available%s</h1>\n' % detail
)
155 f
.write('<a href="%s%s.html">Back to top level</a><p>\n' %
156 (httppref
, rootpage
))
157 printtree(f
, tree
, 0, p
)
159 f
.write("<i>This page automatically created by 'newslist' v. %s." %
161 f
.write(time
.ctime(time
.time()) + '</i>\n')
162 f
.write('</body>\n</html>\n')
164 # Printtree prints the groups as a bulleted list. Groups with
165 # more than <sublistsize> subgroups will be put on a separate page.
166 # Other sets of subgroups are just indented.
168 def printtree(f
, tree
, indent
, p
):
171 if l
> sublistsize
and indent
> 0:
172 # Create a new page and a link to it
173 f
.write('<li><b><a href="%s%s.html">' % (httppref
, p
[1:]))
174 f
.write(p
[1:] + '.*')
175 f
.write('</a></b>%s\n' % pagelinkicon
)
176 createpage(p
[1:], tree
, p
)
185 f
.write('<li>%s\n<ul>' % p
[1:])
194 f
.write('<li><a href="news:%s">%s</a> ' % (p
[1:], p
[1:]))
196 f
.write(' <i>%s</i>\n' % desc
[p
[1:]])
201 printtree(f
, tree
[i
], indent
, p
+'.'+i
)
206 # Reading descriptions file ---------------------------------------
208 # This returns a dict mapping group name to its description
210 def readdesc(descfile
):
218 with
open(descfile
, 'r') as d
:
219 print 'Reading descriptions...'
224 dsc
= ' '.join(bits
[1:])
230 print 'Failed to open description file ' + descfile
233 # Check that ouput directory exists, ------------------------------
234 # and offer to create it if not
236 def checkopdir(pagedir
):
237 if not os
.path
.isdir(pagedir
):
238 print 'Directory %s does not exist.' % pagedir
239 print 'Shall I create it for you? (y/n)'
240 if sys
.stdin
.readline()[0] == 'y':
242 os
.mkdir(pagedir
, 0777)
244 print 'Sorry - failed!'
250 # Read and write current local tree ----------------------------------
252 def readlocallist(treefile
):
253 print 'Reading current local group list...'
256 treetime
= time
.localtime(os
.stat(treefile
)[ST_MTIME
])
258 print '\n*** Failed to open local group cache '+treefile
259 print 'If this is the first time you have run newslist, then'
260 print 'use the -a option to create it.'
262 treedate
= '%02d%02d%02d' % (treetime
[0] % 100, treetime
[1], treetime
[2])
264 with
open(treefile
, 'rb') as dump
:
265 tree
= marshal
.load(dump
)
267 print 'Cannot open local group list ' + treefile
268 return (tree
, treedate
)
270 def writelocallist(treefile
, tree
):
272 with
open(treefile
, 'wb') as dump
:
273 groups
= marshal
.dump(tree
, dump
)
274 print 'Saved list to %s\n' % treefile
276 print 'Sorry - failed to write to local group cache', treefile
277 print 'Does it (or its directory) have the correct permissions?'
280 # Return list of all groups on server -----------------------------
282 def getallgroups(server
):
283 print 'Getting list of all groups...'
285 info
= server
.list()[1]
287 print 'Processing...'
289 print '\nIgnoring following empty groups:'
291 grpname
= i
[0].split()[0]
292 if skipempty
and int(i
[1]) < int(i
[2]):
295 groups
.append(grpname
)
298 print '(End of empty groups)'
301 # Return list of new groups on server -----------------------------
303 def getnewgroups(server
, treedate
):
304 print 'Getting list of new groups since start of %s...' % treedate
,
305 info
= server
.newgroups(treedate
, '000001')[1]
306 print 'got %d.' % len(info
)
307 print 'Processing...',
310 grpname
= i
.split()[0]
311 groups
.append(grpname
)
315 # Now the main program --------------------------------------------
320 # Check that the output directory exists
324 print 'Connecting to %s...' % newshost
325 if sys
.version
[0] == '0':
326 s
= NNTP
.init(newshost
)
330 except (nntplib
.error_temp
, nntplib
.error_perm
), x
:
331 print 'Error connecting to host:', x
332 print 'I\'ll try to use just the local list.'
335 # If -a is specified, read the full list of groups from server
336 if connected
and len(sys
.argv
) > 1 and sys
.argv
[1] == '-a':
337 groups
= getallgroups(s
)
339 # Otherwise just read the local file and then add
340 # groups created since local file last modified.
343 (tree
, treedate
) = readlocallist(treefile
)
345 groups
= getnewgroups(s
, treedate
)
348 addtotree(tree
, groups
)
349 writelocallist(treefile
,tree
)
351 # Read group descriptions
354 print 'Creating pages...'
355 createpage(rootpage
, tree
, '')
358 if __name__
== "__main__":
362 ######################################################################