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


class DeutscheWelle_en(BasicNewsRecipe):
    title = 'Deutsche Welle'
    __author__ = 'unkn0wn'
    description = 'News from Germany and the world'
    publisher = 'Deutsche Welle'
    language = 'en'

    oldest_article = 2
    max_articles_per_feed = 50
    no_stylesheets = True
    remove_javascript = True
    remove_empty_feeds = 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 = [
        ('World', 'http://rss.dw.de/rdf/rss-en-world'),
        ('Germany', 'http://rss.dw.de/rdf/rss-en-ger'),
        ('Europe', 'http://rss.dw.de/rdf/rss-en-eu'),
        ('Business', 'http://rss.dw.de/rdf/rss-en-bus'),
        ('Culture & Lifestyle', 'http://rss.dw.de/rdf/rss-en-cul'),
        ('Sports', 'http://rss.dw.de/rdf/rss-en-sports'),
        ('Visit Germany', 'http://rss.dw.de/rdf/rss-en-visitgermany'),
        ('Asia', 'http://rss.dw.de/rdf/rss-en-asia'),
        ('Top Stories', 'http://rss.dw-world.de/rdf/rss-en-top'),
    ]

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