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
, string
, marshal
, time
, os
, posix
, string
37 #######################################################################
38 # Check these variables before running! #
41 # Filenames which don't start with / are taken as being relative to this.
42 topdir
='/anfs/qsbigdisc/web/html/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 = posix.environ['NNTPSERVER']
50 newshost
= 'nntp-serv.cl.cam.ac.uk'
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
= string
.join(filter(lambda s
: '$' not in s
, string
.split(rcsrev
)))
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...'
123 parts
= string
.splitfields(i
,'.')
124 makeleaf(tree
, parts
)
126 # Makeleaf makes a leaf and the branch leading to it if necessary
127 def makeleaf(tree
,path
):
131 if not tree
.has_key(j
):
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 f
= open(filename
,'w')
150 # f.write('Content-Type: text/html\n')
151 f
.write('<TITLE>Newsgroups available' + detail
+ '</TITLE>\n')
152 f
.write('<H1>Newsgroups available' + detail
+'</H1>\n')
153 f
.write('<A HREF="'+httppref
+rootpage
+'.html">Back to top level</A><P>\n')
154 printtree(f
,tree
,0,p
)
155 f
.write('<I>This page automatically created by \'newslist\' v. '+rcsrev
+'.')
156 f
.write(time
.ctime(time
.time()) + '</I><P>')
159 # Printtree prints the groups as a bulleted list. Groups with
160 # more than <sublistsize> subgroups will be put on a separate page.
161 # Other sets of subgroups are just indented.
163 def printtree(f
, tree
, indent
, p
):
167 if l
> sublistsize
and indent
>0:
168 # Create a new page and a link to it
169 f
.write('<LI><B><A HREF="'+httppref
+p
[1:]+'.html">')
171 f
.write('</A></B>'+pagelinkicon
+'\n')
172 createpage(p
[1:], tree
, p
)
181 f
.write('<LI>'+p
[1:]+'\n<UL>')
190 f
.write('<LI><A HREF="news:' + p
[1:] + '">'+ p
[1:] + '</A> ')
191 if desc
.has_key(p
[1:]):
192 f
.write(' <I>'+desc
[p
[1:]]+'</I>\n')
197 printtree(f
,tree
[i
], indent
, p
+'.'+i
)
202 # Reading descriptions file ---------------------------------------
204 # This returns an array mapping group name to its description
206 def readdesc(descfile
):
215 d
= open(descfile
, 'r')
216 print 'Reading descriptions...'
218 print 'Failed to open description file ' + descfile
222 bits
= string
.split(l
)
225 dsc
= string
.join(bits
[1:])
232 # Check that ouput directory exists, ------------------------------
233 # and offer to create it if not
235 def checkopdir(pagedir
):
236 if not os
.path
.isdir(pagedir
):
237 print 'Directory '+pagedir
+' does not exist.'
238 print 'Shall I create it for you? (y/n)'
239 if sys
.stdin
.readline()[0] == 'y':
241 os
.mkdir(pagedir
,0777)
243 print 'Sorry - failed!'
249 # Read and write current local tree ----------------------------------
251 def readlocallist(treefile
):
252 print 'Reading current local group list...'
255 treetime
= time
.localtime(os
.stat(treefile
)[ST_MTIME
])
257 print '\n*** Failed to open local group cache '+treefile
258 print 'If this is the first time you have run newslist, then'
259 print 'use the -a option to create it.'
261 treedate
= '%02d%02d%02d' % (treetime
[0] % 100 ,treetime
[1], treetime
[2])
263 dump
= open(treefile
,'r')
264 tree
= marshal
.load(dump
)
267 print 'Cannot open local group list ' + treefile
268 return (tree
, treedate
)
270 def writelocallist(treefile
, tree
):
272 dump
= open(treefile
,'w')
273 groups
= marshal
.dump(tree
,dump
)
275 print 'Saved list to '+treefile
+'\n'
277 print 'Sorry - failed to write to local group cache '+treefile
278 print 'Does it (or its directory) have the correct permissions?'
281 # Return list of all groups on server -----------------------------
283 def getallgroups(server
):
284 print 'Getting list of all groups...'
286 info
= server
.list()[1]
288 print 'Processing...'
290 print '\nIgnoring following empty groups:'
292 grpname
= string
.split(i
[0])[0]
293 if skipempty
and string
.atoi(i
[1]) < string
.atoi(i
[2]):
296 groups
.append(grpname
)
299 print '(End of empty groups)'
302 # Return list of new groups on server -----------------------------
304 def getnewgroups(server
, treedate
):
305 print 'Getting list of new groups since start of '+treedate
+'...',
306 info
= server
.newgroups(treedate
,'000001')[1]
307 print 'got '+`
len(info
)`
+'.'
308 print 'Processing...',
311 grpname
= string
.split(i
)[0]
312 groups
.append(grpname
)
316 # Now the main program --------------------------------------------
323 # Check that the output directory exists
327 print 'Connecting to '+newshost
+'...'
328 if sys
.version
[0] == '0':
329 s
= NNTP
.init(newshost
)
333 except (nntplib
.error_temp
, nntplib
.error_perm
), x
:
334 print 'Error connecting to host:', x
335 print 'I\'ll try to use just the local list.'
338 # If -a is specified, read the full list of groups from server
339 if connected
and len(sys
.argv
) > 1 and sys
.argv
[1] == '-a':
341 groups
= getallgroups(s
)
343 # Otherwise just read the local file and then add
344 # groups created since local file last modified.
347 (tree
, treedate
) = readlocallist(treefile
)
349 groups
= getnewgroups(s
, treedate
)
352 addtotree(tree
, groups
)
353 writelocallist(treefile
,tree
)
355 # Read group descriptions
358 print 'Creating pages...'
359 createpage(rootpage
, tree
, '')
366 ######################################################################