#!/usr/bin/env python
# vim:fileencoding=utf-8
from calibre import random_user_agent
from calibre.ebooks.BeautifulSoup import Tag
from calibre.web.feeds.recipes import BasicNewsRecipe


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


def new_tag(soup, name, attrs=()):
    impl = getattr(soup, 'new_tag', None)
    if impl is not None:
        return impl(name, attrs=dict(attrs))
    return Tag(soup, name, attrs=attrs or None)


class TheIndependentNew(BasicNewsRecipe):
    title = 'The Independent'
    __author__ = 'Krittika Goyal'
    description = (
        'The latest in UK News and World News from The '
        'Independent. Wide range of international and local news, sports '
        'news, commentary and opinion pieces.Independent News - Breaking news '
        'that matters. Your daily comprehensive news source - The '
        'Independent Newspaper'
    )
    publisher = 'The Independent'
    oldest_article = 2.0
    ignore_duplicate_articles = {'title', 'url'}
    category = 'news, UK'
    no_stylesheets = True
    use_embedded_content = False
    remove_empty_feeds = True
    language = 'en_GB'
    publication_type = 'newspaper'
    encoding = 'utf-8'
    compress_news_images = True

    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(id=['articleHeader', 'main']),
        classes('headline sub-headline breadcrumb author publish-date hero-image body-content'),
    ]
    remove_tags = [
        classes(
            'inline-related inline-readmore ad-wrapper icon-gallery i-gallery article-im-prompt apester-media'
            ' social-share i-amphtml-replaced-content gallery-btn related'
        ),
        dict(name='amp-live-list'),
    ]
    remove_attributes = ['style']

    def get_browser(self, *a, **kw):
        # This site returns images in JPEG-XR format if the user agent is IE
        if not hasattr(self, 'non_ie_ua'):
            try:
                self.non_ie_ua = random_user_agent(allow_ie=False)
            except TypeError:
                self.non_ie_ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.111 Safari/537.36'
        kw['user_agent'] = self.non_ie_ua
        br = BasicNewsRecipe.get_browser(self, *a, **kw)
        return br

    def preprocess_html(self, soup):
        for img in soup.findAll('amp-img'):
            img.name = 'img'
            img['srcset'] = ''

        for div in soup.findAll(attrs={'class': 'full-gallery'}):
            imgs = {}
            for li in div.findAll('li', attrs={'data-gallery-item': True, 'data-original': True}):
                imgs[li['data-gallery-item']] = li['data-original']
                li.extract()
            for li in div.findAll('li', attrs={'data-gallery-legend': True}):
                src = imgs.get(li['data-gallery-legend'])
                if src is not None:
                    img = new_tag(soup, 'img')
                    img['src'] = src
                    img['style'] = 'display:block'
                    li.append(img)
        return soup

    feeds = [
        ('News - UK', 'http://www.independent.co.uk/news/uk/rss'),
        ('News - World', 'http://www.independent.co.uk/news/world/rss'),
        ('News - Business', 'http://www.independent.co.uk/news/business/rss'),
        ('News - People', 'http://www.independent.co.uk/news/people/rss'),
        ('News - Science', 'http://www.independent.co.uk/news/science/rss'),
        ('News - Media', 'http://www.independent.co.uk/news/media/rss'),
        ('News - Education', 'http://www.independent.co.uk/news/education/rss'),
        ('News - Obituaries', 'http://www.independent.co.uk/news/obituaries/rss'),
        ('News - Corrections', 'http://www.independent.co.uk/news/corrections/rss'),
        ('Voices', 'http://www.independent.co.uk/voices/rss'),
        ('Environment', 'http://www.independent.co.uk/environment/rss'),
        ('Sport - Athletics', 'http://www.independent.co.uk/sport/general/athletics/rss'),
        ('Sport - Cricket', 'http://www.independent.co.uk/sport/cricket/rss'),
        ('Sport - Football', 'http://www.independent.co.uk/sport/football/rss'),
        ('Sport - Golf', 'http://www.independent.co.uk/sport/golf/rss'),
        ('Sport - Motor racing', 'http://www.independent.co.uk/sport/motor-racing/rss'),
        ('Sport - Olympics', 'http://www.independent.co.uk/sport/olympics/rss'),
        ('Sport - Racing', 'http://www.independent.co.uk/sport/racing/rss'),
        ('Sport - Rugby League', 'http://www.independent.co.uk/sport/general/rugby-league/rss'),
        ('Sport - Rugby Union', 'http://www.independent.co.uk/sport/rugby/rugby-union/rss'),
        ('Sport - Sailing', 'http://www.independent.co.uk/sport/general/sailing/rss'),
        ('Sport - Tennis', 'http://www.independent.co.uk/sport/tennis/rss'),
        ('Sport - Others', 'http://www.independent.co.uk/sport/general/others/rss'),
        ('Life & Style - Fashion', 'http://www.independent.co.uk/life-style/fashion/rss'),
        ('Life & Style -Food & Drink', 'http://www.independent.co.uk/life-style/food-and-drink/rss'),
        ('Life & Style - Health and Families', 'http://www.independent.co.uk/life-style/health-and-families/rss'),
        ('Life & Style - History', 'http://www.independent.co.uk/life-style/history/rss'),
        ('Life & Style - Gadgets & Tech', 'http://www.independent.co.uk/life-style/gadgets-and-tech/rss'),
        ('Life & Style - Motoring', 'http://www.independent.co.uk/life-style/motoring/rss'),
        ('Arts & Ents - Art', 'http://www.independent.co.uk/arts-entertainment/art/rss'),
        ('Arts & Ents - Architecture', 'http://www.independent.co.uk/arts-entertainment/architecture/rss'),
        ('Arts & Ents - Music', 'http://www.independent.co.uk/arts-entertainment/music/rss'),
        ('Arts & Ents - Classical', 'http://www.independent.co.uk/arts-entertainment/classical/rss'),
        ('Arts & Ents - Films', 'http://www.independent.co.uk/arts-entertainment/films/rss'),
        ('Arts & Ents - TV', 'http://www.independent.co.uk/arts-entertainment/tv/rss'),
        ('Arts & Ents - Theatre and Dance', 'http://www.independent.co.uk/arts-entertainment/theatre-dance/rss'),
        ('Arts & Ents - Comedy', 'http://www.independent.co.uk/arts-entertainment/comedy/rss'),
        ('Arts & Ents - Books', 'http://www.independent.co.uk/arts-entertainment/books/rss'),
        ('Travel', 'http://www.independent.co.uk/travel/rss'),
        ('Money', 'http://www.independent.co.uk/money/rss'),
        ('IndyBest', 'http://www.independent.co.uk/extras/indybest/rss'),
    ]
