#!/usr/bin/python
# -*- coding: UTF-8 -*-


"""
    Code pour creer un client simple NNTP.
"""


###############################################################################
# Informations de versions
###############################################################################
__project__   = 'Formation Python'
__author__    = 'Simon CHOLLET (s.chollet@llr.in2p3.fr)'
__modifiers__ = ''
__date__      = '14/09/2011'
__version__   = '1.0'


###############################################################################
# Importations
###############################################################################
# Importation du module nntplib
import nntplib


###############################################################################
# Definitions specifiques
###############################################################################
# Adresse du canal a ouvrir
NNTP_GROUP_ADDRESS = "news.gmane.org"


###############################################################################
# Declaration des classes / fonctions
###############################################################################


#
#
# Fonction appelee lors de la demande
# d'execution de la classe/module
if __name__ == "__main__":

    ###########################################################################
    # Ouverture du canal sur les news
    s = nntplib.NNTP(NNTP_GROUP_ADDRESS)

    # Recuperation des informations sur les news
    resp, count, first, last, name = s.group('gmane.comp.python.committers')
    # Affiche les informations
    # Affiche :
    #   Groupe gmane.comp.python.committers, a 1790 articles : [1..1790]
    print 'Groupe %s, a %s articles : [%s..%s]' % (name, count, first, last)

    # Selectionne les 10 derniers
    # Affiche :
    #   1781 Re: Three wishes for Mercurial-Roundup integration
    #   1782 Re: Three wishes for Mercurial-Roundup integration
    #   1783 Re: Three wishes for Mercurial-Roundup integration
    #   1784 Re: Three wishes for Mercurial-Roundup integration
    #   1785 Re: Three wishes for Mercurial-Roundup integration
    #   1786 Working on 3.2.2
    #   1787 [RELEASED] Python 3.2.2
    #   1788 Re: [RELEASED] Python 3.2.2
    #   1789 Re: [PSF-Board] Contributor agreement
    #   1790 Re: [PSF-Board] Contributor agreement
    resp, subs = s.xhdr('subject', first + '-' + last)
    for id, sub in subs[-10:]:
        print id, sub

    # Ferme la connexion
    s.quit()
