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 = 'Pro Publica'
    publisher = 'ProPublica.org'
    category = 'Political blog'
    description = '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'),
        classes('wp-block-propublica-notes--top'),
    ]
    remove_tags_after = classes('article-body')

    feeds = [
        ('Top Stories', '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
