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


class newsminute(BasicNewsRecipe):
    title = 'The News Minute'
    __author__ = 'unkn0wn'
    description = (
        'The News Minute is a digital news platform reporting and writing on issues in India, with a '
        'specific focus on the 5 southern states. Our content includes news, ground reportage, news '
        'analysis, opinion and blogs. Our core strengths include our deep access in the southern states, '
        'incisive editorial acumen and insightful news analysis and opinions.'
    )
    language = 'en_IN'

    no_stylesheets = True
    remove_javascript = True
    masthead_url = 'https://pkcindia.com/wp-content/uploads/2021/09/TMN-Logo-1.png'
    ignore_duplicate_articles = {'title', 'url'}
    resolve_internal_links  = True
    remove_empty_feeds = True
    remove_attributes = ['style', 'height', 'width']

    keep_only_tags = [
        classes(
            'arr--section-name arr--story--headline-h1 arr--sub-headline arr--hero-image author-card-wrapper arr--story-page-card-wrapper'
        ),
    ]

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

    def parse_index(self):
        index = 'https://www.thenewsminute.com/'
        sections = [
            'tamil-nadu', 'telangana', 'andhra-pradesh', 'karnataka', 'kerala'
        ]
        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
                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
