#!/usr/bin/env python
from calibre.web.feeds.news import BasicNewsRecipe, classes


class STHKRecipe(BasicNewsRecipe):
    title = '星島日報 (香港)'
    __author__ = 'unkn0wn'
    description = "The Sing Tao Daily is among Hong Kong's oldest Chinese language newspapers. (https://std.stheadline.com/)"
    category = 'Chinese, News, Hong Kong'
    language = 'zh'
    encoding = 'utf-8'
    masthead_url = 'https://std.stheadline.com/dist/images/logo-v2@2x.png'
    no_stylesheets = True
    remove_javascript = True
    ignore_duplicate_articles = {'title', 'url'}
    remove_empty_feeds = True
    use_embedded_content = False
    remove_attributes = ['style', 'height', 'width']

    extra_css = '''
        img {display:block; margin:0 auto;}
        .date { font-size:small; }
        .caption-text, .media-library-item__attributes { font-size:small; text-align:center; }
    '''

    keep_only_tags = [dict(name='article', attrs={'class': 'content'})]
    remove_tags = [
        dict(name=['video', 'svg', 'button']),
        dict(attrs={'id': 'articleShareIcons'}),
        classes('in-article-banner stick-box-gray article-pagination comments'),
    ]

    def parse_index(self):
        index = 'https://std.stheadline.com/'
        sections = ['daily', 'realtime', 'supplement']
        feeds = []
        soup = self.index_to_soup(index)
        for sec in sections:
            section = sec.capitalize()
            self.log(section)
            articles = []
            for a in soup.findAll('a', attrs={'href': lambda x: x and x.startswith(index + sec + '/')}):
                url = a['href']
                if url in {index + sec + '/', index + sec}:
                    continue
                if '/article/' not in url:
                    continue
                title = self.tag_to_string(a)
                self.log('\t', title, '\n\t\t', url)
                articles.append({'title': title, 'url': url})
            if articles:
                feeds.append((section, articles))
        return feeds
