#!/usr/bin/env python
# vim:fileencoding=utf-8
from __future__ import unicode_literals, division, absolute_import, print_function

__license__ = 'GPL v3'
__copyright__ = '2010, Darko Miletic <darko.miletic at gmail.com>'

'''
Deutsche Welle (english) - dw.com/en
'''

import re
from calibre.web.feeds.news import BasicNewsRecipe


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

    oldest_article = 1
    max_articles_per_feed = 50
    no_stylesheets = True
    remove_javascript = True
    remove_empty_feeds = True
    ignore_duplicate_articles = {'title', 'url'}

    feeds = [
        ('Top Stories', 'http://rss.dw-world.de/rdf/rss-en-top'),
        ('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')
    ]

    keep_only_tags = [
        dict(name='div', attrs={'class': 'col3'})
    ]

    remove_tags_after = [
        dict(name='div', attrs={'class': 'group'})
    ]

    remove_tags = [
        dict(name='div', attrs={'class': 'col1'}),
        dict(name='div', attrs={'class': re.compile('gallery')}),
        dict(name='div', attrs={'class': re.compile('audio')}),
        dict(name='div', attrs={'class': re.compile('video')})
    ]

    remove_attributes = ['height', 'width',
                         'onclick', 'border', 'lang', 'link']

    extra_css = '''
        h1 {font-size: 1.6em; margin-top: 0em}
        .artikel {font-size: 1em; text-transform: uppercase; margin: 0em}
    '''

    def preprocess_html(self, soup):
        # convert local hyperlinks
        for a in soup.findAll('a', href=True):
            if a['href'].startswith('/'):
                a['href'] = 'http://www.dw.com' + a['href']
            elif a['href'].startswith('#'):
                del a['href']
        # remove all style attributes with an effect on font size
        for item in soup.findAll(attrs={'style': re.compile('font-size')}):
            del item['style']
        return soup
