#!/usr/bin/env  python2
# -*- coding: cp1252 -*-

__license__ = 'GPL v3'
__copyright__ = '2009, Darko Miletic <darko.miletic at gmail.com>'
'''
politico.eu
'''

from calibre.web.feeds.news import BasicNewsRecipe


class Politico(BasicNewsRecipe):

    title = 'Politico'
    __author__ = 'Darko Miletic and Sujata Raman'
    description = 'Political news from USA'
    publisher = 'Capitol News Company, LLC'
    category = 'news, politics, USA'
    oldest_article = 2
    max_articles_per_feed = 20
    use_embedded_content = False
    no_stylesheets = True
    remove_javascript = True
    encoding = 'UTF-8'
    language = 'en'

    html2lrf_options = [
        '--comment', description, '--category', category, '--publisher', publisher, '--ignore-tables'
    ]

    html2epub_options = 'publisher="' + publisher + '"\ncomments="' + \
        description + '"\ntags="' + category + '"\nlinearize_tables=True'

    keep_only_tags = [
        dict(name='div', attrs={'class':['container']}),
    ]

    remove_tags = [
        dict(name=['notags', 'embed', 'aside', 'object', 'link']),
        dict(name='div', attrs={'class':'module-article-inline related-articles'}),
        dict(name='header', attrs={'class':'summary-header'}),
        dict(name='div', attrs={'class':'amazon-polly-wrapper'}),
        dict(name='div', attrs={'class': lambda x: x and 'story-meta' in x.split()}),
        dict(attrs={'class': lambda x: x and 'story-tools' in x.split()}),
        dict(attrs={'class': lambda x: x and 'social-sharing' in x.split()}),
        dict(attrs={'class': lambda x: x and 'date-time' in x.split()}),
        dict(attrs={'class': lambda x: x and 'authors' in x.split()}),
        dict(attrs={'class': lambda x: x and 'story-meta__authors' in x.split()}),
        dict(attrs={'class': lambda x: x and 'story-meta__timestamp' in x.split()}),
        dict(attrs={'class': lambda x: x and 'story-continued' in x.split()}),
        dict(attrs={'class': lambda x: x and 'story-supplement' in x.split()}),
        dict(attrs={'class': lambda x: x and 'story-share' in x.split()}),
        dict(attrs={'class': lambda x: x and 'suggested' in x.split()}),
        dict(attrs={'class': lambda x: x and 'article__sidebar' in x.split()}),
        dict(name='svg'),
        dict(name='footer'),
        dict(name='span', attrs={'class':'ad-label'}),
        dict(name='div', attrs={'class':'pro-pill'}),
        dict(name='ol', attrs={'class':'breadcrumbs__list'}),
    ]

    preprocess_regexps = [(re.compile(r'<a\s[^>]*>([^<]*)</a>', re.DOTALL|re.IGNORECASE), lambda match: match.group(1))]

    remove_tags_after  = dict(attrs={'class': lambda x: x and 'article__more-from' in x.split()})

    extra_css = '''
                body{font-family:Arial,Sans-serif;}
                element.style{color:#FF0000;font-family:Arial,Sans-serif;}
                .author{color:#808080;font-size:x-small;}
                a{ color:#003399;}
                .byline{color:#696969 ; font-size:x-small;}
                .story{color:#000000;}
                td{color:#000000;}
                .calibre_navbar {display : none;}
                '''

    feeds = [
        (u'Top Stories', u'http://www.politico.eu/feed'),
    ]

    def postprocess_html(self, soup, first):
        for tag in soup.findAll(name=['table', 'tr', 'td']):
            tag.name = 'div'
        return soup
