#!/usr/bin/env  python2

__license__ = 'GPL v3'
__copyright__ = '2008, Darko Miletic <darko.miletic at gmail.com>'
'''
liberation.fr
'''

from calibre.web.feeds.news import BasicNewsRecipe


class Liberation(BasicNewsRecipe):

    title = u'Liberation'
    __author__ = 'calibre'
    description = 'Actualités'
    category = 'Actualités, France, Monde'
    language = 'fr'

    use_embedded_content = False
    timefmt = ' [%d %b %Y]'
    max_articles_per_feed = 15
    no_stylesheets = True
    remove_empty_feeds = True
    needs_subscription = 'optional'

    keep_only_tags = [dict(name='article')]
    remove_tags = [dict(attrs={'class': ['tool-bar']})]

    feeds = [

    (u'La une', u'http://rss.liberation.fr/rss/9/'),
    (u'Monde', u'http://www.liberation.fr/rss/10/'),
    (u'Économie', u'http://www.liberation.fr/rss/13/'),
    (u'Politiques', u'http://www.liberation.fr/rss/11/'),
    (u'Société', u'http://www.liberation.fr/rss/12/'),
    (u'Cinéma', u'http://www.liberation.fr/rss/58/'),
    (u'Écran', u'http://www.liberation.fr/rss/53/'),
    (u'Sports', u'http://www.liberation.fr/rss/12/')
    ]

    def get_browser(self):
        br = BasicNewsRecipe.get_browser(self)
        if self.username is not None and self.password is not None:
            br.open('http://token.liberation.fr/accounts/login/')
            br.select_form(nr=0)
            br['email'] = self.username
            br['password'] = self.password
            br.submit()
        return br

    def get_masthead_url(self):
        masthead = 'http://s0.libe.com/libe/img/common/logo-liberation-150.png'
        br = BasicNewsRecipe.get_browser(self)
        try:
            br.open(masthead)
        except:
            self.log("\nCover unavailable")
            masthead = None
        return masthead

    def get_article_url(self, article):
        url = BasicNewsRecipe.get_article_url(self, article)
        url = url.split('/')[-2]
        encoding = {'0B': '.', '0C': '/', '0A': '0', '0F': '=', '0G': '&',
                    '0D': '?', '0E': '-', '0N': '.com', '0L': 'http://', '0S':
                    'www.', '0I': '_'}
        for k, v in encoding.iteritems():
            url = url.replace(k, v)
        return url.partition('?')[0]
