nntplib: abstraction of threads

Rakesh rakesh_usenet at yahoo.com
Sat Jan 15 20:52:47 EST 2005


For a particular application of mine, I need to get the messages from
usenet , (and group them by each thread) . My startup python code looks
as follows.

<--- Startup code to read messages from a newsgroup -->

import nntplib, cStringIO, rfc822, sys

SRVR = '<my_news_server>' # Your news server
newsgroup = 'comp.lang.c' # Group of your choice

def inpdflt(s, d):
resp = raw_input("%s [%s]: " % (s, d))
return resp or d

news = nntplib.NNTP(SRVR)
resp, estimate, first, last, name = news.group(newsgroup)

if estimate == '0':
sys.exit("No messages in " + newsgroup)

#
# Get (article number, subject, poster, date, id, references, size,
lines)
# for each of the articles between first and last
#
xover = news.xover(first, last)

# loop through articles, extracting headers
for x in xover[1]:
# x == (article number, subject, poster, date, id, references,
size, lines)
try:
hdrs = news.head(x[0])[3]
mesg = rfc822.Message(cStringIO.StringIO("\r\n".join(hdrs)))
print '%s\n+++%s' % (mesg.getheader("from"),
mesg.getheader("subject"))
except nntplib.NNTPError:
pass
news.quit()

<-- End newsgroup -->

I am getting all the messages of the newsgroup stored in the newsgroup
server.
What I want is to *group the messages belonging to each thread* .

How would I do that ?

Eg:

Topic 1
|
-- Re: Topic:1
-- Re: Topic: 1
|
-- Re: Re: Topic 1

Topic 2
|
-- Re: Topic:2


Total number of messages 6, but number of threads = 2,
I want to get an abstraction something similar to this.




More information about the Python-list mailing list