#!/usr/bin/env python
# vim:fileencoding=utf-8
__license__ = 'GPL v3'
__copyright__ = '2014, Darko Miletic <darko.miletic at gmail.com>'
'''
www.wired.com
'''

from calibre import browser
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 WiredDailyNews(BasicNewsRecipe):
    title = 'Wired Daily Edition'
    __author__ = 'Darko Miletic, PatStapleton(update 2020-05-24)'
    description = (
        'Wired is a full-color monthly American magazine, published in both print'
        ' and online editions, that reports on how emerging technologies affect culture,'
        ' the economy and politics.'
    )
    publisher = 'Conde Nast'
    category = 'news, IT, computers, technology'
    oldest_article = 2
    max_articles_per_feed = 200
    no_stylesheets = True
    encoding = 'utf-8'
    use_embedded_content = False
    language = 'en'
    ignore_duplicate_articles = {'url'}
    remove_empty_feeds = True
    publication_type = 'newsportal'
    extra_css = '''
        .entry-header{
                        text-transform: uppercase;
                        vertical-align: baseline;
                        display: inline;
                        }
        ul li{display: inline}
    '''

    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)

    keep_only_tags = [
        classes('content-header lead-asset article__body'),
    ]
    remove_tags = [
        classes(
            'related-cne-video-component tags-component callout--related-list iframe-embed podcast_storyboard'
            ' inset-left-component ad consumer-marketing-component social-icons'
        ),
        dict(name=['meta', 'link', 'aside']),
        dict(id=['sharing', 'social', 'article-tags', 'sidebar']),
    ]
    remove_attributes = ['srcset']
    handle_gzip = True

    feeds = [
        ('Top Stories', 'https://www.wired.com/feed/rss'),
        ('Business', 'https://www.wired.com/feed/category/business/latest/rss'),
        ('Culture', 'https://www.wired.com/feed/category/culture/latest/rss'),
        ('Gear', 'https://www.wired.com/feed/category/gear/latest/rss'),
        ('Ideas', 'https://www.wired.com/feed/category/ideas/latest/rss'),
        ('Science', 'https://www.wired.com/feed/category/science/latest/rss'),
        ('Security', 'https://www.wired.com/feed/category/security/latest/rss'),
        ('Transportation', 'https://www.wired.com/feed/category/transportation/latest/rss'),
        ('Backchannel', 'https://www.wired.com/feed/category/backchannel/latest/rss'),
        ('WIRED Guides', 'https://www.wired.com/feed/tag/wired-guide/latest/rss'),
        #    (u'Photo', u'https://www.wired.com/feed/category/photo/latest/rss'),
    ]

    def get_article_url(self, article):
        return article.get('link', None)

    # Wired changes the content it delivers based on cookies, so the
    # following ensures that we send no cookies
    def get_browser(self, *args, **kwargs):
        return self

    def clone_browser(self, *args, **kwargs):
        return self.get_browser()

    def open_novisit(self, *args, **kwargs):
        br = browser()
        return br.open_novisit(*args, **kwargs)

    open = open_novisit
