# -*- mode: python; coding: utf-8; -*-
# vim: set syntax=python fileencoding=utf-8

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

from calibre.web.feeds.news import BasicNewsRecipe
try:
    from urllib.parse import urlencode
except ImportError:
    from urllib import urlencode


class Haaretz_en(BasicNewsRecipe):
    title = 'Haaretz'
    __author__ = 'Darko Miletic'
    description = "Haaretz.com is the world's leading English-language Website for real-time news and analysis of Israel and the Middle East."
    publisher = 'Haaretz'
    oldest_article = 2
    max_articles_per_feed = 200
    no_stylesheets = True
    encoding = 'utf8'
    use_embedded_content = False
    language = 'en_IL'
    needs_subscription = True
    remove_empty_feeds = True
    ignore_duplicate_articles = {'url'}
    publication_type = 'newspaper'
    PREFIX = 'https://www.haaretz.com'
    LOGIN = 'https://services.haaretz.com/ms-sso/loginUrlEncoded'
    LOGOUT = 'https://services.haaretz.com/ms-sso/logout'
    extra_css             = """
                                body{font-family: Merriweather, "Helvetica Neue", Helvetica, Arial, sans-serif }
                                div.mx time{display: none}
                                div.my time{display: none}
                                div.mq time{display: none}
                                div.mr time{display: none}
                            """

    conversion_options = {
        'comment': description, 'publisher': publisher, 'language': language
    }

    keep_only_tags = [
        dict(name='div', attrs={'data-test': 'articleHeader'}),
        dict(attrs={'data-test': 'articleBodyImage'}),
        dict(name='div', attrs={'class': ['bu a b d e ', 'bu a b d e']}),
        dict(name='div', attrs={'data-test': 'articleBody'})
    ]
    remove_attributes = ['width', 'height']
    remove_tags = [
        dict(name=['div', 'ul', 'button', 'svg'], attrs={'data-test': [
            'audioPlayer', 'newsletter', 'relatedArticles', 'tags', 'writerAlertButton', 'IconAlefLogo']})
    ]

    feeds = [

    (u'Headlines', 'https://www.haaretz.com/srv/haaretz-latest-headlines'),
    (u'Opinion', 'https://www.haaretz.com/srv/opinion-rss'),
    (u'Security & Aviation', 'https://www.haaretz.com/srv/security-&-aviation-rss'),
    (u'Israel News', 'https://www.haaretz.com/srv/israel-news-rss'),
    (u'World News', 'https://www.haaretz.com/srv/world-news-rss'),
    (u'Jewish World', 'https://www.haaretz.com/srv/jewish-world-rss'),
    (u'Business', 'https://www.haaretz.com/srv/business-rss'),
    (u'Science & Health', 'https://www.haaretz.com/srv/science-&-health-rss'),
    (u'Life & Culture', 'https://www.haaretz.com/srv/life-&-culture-rss'),
    (u'Books', 'https://www.haaretz.com/srv/books-rss'),
    (u'Food', 'https://www.haaretz.com/srv/food-rss')
    ]

    def get_browser(self):
        br = BasicNewsRecipe.get_browser(self)
        br.open(self.PREFIX)
        if self.username is not None and self.password is not None:
            data = urlencode(
                {
                    'newsso': 'true',
                    'fromlogin': 'true',
                    'site': '85',
                    'layer': 'login',
                    'anonymousId': '16549476476914546',
                    'userName': self.username,
                    'password': self.password
                }
            )
            br.open(self.LOGIN, data)
        return br
