
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 ProPublicaRecipe(BasicNewsRecipe):
    __license__ = 'GPL v3'
    __author__ = 'kwetal'
    language = 'en'
    version = 1

    title = u'Pro Publica'
    publisher = u'ProPublica.org'
    category = u'Political blog'
    description = u'Independent investigative journalism in the public interest.'

    oldest_article = 14
    max_articles_per_feed = 100
    use_embedded_content = False

    remove_empty_feeds = True
    no_stylesheets = True
    remove_javascript = True

    keep_only_tags = [
        classes('title-wrapper content-wrapper article-header lead-art article-body')
    ]
    remove_tags = [
        classes('email-signup story-tools newsletter topics-list')
    ]
    remove_tags_after = classes('article-body')

    feeds = [
        (u'Top Stories', u'http://feeds.propublica.org/propublica/main'),
    ]

    def preprocess_html(self, soup):
        for img in soup.findAll('img', **classes('lazyload')):
            source = img.findParent('picture')
            if source is not None:
                source = source.find('source')
                if source is not None:
                    img['src'] = source['data-srcset']
        for img in soup.findAll('img', srcset=True):
            img['src'] = img['srcset'].split()[0]
        return soup
