#!/usr/bin/env python
__license__ = 'GPL v3'
__copyright__ = '2009, Matthew Briggs'
__docformat__ = 'restructuredtext en'

'''
http://www.theaustralian.news.com.au/
'''

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 DailyTelegraph(BasicNewsRecipe):
    title = 'The Australian'
    __author__ = 'Kovid Goyal'
    description = (
        'National broadsheet newspaper from down under - colloquially known as The Oz'
        '. You will need to have a subscription to '
        'http://www.theaustralian.com.au to get full articles.'
    )
    language = 'en_AU'

    oldest_article = 2
    needs_subscription = 'optional'
    max_articles_per_feed = 30
    remove_javascript = True
    no_stylesheets = True
    encoding = 'utf8'
    remove_empty_feeds = True
    ignore_duplicate_articles = {'url'}

    keep_only_tags = [
        classes('story-headline'),
        dict(id='story'),
    ]
    remove_tags = [
        dict(id='comments'),
        classes(
            'story-info story-header-tools module-controls story-sidebar'
            ' story-footer story-footer-tools story-extras story-related vms-nav'
            ' vms-endcard vms-discover share-tools story-comments-link'
            ' vms-controls ooyala-player vms-countdown vms-header comments'
        ),
    ]

    feeds = [
        ('National News', 'https://www.news.com.au/content-feeds/latest-news-national/'),
        ('World News', 'https://www.news.com.au/content-feeds/latest-news-world/'),
        ('Entertainment', 'https://www.news.com.au/content-feeds/latest-news-entertainment/'),
        ('Technology', 'https://www.news.com.au/content-feeds/latest-news-technology/'),
        ('Lifestyle', 'https://www.news.com.au/content-feeds/latest-news-lifestyle/'),
        ('Sport', 'https://www.news.com.au/content-feeds/latest-news-sport/'),
        ('Finance', 'https://www.news.com.au/content-feeds/latest-news-finance/'),
        ('Travel', 'https://www.news.com.au/content-feeds/latest-news-travel/'),
    ]

    def get_browser(self):
        return BasicNewsRecipe.get_browser(self, user_agent='common_words/based')

    def get_article_url(self, article):
        ans = article.link
        if '/video/' in ans:
            return
        return ans

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