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


class DeutscheWelle_hr(BasicNewsRecipe):
    title = 'Deutsche Welle'
    __author__ = 'unkn0wn'
    description = 'Vesti iz Njemacke i svijeta'
    publisher = 'Deutsche Welle'
    category = 'news, politics, Germany'
    oldest_article = 2
    max_articles_per_feed = 100
    use_embedded_content = False
    no_stylesheets = True
    language = 'hr'
    publication_type = 'newsportal'
    remove_empty_feeds = True
    masthead_url = 'http://www.dw-world.de/skins/std/channel1/pics/dw_logo1024.gif'
    remove_javascript = True

    ignore_duplicate_articles = {'title', 'url'}
    remove_attributes = ['height', 'width', 'style']

    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 = [dict(name='article')]

    remove_tags = [dict(name=['footer', 'source']), dict(attrs={'data-tracking-name': 'sharing-icons-inline'}), classes('kicker advertisement vjs-wrapper')]

    feeds = [
        ('Svijet', 'http://rss.dw-world.de/rdf/rss-cro-svijet'),
        ('Europa', 'http://rss.dw-world.de/rdf/rss-cro-eu'),
        ('Njemacka', 'http://rss.dw-world.de/rdf/rss-cro-ger'),
        ('Vijesti', 'http://rss.dw-world.de/rdf/rss-cro-all'),
    ]

    def preprocess_html(self, soup):
        for img in soup.findAll('img', srcset=True):
            img['src'] = img['srcset'].split()[6]
        return soup
