import re

from calibre.web.feeds.recipes import BasicNewsRecipe


class seznamzpravyRecipe(BasicNewsRecipe):
    __author__ = 'bubak'
    title = 'Seznam zprávy'
    description = 'seznamzpravy.cz'
    oldest_article = 1
    max_articles_per_feed = 20

    feeds = [('Nejnovější články', 'http://www.seznamzpravy.cz/rss')]

    language = 'cs'
    cover_url = 'https://www.seznam.cz/media/img/logo_v2.svg'
    remove_javascript = True
    no_stylesheets = True
    use_embedded_content = False

    remove_tags_before = dict(name='div', attrs={'class': ['c_fT']})
    remove_tags = [
        dict(
            name='div',
            attrs={
                'class': [
                    'g_c9',
                    'd_eQ',
                    'd_eS',
                    'e-aJ',
                    'g_dE',
                    'XXXg_bj',
                    'd_e3',
                    'g_bv',
                    'd_eT',
                    'h_hA',
                    'g_bv g_bt',
                    'e_S g_bz',
                    'g_ei',
                    'e_S',
                    'c_fP',
                    'c_ix',
                ]
            },
        ),
        dict(
            name='div',
            attrs={
                'data-dot': [
                    'ogm-header',
                    'ogm-ribbon',
                    'ogm-actual-box',
                    'ogm-main-media',
                    'mol-embed',
                    'ogm-breadcrumb-navigation',
                    'ogm-related-documents',
                    'ogm-related-tags',
                    'mol-articles-container',
                    'ogm-document-timeline',
                    'mol-document-links',
                ]
            },
        ),
        dict(name='div', attrs={'data-e2e': ['mol-advert']}),
        dict(name='header', attrs={'data-dot': ['tpl-mainHeader']}),
        dict(name='h6', attrs={'id': ['accessibility-article', 'accessibility-header', 'accessibility-content']}),
        dict(name='div', attrs={'class': re.compile(r'breadcrumb', re.IGNORECASE)}),
        dict(name='div', attrs={'data-dot': re.compile(r'ogm-date', re.IGNORECASE)}),
        dict(name='svg'),
        dict(name='figure'),
        dict(name='aside'),
    ]
    remove_tags_after = dict(name='div', attrs={'data-dot': 'ogm-related-tags'})

    visited_urls = {}

    def get_article_url(self, article):
        url = BasicNewsRecipe.get_article_url(self, article)
        if url in self.visited_urls:
            self.log.debug('Ignoring duplicate: ' + url)
            return None
        else:
            self.visited_urls[url] = True
            self.log.debug('Accepting: ' + url)
            return url

    def populate_article_metadata(self, article, soup, first):
        self.log('Article date ', article.date)
