#!/usr/bin/env python2
# vim:fileencoding=utf-8
from __future__ import unicode_literals, division, absolute_import, print_function
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 NewYorkPost(BasicNewsRecipe):
    title = 'New York Post'
    __author__ = 'Darko Miletic'
    description = 'Daily newspaper'
    publisher = 'NYP Holdings, Inc.'
    category = 'news, politics, USA'
    oldest_article = 1.1
    max_articles_per_feed = 10
    no_stylesheets = True
    encoding = 'utf8'
    use_embedded_content = False
    language = 'en'

#	see note regarding solution to figcaption not a class

    extra_css = """
        body{font-family: Arial,sans-serif }
        .img{margin-bottom: 0.4em} 
        .post_caption{font-size: small}
        .byline_author,.date{font-size: small}
    """

    compress_news_images = True
    compress_news_images_auto_size = 16

    conversion_options = {
        'comment': description, 'tags': category, 'publisher': publisher, 'language': language
    }
    ignore_duplicate_articles = {'title', 'url'}

    keep_only_tags = [
        dict(itemprop=['headline', 'articleBody']),
        dict(name='h1'),
        classes('byline byline-date source article-info entry-content entry-content-read-more featured-image'
        'headline headline--single' 'date meta meta--byline' 'Date published'),
    ]

#	remove 'aside' added to remove "More Stores" box that appears in most every article

    remove_tags	=	[
        dict(itemprop=['sharebar-trigger-desktop', ]),
        dict(name='aside'),
        classes(
            'floating-share floating-share-wrap sharedaddy sd-sharing-enabled tag-list module-wrapper'
            ' inline-module--author inline-module--columnist inline-module--primary-tag'
            ' author-flyout modal__email-author button gowatchit-inline'
            '"pushly_popover-box pushly-prompt-slide"'
                ),
        dict(name=['link', 'meta']),
    ]

    feeds = [
        ('All Stories','https://nypost.com/feed'),
        ('News','https://nypost.com/news/feed'),
        ('Metro', 'http://nypost.com/metro/feed/'),
        ('Business', 'http://nypost.com/business/feed/'),
        ('Opinion', 'http://nypost.com/opinion/feed/'),
        ('Technology', 'http://nypost.com/tech/feed/'),
        ('Media', 'http://nypost.com/media/feed/'),
        ('Entertainment', 'http://nypost.com/entertainment/feed/'),
        ('Living', 'http://nypost.com/living/feed/'),
        ('Page 6', 'http://pagesix.com/feed/'),
    ]

    def print_version(self, url):
        return url.replace('nypost.com/p/', 'nypost.com/f/print/')

    def preprocess_html(self, soup):
        for img in soup.findAll('img', attrs={'data-srcset': True}):
            img['src'] = img['data-srcset'].split()[0]
        for pic in soup.findAll('picture'):
            source = pic.find('source', srcset=True)
            if source is not None:
                img = pic.find('img')
                if img is not None:
                    img['src'] = source['srcset'].split()[0]
        for source in pic.findAll('source'):
            source.extract()
        for fig_c in soup.findAll('figcaption'):
            fig_c['class'] = 'post_caption'
        return soup
    
    # Convert all links to text:
    def preprocess_html(self, soup):
        for a in soup.findAll('a', href=True):
            a.tag = 'span'
            del a['href']
        return soup
    