#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

__license__ = 'GPL v3'
__copyright__ = '2009, Gerhard Aigner <gerhard.aigner at gmail.com>'

''' http://www.derstandard.at - Austrian Newspaper '''

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 DerStandardRecipe(BasicNewsRecipe):
    title = 'derStandard'
    __author__ = 'Gerhard Aigner and Sujata Raman and Marcel Jira and Peter Reschenhofer'
    description = 'Nachrichten aus Österreich'
    publisher = 'derStandard.at'
    category = 'news, politics, nachrichten, Austria'
    use_embedded_content = False
    remove_empty_feeds = True
    no_stylesheets = True
    encoding = 'utf-8'
    language = 'de_AT'

    oldest_article = 1
    max_articles_per_feed = 100
    ignore_duplicate_articles = {'title', 'url'}

    masthead_url = 'http://images.derstandard.at/2012/06/19/derStandardat_1417x274.gif'

    feeds = [
        ('International', 'https://www.derstandard.at/rss/international'),
        ('Inland', 'https://www.derstandard.at/rss/inland'),
        ('Wirtschaft', 'https://www.derstandard.at/rss/wirtschaft'),
        ('Web', 'https://www.derstandard.at/rss/web'),
        ('Sport', 'https://derStandard.at/rss/Sport'),
        ('Panorama', 'https://derStandard.at/rss/Panorama'),
        ('Etat', 'https://derStandard.at/rss/Etat'),
        ('Kultur', 'https://derStandard.at/rss/Kultur'),
        ('Wissenschaft', 'https://derStandard.at/rss/Wissenschaft'),
        ('Gesundheit', 'https://derStandard.at/rss/Gesundheit'),
        # (u'Bildung', u'https://derStandard.at/rss/Bildung'),
        # (u'Meinung', u'https://derStandard.at/rss/Meinung'),
        ('Lifestyle', 'https://derStandard.at/rss/Lifestyle'),
        # (u'Reisen', u'https://derStandard.at/rss/Reisen'),
        # (u'Familie', u'https://derstandard.at/rss/Familie'),
        # (u'Meinung', u'https://derStandard.at/rss/Meinung'),
        # (u'User', u'https://derStandard.at/rss/User'),
        ('Karriere', 'https://derStandard.at/rss/Karriere'),
        # (u'Immobilien', u'https://derstandard.at/rss/Immobilien'),
        # (u'Automobil', u'https://derstandard.at/rss/Automobil'),
        ('dieStandard', 'https://derStandard.at/rss/diestandard'),
        ('Newsroom', 'https://www.derstandard.at/rss'),
    ]

    def get_browser(self):
        br = BasicNewsRecipe.get_browser(self)
        br.set_simple_cookie('DSGVO_Check', '', '.derstandard.at')
        headers = {
            'X-Requested-With': 'XMLHttpRequest',
            'Content-Type': 'application/json; charset=UTF-8',
            'DNT': '1',
            'Pragma': 'no-cache',
            'Cache-Control': 'no-cache',
        }
        import mechanize

        req = mechanize.Request(url='https://apps.derstandard.at/privacyprotection/api/agree', data=None, headers=headers, method='POST')
        br.open(req)
        return br

    keep_only_tags = [
        classes('article-header article-body article-origins article-subtitle article-pubdate'),
    ]

    remove_tags = [
        dict(name=['link', 'iframe', 'style', 'hr']),
        dict(attrs={'class': ['lookup-links', 'media-list']}),
        dict(name='form', attrs={'name': 'sitesearch'}),
        dict(name='div', attrs={'class': ['socialsharing', 'block video', 'blog-browsing section', 'diashow', 'supplemental']}),
        dict(name='div', attrs={'id': 'highlighted'}),
    ]

    remove_attributes = ['width', 'height']
