#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
# License: GPLv3 Copyright: 2009, Kovid Goyal <kovid at kovidgoyal.net>

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 LeTemps(BasicNewsRecipe):
    title = u'Le Temps'
    oldest_article = 7
    max_articles_per_feed = 100
    __author__ = 'Kovid Goyal'
    description = 'French news. Needs a subscription from https://www.letemps.ch'
    no_stylesheets = True
    remove_javascript = True
    encoding = 'UTF-8'
    language = 'fr'
    needs_subscription = True
    use_embedded_content = False
    remove_empty_feeds = True

    def get_browser(self):
        br = BasicNewsRecipe.get_browser(self)
        br.open('https://www.letemps.ch/user/login')
        br.select_form(action='/user/login')
        br['name'] = self.username
        br['pass'] = self.password
        raw = br.submit().read()
        if b'href="/subscribe"' in raw:
            raise ValueError(
                'Failed to login to letemps.ch. Check '
                'your username and password'
            )
        return br

    keep_only_tags = [
        classes('picture-cover article-content'),
    ]

    remove_tags = [
        dict(name=['button', 'source']),
        classes('share related-news'),
    ]

    feeds = [
        (u'Actualité', u'https://letemps.ch/rss/site/'),
        (u'Actualité - Monde', u'https://letemps.ch/rss/site/actualite/monde'),
        (
            u'Actualité - Suisse & régions',
            u'https://letemps.ch/rss/site/actualite/suisse_regions'
        ), (u'Actualité - Sport', u'https://letemps.ch/rss/site/actualite/sports'),
        (
            u'Actualité - Sciences & Environnement',
            u'https://letemps.ch/rss/site/actualite/sciences_environnement'
        ),
        (
            u'Actualité - Multimédia',
            u'https://letemps.ch/rss/site/actualite/multimedia'
        ),
        (u'Actualité - Société', u'https://letemps.ch/rss/site/actualite/societe'),
        (
            u'Actualité - Société | Quoi de neuf',
            u'https://letemps.ch/rss/site/actualite/societe/quoi_de_neuf'
        ), (u'Economie & Finance', u'https://letemps.ch/rss/site/economie_finance'),
        (
            u'Economie & Finance - Finance',
            u'https://letemps.ch/rss/site/economie_finance/finance'
        ),
        (
            u'Economie & Finance - Fonds de placement',
            u'https://letemps.ch/rss/site/economie_finance/fonds_placement'
        ),
        (
            u'Economie & Finance - Carrières',
            u'https://letemps.ch/rss/site/economie_finance/carrieres'
        ), (u'Culture', u'http://letemps.ch/rss/site/culture'),
        (u'Culture - Cinémas', u'https://letemps.ch/rss/site/culture/cinema'),
        (u'Culture - Musiques', u'https://letemps.ch/rss/site/culture/musiques'),
        (u'Culture - Scènes', u'https://letemps.ch/rss/site/culture/scenes'),
        (
            u'Culture - Arts plastiques',
            u'https://letemps.ch/rss/site/culture/arts_plastiques'
        ), (u'Culture - Livres', u'https://letemps.ch/rss/site/culture/livres'),
        (u'Lifestyle - Luxe', u'https://letemps.ch/rss/site/lifestyle/luxe'),
        (u'Lifestyle - Mode', u'https://letemps.ch/rss/site/lifestyle/mode'),
        (
            u'Lifestyle - Horlogerie & Joaillerie',
            u'https://letemps.ch/rss/site/lifestyle/horlogerie_joaillerie'
        ), (u'Lifestyle - Design', u'https://letemps.ch/rss/site/lifestyle/design'),
        (u'Lifestyle - Voyages', u'https://letemps.ch/rss/site/lifestyle/voyages'),
        (
            u'Lifestyle - Gastronomie',
            u'https://letemps.ch/rss/site/lifestyle/gastronomie'
        ),
        (
            u'Lifestyle - Architecture & Immobilier',
            u'https://letemps.ch/rss/site/lifestyle/architecture_immobilier'
        ),
        (
            u'Lifestyle - Automobile',
            u'https://letemps.ch/rss/site/lifestyle/automobile'
        ), (u'Opinions', u'http://letemps.ch/rss/site/opinions'),
        (
            u'Opinions - Editoriaux',
            u'https://letemps.ch/rss/site/opinions/editoriaux'
        ), (u'Opinions - Invités', u'https://letemps.ch/rss/site/opinions/invites'),
        (
            u'Opinions - Chroniques',
            u'https://letemps.ch/rss/site/opinions/chroniques'
        ),
        (u'Opinions - Chappatte', u'https://letemps.ch/rss/site/opinions/chappatte')
    ]
