__license__ = 'GPL v3'
__copyright__ = '2013, Alexander Schremmer <alex@alexanderweb.de>, Robert Riemann <robert@riemann.cc>'

import re

from calibre.web.feeds.news import BasicNewsRecipe


class TazRSSRecipe(BasicNewsRecipe):
    title = 'Taz - die Tageszeitung'
    description = 'Taz.de - die tageszeitung (Anpassung von Robert)'
    __author__ = 'Alexander Schremmer, Robert Riemann'
    language = 'de'
    lang = 'de-DE'
    category = 'news, Germany'
    timefmt = ' [%a, %d %b %Y]'
    publication_type = 'newspaper'
    remove_empty_feeds = True
    use_embedded_content = False
    oldest_article = 7
    max_articles_per_feed = 100
    publisher = 'taz Entwicklungs GmbH & Co. Medien KG'
    # masthead_url = u'http://galeria-autonomica.de/wp-content/uploads/a_taz-logo.gif'
    masthead_url = 'http://upload.wikimedia.org/wikipedia/de/thumb/1/15/Die-Tageszeitung-Logo.svg/500px-Die-Tageszeitung-Logo.svg.png'

    conversion_options = {
        'publisher': publisher,
        'language': lang,
    }
    feeds = [
        ('Schlagzeilen', 'http://www.taz.de/!p3270;rss/'),
        ('Politik', 'http://www.taz.de/Politik/!p2;rss/'),
        ('Zukunft', 'http://www.taz.de/Zukunft/!p4;rss/'),
        ('Netz', 'http://www.taz.de/Netz/!p5;rss/'),
        ('Debatte', 'http://www.taz.de/Debatte/!p9;rss/'),
        ('Leben', 'http://www.taz.de/Leben/!p10;rss/'),
        ('Sport', 'http://www.taz.de/Sport/!p12;rss/'),
        ('Wahrheit', 'http://www.taz.de/Wahrheit/!p13;rss/'),
        ('Berlin', 'http://www.taz.de/Berlin/!p14;rss/'),
        ('Nord', 'http://www.taz.de/Nord/!p11;rss/'),
    ]
    # omit articles already linked in Schlagzeilen feed
    ignore_duplicate_articles = {'title', 'url'}

    # use the cover presented on the homepage
    cover_url = 'http://www.taz.de/digitaz/.s1jpeg320'

    no_stylesheets = True  # default value is False, but True makes process much faster
    keep_only_tags = [dict(name=['div'], attrs={'class': re.compile(r'.*\bsect_article\b.*')})]
    remove_tags = [
        dict(name=['div'], attrs={'class': 'sectfoot'}),
        # remove: taz paywall
        dict(name=['div'], attrs={'id': 'tzi_paywall'}),
    ]


# with article pictures on Kindle super-slow
#    def populate_article_metadata(self, article, soup, first):
#        if first and hasattr(self, 'add_toc_thumbnail'):
#            picdiv = soup.find('img')
#            if picdiv is not None:
#                self.add_toc_thumbnail(article,picdiv['src'])
