#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>

from __future__ import absolute_import, division, print_function, unicode_literals

from calibre.web.feeds.news import BasicNewsRecipe


def classes(classes):
    q = frozenset(classes.split(' '))
    return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})


class TheDailyMail(BasicNewsRecipe):
    title = 'The Daily Mail'
    oldest_article = 2
    language = 'en_GB'

    __author__ = 'Kovid Goyal'
    simultaneous_downloads = 1
    max_articles_per_feed = 50
    use_embedded_content = False
    compress_news_images = True
    compress_news_images_auto_size = 8
    no_stylesheets = True
    delay = 2
    browser_type = 'webengine'

    keep_only_tags = [
        dict(name='h1'),
        classes('author-section byline-section'),
        dict(itemprop='articleBody'),
    ]
    remove_tags = [classes('related-carousel')]

    feeds = [
        ('Home', 'https://www.dailymail.co.uk/home/index.rss'),
        ('News', 'https://www.dailymail.co.uk/news/index.rss'),
        ('Sport', 'https://www.dailymail.co.uk/sport/index.rss'),
        ('TV and Showbiz', 'https://www.dailymail.co.uk/tvshowbiz/index.rss'),
        ('Health', 'https://www.dailymail.co.uk/health/index.rss'),
        ('Science and Technology', 'https://www.dailymail.co.uk/sciencetech/index.rss'),
        ('Money', 'https://www.dailymail.co.uk/money/index.rss'),
        ('Property', 'https://www.dailymail.co.uk/property/index.rss'),
        ('Motoring', 'https://www.dailymail.co.uk/motoring/index.rss'),
        ('Travel', 'https://www.dailymail.co.uk/travel/index.rss'),
    ]

    def preprocess_html(self, soup):
        for img in soup.findAll(attrs={'data-src': True}):
            img['src'] = img['data-src']
        all_h1s = soup.findAll('h1')
        for h1 in all_h1s[1:]:
            h1.extract()
        return soup
