#!/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 testnewssource(BasicNewsRecipe):
    title = 'Test News'
    __author__ = 'Darko Miletic'
    # try
    creator = 'DATE'
    contributor = "DAte"
    #
    description = 'Breaking News from FOX'
    publisher = 'FOXNews.com'
    category = 'news, breaking news, latest news, current news, world news, national news, USA'
    oldest_article = 1
    max_articles_per_feed = 20
    no_stylesheets = True
    encoding = 'utf8'
    use_embedded_content = False

    language = 'en'
    remove_empty_feeds = True
    extra_css = """
        body{font-family: Arial,sans-serif }
        .caption{font-size: x-small}
        .author,.dateline{font-size: small}
    """

    # title below is title in calibre and Kindle
    # title concatenation line puts date on kindle list of documents
    # coverdate below appears on cover page of mobi document in calibre and kindle
    conversion_options = {
        #        'title' : title,
        'title': title + time.strftime(': %a, %b %d'),
        #       'coverdate' : time.strftime(': %a, %b %d'),
        'comment': description,
        'tags': category,
        'publisher': publisher,
        'authors': title,
        'language': language
    }

    # added itemposttime class from web rss header page
    remove_attributes = ['xmlns', 'lang']
    ignore_duplicate_articles = {'title', 'url'}
    keep_only_tags = [
        dict(itemprop=['headline', 'articleBody']),
        dict(name='h1'),
        classes(
            'author-byline date article-date itemposttime source article-info featured-image article-body'
        ),
    ]

    feeds = [(u'Latest Headlines', u'http://feeds.foxnews.com/foxnews/latest'),
             (u'Travel',
              u'http://feeds.foxnews.com/foxnews/internal/travel/mixed')]

    # Small piece of code to 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
