#!/usr/bin/env python

from calibre.web.feeds.news import BasicNewsRecipe, classes


class projectsynd(BasicNewsRecipe):
    title = 'Project Syndicate'
    __author__ = 'unkn0wn'
    description = (
        'Project Syndicate produces and delivers original, high-quality commentaries to a global audience. '
        'Featuring exclusive contributions by prominent political leaders, policymakers, scholars, business '
        'leaders, and civic activists from around the world, we provide news media and their readers with cutting-edge '
        'analysis and insight, regardless of ability to pay. Our membership includes over 500 media outlets – more '
        'than half of which receive our commentaries for free or at subsidized rates – in 156 countries. '
        'Download Weekly.'
    )
    language = 'en'
    no_stylesheets = True
    remove_javascript = True
    masthead_url = 'https://www.project-syndicate.org/images/PS_Logotype.svg'
    ignore_duplicate_articles = {'title', 'url'}
    resolve_internal_links = True
    remove_empty_feeds = True
    remove_attributes = ['style', 'height', 'width']
    oldest_article = 7  # days
    delay = 2

    def get_browser(self, *args, **kwargs):
        kwargs['user_agent'] = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
        br = BasicNewsRecipe.get_browser(self, *args, **kwargs)
        br.addheaders += [
            ('Referer', 'https://www.google.com/'),
            ('X-Forwarded-For', '66.249.66.1'),
        ]
        return br

    recipe_specific_options = {
        'days': {
            'short': 'Oldest article to download from this news source. In days ',
            'long': 'For example, 0.5, gives you articles from the past 12 hours',
            'default': str(oldest_article),
        }
    }

    def __init__(self, *args, **kwargs):
        BasicNewsRecipe.__init__(self, *args, **kwargs)
        d = self.recipe_specific_options.get('days')
        if d and isinstance(d, str):
            self.oldest_article = float(d)

    extra_css = '''
        [aria-label="Author information"],  .article__byline, .image--lead { font-size: small; }
        .article__abs { font-style: italic; }
        em { font-style:italic; color:#202020; }
    '''

    keep_only_tags = [
        classes('image--lead article_title article__byline article__title--main article__abs article__body'),
        dict(attrs={'aria-label': 'Author information'}),
    ]

    remove_tags = [
        dict(name='source'),
        classes('inlay--slide slide__container editorpick-container  special--generic mrf--promotion'),
    ]

    feeds = [
        'https://www.project-syndicate.org/rss/section/economics',
        'https://www.project-syndicate.org/rss/section/politics-world-affairs',
        'https://www.project-syndicate.org/rss/onpoint',
        'https://www.project-syndicate.org/rss/section/environment-sustainability',
        'https://www.project-syndicate.org/rss/section/global-health-development',
        'https://www.project-syndicate.org/rss/section/culture-society',
        'https://www.project-syndicate.org/rss/section/innovation-technology',
        'https://www.project-syndicate.org/rss',
    ]

    def preprocess_html(self, soup):
        if div := soup.find(**classes('article__abs')):
            if p := div.find('p'):
                div.name = 'p'
                div.clear()
                div.string = p.get_text()
        for h2 in soup.findAll(['h2', 'h3']):
            h2.name = 'h4'
        for img in soup.findAll('img', attrs={'src': True}):
            img['src'] = img['src'].replace('medium', 'xlarge')
        return soup
