#!/usr/bin/env python
# vim:fileencoding=utf-8
from calibre.web.feeds.news import BasicNewsRecipe, classes


class NewYorkPost(BasicNewsRecipe):
    title = 'New York Post'
    __author__ = 'unkn0wn'
    description = 'Daily newspaper'
    publisher = 'NYP Holdings, Inc.'
    category = 'news, politics, USA'
    oldest_article = 1
    no_stylesheets = True
    encoding = 'utf-8'
    use_embedded_content = False
    language = 'en_US'
    remove_attributes = ['style', 'height', 'width']
    masthead_url = 'https://static.storied.co/gQlwL7PA4X5XM6b8/projects/JYX3m27B8zP9BW4D/assets/images/81e851fd6c5d8fc747c41064d8aee316.png'
    extra_css = '.byline, .date, .article-header__top, .calibre-nuked-tag-figcaption { font-size: small; }'

    def get_cover_url(self):
        soup = self.index_to_soup('https://www.frontpages.com/us-newspapers/')
        for img in soup.findAll('img', src=True):
            if 'new-york-post' in img['src']:
                return 'https://www.frontpages.com' + img['src']

    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)

    ignore_duplicate_articles = {'title', 'url'}

    keep_only_tags = [
        classes('article-header single__content'),
    ]

    remove_tags = [
        dict(attrs={'id': ['connatix-outstream-video']}),
        classes('article-header__social-icons wpcom-liveblog-container nyp-s2n-wrapper nyp-slideshow-modal-image__icon  inline-module inline-module__inner'),
        dict(name=['svg', 'link', 'meta', 'aside']),
    ]

    # https://nypost.com/rssfeeds/
    feeds = [
        ('US-News', 'https://nypost.com/us-news/feed/'),
        ('Metro', 'https://nypost.com/metro/feed/'),
        ('Politics', 'https://nypost.com/politics/feed/'),
        ('World News', 'https://nypost.com/world-news/feed/'),
        ('Sports', 'https://nypost.com/sports/feed/'),
        ('Business', 'https://nypost.com/business/feed/'),
        ('Opinion', 'https://nypost.com/opinion/feed/'),
        ('Entertainment', 'https://nypost.com/entertainment/feed/'),
        ('Fashion & Beauty', 'https://nypost.com/fashion-and-beauty/feed/'),
        ('Lifestyle', 'https://nypost.com/lifestyle/feed/'),
        ('Tech', 'https://nypost.com/tech/feed/'),
        ('Media', 'https://nypost.com/media/feed/'),
        ('Real Estate', 'https://nypost.com/real-estate/feed/'),
        ('Page Six', 'https://pagesix.com/feed/'),
        ('Others', 'https://nypost.com/feed/'),
    ]

    def preprocess_html(self, soup):
        for img in soup.findAll('img', attrs={'src': True}):
            img['src'] = img['src'].split('?')[0] + '?w=600'
        return soup
