nntplib: abstraction of threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rakesh

    nntplib: abstraction of threads

    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_serve r>' # 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(SR VR)
    resp, estimate, first, last, name = news.group(news group)

    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(firs t, 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.Strin gIO("\r\n".join (hdrs)))
    print '%s\n+++%s' % (mesg.getheader ("from"),
    mesg.getheader( "subject"))
    except nntplib.NNTPErr or:
    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.

  • Werner Amann

    #2
    Re: nntplib: abstraction of threads

    Rakesh schrieb:
    [color=blue]
    > What I want is to *group the messages belonging to each thread* .[/color]

    Hello

    Why not sort with Message-ID and References?
    Attention - it is a Newbie-Solution.

    import nntplib

    hamster = nntplib.NNTP('1 27.0.0.1', 119, 'user', 'pass')
    resp, count, first, last, name = hamster.group(' comp.lang.pytho n')
    resp, items = hamster.xover(f irst,last)

    start_dic = {}
    re_dic = {}
    numb = 1

    for id,subject,auth or,date,message _id,references, size,lines in items:
    if 'Re:' not in subject:
    start_dic[subject] = (author, message_id)
    else:
    re_dic[numb] = (subject, author, references)
    numb += 1

    resp = hamster.quit()

    for a in start_dic:
    print a
    print start_dic[a][0]
    for b in re_dic:
    if start_dic[a][1] in re_dic[b][2]:
    print '|'
    print ' ->', re_dic[b][0]
    print ' ', re_dic[b][1]
    print

    --
    Werner Amann

    Comment

    • Steve Holden

      #3
      Re: nntplib: abstraction of threads

      Werner Amann wrote:
      [color=blue]
      > Rakesh schrieb:
      >
      >[color=green]
      >>What I want is to *group the messages belonging to each thread* .[/color]
      >
      >
      > Hello
      >
      > Why not sort with Message-ID and References?
      > Attention - it is a Newbie-Solution.
      >
      > import nntplib
      >
      > hamster = nntplib.NNTP('1 27.0.0.1', 119, 'user', 'pass')
      > resp, count, first, last, name = hamster.group(' comp.lang.pytho n')
      > resp, items = hamster.xover(f irst,last)
      >
      > start_dic = {}
      > re_dic = {}
      > numb = 1
      >
      > for id,subject,auth or,date,message _id,references, size,lines in items:
      > if 'Re:' not in subject:
      > start_dic[subject] = (author, message_id)
      > else:
      > re_dic[numb] = (subject, author, references)
      > numb += 1
      >
      > resp = hamster.quit()
      >
      > for a in start_dic:
      > print a
      > print start_dic[a][0]
      > for b in re_dic:
      > if start_dic[a][1] in re_dic[b][2]:
      > print '|'
      > print ' ->', re_dic[b][0]
      > print ' ', re_dic[b][1]
      > print
      >[/color]
      Better still, do a Google search on "mail threading algorithm",
      implement the algorithm described in



      and post your implementation back to the newsgroup :-)

      regards
      Steve
      --
      Steve Holden http://www.holdenweb.com/
      Python Web Programming http://pydish.holdenweb.com/
      Holden Web LLC +1 703 861 4237 +1 800 494 3119

      Comment

      • Rakesh

        #4
        Re: nntplib: abstraction of threads


        Steve Holden wrote:[color=blue]
        > Werner Amann wrote:
        >[color=green]
        > > Rakesh schrieb:
        > >
        > >[color=darkred]
        > >>What I want is to *group the messages belonging to each thread* .[/color]
        > >
        > >
        > > Hello
        > >
        > > Why not sort with Message-ID and References?
        > > Attention - it is a Newbie-Solution.
        > >
        > > import nntplib
        > >
        > > hamster = nntplib.NNTP('1 27.0.0.1', 119, 'user', 'pass')
        > > resp, count, first, last, name = hamster.group(' comp.lang.pytho n')
        > > resp, items = hamster.xover(f irst,last)
        > >
        > > start_dic = {}
        > > re_dic = {}
        > > numb = 1
        > >
        > > for id,subject,auth or,date,message _id,references, size,lines in[/color][/color]
        items:[color=blue][color=green]
        > > if 'Re:' not in subject:
        > > start_dic[subject] = (author, message_id)
        > > else:
        > > re_dic[numb] = (subject, author, references)
        > > numb += 1
        > >
        > > resp = hamster.quit()
        > >
        > > for a in start_dic:
        > > print a
        > > print start_dic[a][0]
        > > for b in re_dic:
        > > if start_dic[a][1] in re_dic[b][2]:
        > > print '|'
        > > print ' ->', re_dic[b][0]
        > > print ' ', re_dic[b][1]
        > > print
        > >[/color]
        > Better still, do a Google search on "mail threading algorithm",
        > implement the algorithm described in
        >
        > http://www.jwz.org/doc/threading.html[/color]

        Thanks a lot for the link.
        [color=blue]
        >
        > and post your implementation back to the newsgroup :-)[/color]

        Sure I would. I would definitely do the same.
        I am a python newbie and am reading nntp spec (rfc) right now.
        Once I get a working version I would definitely post the same.

        Comment

        • JanC

          #5
          Re: nntplib: abstraction of threads

          Steve Holden schreef:
          [color=blue]
          > Better still, do a Google search on "mail threading algorithm",
          > implement the algorithm described in
          >
          > http://www.jwz.org/doc/threading.html
          >
          > and post your implementation back to the newsgroup :-)[/color]

          <http://www.google.com/search?q=jwz+py thon&btnI=I'm+F eeling+Lucky>
          <http://www.amk.ca/python/code/jwz>

          --
          JanC

          "Be strict when sending and tolerant when receiving."
          RFC 1958 - Architectural Principles of the Internet - section 3.9

          Comment

          Working...