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


class bq(BasicNewsRecipe):
    title = 'BQ Prime'
    description = 'Bloomberg Quint: India’s premier multi-platform business and financial news company.'
    language = 'en_IN'
    __author__ = 'unkn0wn'
    oldest_article = 2  # days
    max_articles_per_feed = 50
    encoding = 'utf-8'
    use_embedded_content = False
    no_stylesheets = True
    remove_attributes = ['style', 'height', 'width']
    masthead_url = 'https://www.bqprime.com/icons/bqprime-blue.svg'
    ignore_duplicate_articles = {'url'}

    keep_only_tags = [
        classes('story-base-template-m__story-content__1sSai print-story-card story-element-text'),
        dict(name='img', attrs={'property': 'og:image'}),
    ]

    remove_tags = [
        classes('desktop-hidden desktop-only bm-container bookmark-m__bookmark-container__30eHl bookmark-m__container__23o1v'),
        dict(name='div', attrs={'id': 'piano-preview-story'}),
    ]

    feeds = [
        ('Articles', 'https://www.bqprime.com/stories.rss'),
    ]

    def preprocess_raw_html(self, raw_html, url):
        return raw_html.replace('<meta property="og:image" content=', '<img property="og:image" src=')

    def preprocess_html(self, soup):
        pic = soup.find('img', attrs={'property': 'og:image'})
        head = soup.find('h1')
        if pic and head:
            head.append(pic)
        return soup
