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


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


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


###############################################################################
# Importations
###############################################################################
# Importation du module urllib2
import urllib2

# Importation de ElementTree
import xml.etree.ElementTree as xmltree


###############################################################################
# Definitions specifiques
###############################################################################
# Adresse Web a visiter
HTTP_WEB_ADDRESS_URL = "http://www.python.org"

# Adresse Web a visiter
HTTP_RSS_ADDRESS_URL = "http://www.lemonde.fr/rss/une.xml"

# Page HTML a sauvegarder
HTTP_OUTPUT_FILE_PATH = "../output/site.html"


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


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

    ###########################################################################
    # Ouverture de l'adresse Web
    site = urllib2.urlopen(HTTP_WEB_ADDRESS_URL)

    # Enregistre le contenu du site
    out = open(HTTP_OUTPUT_FILE_PATH, 'w')
    out.write(site.read())
    out.close

    # Affiche :
    #   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    #   
    #   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    #   
    #   <head>
    #     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    #     <title>Python Programming Language &ndash; Official Website</title>
    #     <meta name="keywords" content="python programming language object oriented web free source" />
    #     <meta name="description" content="      Home page for Python, an interpreted, interactive, object-oriented, extensible
    #         programming language. It provides an extraordinary combination of clarity and
    #         versatility, and is free and comprehensively ported." />
    #   ...
    #       </head>
    #   ...
    #   </body>
    #   </html>
    print ">>> urllib2 <<<"
    print site.read()

    ###########################################################################
    # Mini lecteur de flux RSS
    # Ouverture de l'adresse RSS
    rss = urllib2.urlopen(HTTP_RSS_ADDRESS_URL)

    # Parse les donnees
    root = xmltree.parse(rss).getroot()

    # Les titres
    titles  = []
    display = ""
    for node in root:
        for item in node.findall('item'):
            titles.append((item.find('title').text,
                           item.find('link').text,
                           item.find('description').text))
            display += "%d- %s\n" % (len(titles), item.find('title').text)

    # Affichage de la liste des titres trouves
    # Affiche :
    #   >>> urllib2 - RSS <<<
    #   1- Ligue des champions : L'OM règle ses dettes
    #   2- Côte d'Ivoire : Gbagbo sera jugé par la Cour pénale internationale
    #   3- 2012 : M. Raffarin conseille à M. Sarkozy d'entrer tôt en campagne
    #   4- Daubresse propose des contrats rémunérés de sept heures hebdomadaires aux bénéficiaires du RSA
    #   5- Mort de Richard Hamilton, un des fondateurs du Pop Art anglais
    #   ...
    print ">>> urllib2 - RSS <<<"
    print display
