#!/usr/bin/env  python

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


class OpenMagazine(BasicNewsRecipe):
    title = 'Open Magazine'
    description = 'The weekly current affairs and features magazine.'
    language = 'en_IN'
    __author__ = 'unkn0wn'
    oldest_article = 7  # days
    max_articles_per_feed = 50
    encoding = 'utf-8'
    use_embedded_content = False
    no_stylesheets = True
    remove_attributes = ['style', 'height', 'width']
    masthead_url = 'https://openthemagazine.com/wp-content/themes/open/images/logo.png'
    ignore_duplicate_articles = {'url'}
    extra_css = '[id^="caption-attachment"] {font-size: small;font-style: italic;}'
    'blockquote{color:#404040;}'
    '.about-author{font-size:small;}'

    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)

    def get_cover_url(self):
        d = self.recipe_specific_options.get('days')
        if not (d and isinstance(d, str)):
            soup = self.index_to_soup('https://openthemagazine.com/')
            tag = soup.find(attrs={'class': 'magazine-item mr-1'})
            if tag:
                self.cover_url = tag.find('img')['src']
            return getattr(self, 'cover_url', None)

    keep_only_tags = [
        classes('post-data post-thumb post-meta post-excerp'),
    ]

    remove_tags = [
        classes('top-social menu-social post-author blurb-social button-links'),
    ]

    remove_tags_after = [
        classes('post-meta post-excerp'),
    ]

    feeds = [
        ('Cover Story', 'https://openthemagazine.com/cover-story/feed/'),
        ('Columns', 'https://openthemagazine.com/columns/feed'),
        ('Essays', 'https://openthemagazine.com/essays/feed'),
        ('Features', 'https://openthemagazine.com/features/feed'),
        ('Books', 'https://openthemagazine.com/lounge/books/feed'),
        ('Art & Culture', 'https://openthemagazine.com/art-culture/feed'),
        ('Cinema', 'https://openthemagazine.com/cinema/feed'),
    ]
