#!/usr/bin/env python

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


class StrangeHorizons(BasicNewsRecipe):
    title = 'Strange Horizons'
    description = (
        'Strange Horizons is a weekly magazine of and about speculative fiction. '
        'We publish fiction, poetry, reviews, essays, interviews, roundtable '
        'discussions, and art.'
    )
    __author__ = 'unkn0wn'
    no_stylesheets = True
    use_embedded_content = False
    encoding = 'utf-8'
    language = 'en'
    remove_attributes = ['style', 'height', 'width']
    masthead_url = 'http://strangehorizons.com/wordpress/wp-content/themes/strangehorizons/images/sh-logo.jpg'
    ignore_duplicate_articles = {'url'}
    resolve_internal_links = True
    oldest_article = 7

    extra_css = '''
        .author-biographies, .content-warning-container-ltr, .category {font-size:small; font-style:italic; font-color:#404040;}
        .byline {font-size:small; font-color:#202020;}
        img {display:block; margin:0 auto;}
    '''

    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 = remove_tags_after = [dict(name='div', attrs={'class': 'post'})]

    remove_tags = [dict(name='button'), classes('font-size sharedaddy comments-form-row')]

    def preprocess_html(self, soup):
        h1 = soup.find(attrs={'class': 'title'})
        if h1 and h1.find('a'):
            h1.a.name = 'h1'
        return soup

    feeds = [('Articles', 'http://strangehorizons.com/wordpress/feed/')]
