#!/usr/pkg/bin/python3.13

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


class HindustanTimes(BasicNewsRecipe):
    title = u'Hindustan Times'
    description = 'News from India.'
    language = 'en_IN'
    __author__ = 'unkn0wn'
    oldest_article = 1  # days
    max_articles_per_feed = 50
    encoding = 'utf-8'
    use_embedded_content = False
    masthead_url = 'https://epaper.hindustantimes.com//Images/logo/logo.png'
    no_stylesheets = True
    remove_attributes = ['style', 'height', 'width']
    ignore_duplicate_articles = {'url'}
    extra_css = '.about-authors { font-size: smaller }'

    keep_only_tags = [
        classes('fullStory dateTime storyBy storyDetails detail freemiumText paywall'),
    ]

    remove_tags = [
        classes('htsWrapper shareArticle new__newsletter__signup signup__box subscribe freemium-card adMinHeight313'
                ' storyTopics embed_div shareIcons close-btn mainCoachmarks sharePopUp'),
        dict(name='div', attrs={'class':[]}),
        dict(name=['footer', 'button']),
    ]

    feeds = [
        ('Editorial','https://www.hindustantimes.com/feeds/rss/editorials/rssfeed.xml'),
        ('Opinion','https://www.hindustantimes.com/feeds/rss/opinion/rssfeed.xml'),
        ('HT Insight','https://www.hindustantimes.com/feeds/rss/ht-insight/rssfeed.xml'),
        ('Analysis','https://www.hindustantimes.com/feeds/rss/analysis/rssfeed.xml'),
        ('India News','https://www.hindustantimes.com/feeds/rss/india-news/rssfeed.xml'),
        ('World News','https://www.hindustantimes.com/feeds/rss/world-news/rssfeed.xml'),
        ('Business','https://www.hindustantimes.com/feeds/rss/business/rssfeed.xml'),
        ('Science','https://www.hindustantimes.com/feeds/rss/science/rssfeed.xml'),
        ('Education','https://www.hindustantimes.com/feeds/rss/education/rssfeed.xml'),
        ('Elections','https://www.hindustantimes.com/feeds/rss/elections/rssfeed.xml'),
        ('Sports','https://www.hindustantimes.com/feeds/rss/sports/rssfeed.xml'),
        ('Books','https://www.hindustantimes.com/feeds/rss/books/rssfeed.xml'),
        ('HT Weekend','https://www.hindustantimes.com/feeds/rss/ht-weekend/rssfeed.xml'),
        # ('Entertainment','https://www.hindustantimes.com/feeds/rss/entertainment/rssfeed.xml'),
        # ('Lifestyle',''https://www.hindustantimes.com/feeds/rss/lifestyle/rssfeed.xml'),
        # ('Cities',''https://www.hindustantimes.com/feeds/rss/cities/rssfeed.xml'),
        # ('Budget',''https://www.hindustantimes.com/feeds/rss/budget/rssfeed.xml')
        ]

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

    def preprocess_html(self, soup):
        for img in soup.findAll('img', attrs={'data-src': True}):
            img['src'] = img['data-src']
        return soup
