__license__ = 'GPL v3'
__author__ = 'Lorenzo Vigentini and Olivier Daigle'
__copyright__ = '2012, Lorenzo Vigentini <l.vigentini at gmail.com>, Olivier Daigle <odaigle _at nuvucameras __dot__ com>'
__version__ = 'v1.01'
__date__ = '17, March 2013'
__description__ = 'Canadian Paper '

'''
http://www.ledevoir.com/
'''

from calibre.web.feeds.news import BasicNewsRecipe


def classes(classes):
    q = frozenset(classes.split(' '))
    return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})


class ledevoir(BasicNewsRecipe):
    author = 'Lorenzo Vigentini'
    description = 'Canadian Paper. A subscription is optional, with it you get more content'

    cover_url = 'http://www.ledevoir.com/images/ul/graphiques/logo_devoir.gif'
    title = 'Le Devoir '
    publisher = 'leDevoir.com'
    category = 'News, finance, economy, politics'

    language = 'fr'
    encoding = 'utf-8'
    timefmt = '[%a, %d %b, %Y]'

    oldest_article = 1
    max_articles_per_feed = 50
    min_articles_per_feed = 0
    use_embedded_content = False
    needs_subscription = 'optional'

    compress_news_images = True
    compress_news_images_auto_size = 4

    remove_javascript = True
    no_stylesheets = True
    remove_empty_feeds = True

    keep_only_tags = [
        dict(name='h1'),
        dict(attrs={'class': 'col-xs-14 col-sm-7 col-md-9 col-lg-10'}),
    ]
    remove_tags = [classes('category-nav social')]

    feeds = [
        ('A la une', 'http://www.ledevoir.com/rss/manchettes.xml'),
        ('Édition complete', 'http://feeds2.feedburner.com/fluxdudevoir'),
        ('Opinions', 'http://www.ledevoir.com/rss/opinions.xml'),
        ('Chroniques', 'http://www.ledevoir.com/rss/chroniques.xml'),
        ('Politique', 'http://www.ledevoir.com/rss/section/politique.xml?id=51'),
        ('International', 'http://www.ledevoir.com/rss/section/international.xml?id=76'),
        ('Culture', 'http://www.ledevoir.com/rss/section/culture.xml?id=48'),
        ('Environnement', 'http://www.ledevoir.com/rss/section/environnement.xml?id=78'),
        ('Societe', 'http://www.ledevoir.com/rss/section/societe.xml?id=52'),
        ('Economie', 'http://www.ledevoir.com/rss/section/economie.xml?id=49'),
        ('Sports', 'http://www.ledevoir.com/rss/section/sports.xml?id=85'),
        ('Art de vivre', 'http://www.ledevoir.com/rss/section/art-de-vivre.xml?id=50'),
    ]

    def get_browser(self):
        br = BasicNewsRecipe.get_browser(self)
        if self.username is not None and self.password is not None:
            br.open('http://www.ledevoir.com/auth/login')
            br.select_form(action='/auth/login')
            br['login[courriel]'] = self.username
            br['login[password]'] = self.password
            br.submit()
        return br
