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


class DeutscheWelle_bs(BasicNewsRecipe):
    title = 'Deutsche Welle'
    __author__ = 'unkn0wn'
    description = 'Vijesti 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 = 'bs'
    publication_type = 'newsportal'
    remove_empty_feeds = True
    remove_javascript = True
    masthead_url = 'http://www.dw-world.de/skins/std/channel1/pics/dw_logo1024.gif'

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

    keep_only_tags = [dict(name='article')]

    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)

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

    feeds = [
        ('Politika', 'http://rss.dw-world.de/rdf/rss-bos-pol'),
        ('Evropa', 'http://rss.dw-world.de/rdf/rss-bos-eu'),
        ('Kiosk', 'http://rss.dw-world.de/rdf/rss-bos-eu'),
        ('Ekonomija i Nuka', 'http://rss.dw-world.de/rdf/rss-bos-eco'),
        ('Kultura', 'http://rss.dw-world.de/rdf/rss-bos-cul'),
        ('Sport', 'http://rss.dw-world.de/rdf/rss-bos-sp'),
    ]

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