'''
www.business-standard.com
'''

from calibre.web.feeds.news import BasicNewsRecipe, classes

class BusinessStandard(BasicNewsRecipe):
    title = 'Business Standard | Print Edition'
    __author__ = 'unkn0wn'
    description = "India's most respected business daily"
    no_stylesheets = True
    use_embedded_content = False
    encoding = 'utf-8'
    publisher = 'Business Standard Limited'
    category = 'news, business, money, india, world'
    language = 'en_IN'
    extra_css = '''
        .article__desc{font-size:small;}
        .article_image{font-size:small; font-style:italic;}
        .article__dateline{font-size:small;}
        .full-img{font-size:small; font-style:italic; text-align:center;}
        .pubDate{font-size:small; text-align:center;}
    '''

    masthead_url = 'https://bsmedia.business-standard.com/include/_mod/site/html5/images/business-standard-logo.png'

    def get_cover_url(self):
        soup = self.index_to_soup(
            'https://www.magzter.com/IN/Business-Standard-Private-Ltd/Business-Standard/Newspaper/'
        )
        for citem in soup.findAll(
            'meta', content=lambda s: s and s.endswith('view/3.jpg')
        ):
            return citem['content']

    remove_attributes = ['width', 'height', 'style']

    keep_only_tags = [
        classes('article__title article__content article_content article_image article__dateline headline'
                ' alternativeHeadline full-img article-content__img pubDate'),
        dict(name='section', attrs={'subscriptions-section':'content'}),
        dict(name='span', attrs={'class':'p-content'})
    ]
    remove_tags = [
        classes('also-read-panel related-keyword more-stories-pagination'),
        dict(name='br')
    ]
    
    def parse_index(self):
        soup = self.index_to_soup('https://www.business-standard.com/todays-paper')
        ans = self.bs_parse_index(soup)
        return ans

    def bs_parse_index(self, soup):
        feeds = []
        div = soup.find('div', attrs={'class':'main-cont-left'})
        for section in div.findAll('div', attrs={'class': 'row-inner'}):
            h2 = section.find('h2')
            secname = self.tag_to_string(h2)
            self.log(secname)
            articles = []
            for a in section.findAll(
                'a', href=lambda x: x and x.startswith('/article/')
            ):
                url = a['href'].replace('article','article-amp')
                url = 'https://wap.business-standard.com' + url
                title = self.tag_to_string(a).strip().replace('Premium Content','')
                articles.append({'title': title, 'url': url})
                self.log('\t', title, '\n\t\t', url)
            if articles:
                feeds.append((secname, articles))
        return feeds
    
    def preprocess_html(self, soup):
        subs = soup.find('section', attrs={'subscriptions-section':'content'})
        if subs:
            art = soup.find(**classes('article_image'))
            if art:
                art.extract()
            div = soup.find(**classes('article_content'))
            if div:
                div.extract()
        h2 = soup.find('h2')
        if h2:
            h2.name = 'h4'
        for img in soup.findAll('amp-img', src=True):
            img.name = 'img'
            img['src'] = img['src'].replace('\\','').split('?')[0]
        return soup        