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

def absurl(x):
    if x.startswith('/'):
        x = 'https://www.livelaw.in' + x
    return x
    
class livelaw(BasicNewsRecipe):
    title = 'Live Law'
    __author__ = 'unkn0wn'
    description = (
        'Live Law is a comprehensive legal news portal which is committed to providing accurate'
        ' and honest news about legal developments.')
    no_stylesheets = True
    use_embedded_content = False
    encoding = 'utf-8'
    language = 'en_IN'
    remove_attributes = ['height', 'width', 'style'] 
    masthead_url = 'https://www.livelaw.in/images/logo.png'
    timefmt = '[%d %b %Y]'
    oldest_article = 2 #days
    max_articles_per_feed = 20
    ignore_duplicate_articles = {'title', 'url'}
    extra_css = '[data-datestring]{font-size:smaller;}'  
     
    keep_only_tags = [
        classes('trending_heading author-on-detail details-date-time detail_img_cover details-content-story')
    ]
    
    remove_tags = [
        dict(name = 'div', attrs = {'id' : lambda x: x and x.startswith('inside_post_content_ad')}),    
        dict(name = 'div', attrs = {'id' : lambda x: x and x.startswith('filler_ad')})
    ]
    
    def articles_from_soup(self, soup):
        ans = []
        div = soup.find('div', **classes('news_listing_section_mixin'))
        for	h2 in div.findAll('h2', **classes('text_heading')):
            a = h2.find('a', href = True)
            title = self.tag_to_string(a)
            url = absurl(a['href'])
            d = h2.find_next_sibling('div')
            if d:
                date = self.tag_to_string(d)
            if not url or not title:
                    continue
            self.log('\t', title)
            self.log('\t\t', url)
            ans.append({
                'title': title,
                'url': url,
                'date': date})
        return ans

    def parse_index(self):
        soup = self.index_to_soup('https://www.livelaw.in')
        nav_div = soup.find('div', **classes('navbar_center'))
        section_list = []

        # Finding all the section titles that are acceptable
        for a in nav_div.findAll(['a']):
            if self.is_accepted_entry(a):
                section_list.append((self.tag_to_string(a), absurl(a['href'])))
        feeds = []

        # For each section title, fetch the article urls
        for section in section_list:
            section_title = section[0]
            section_url = section[1]
            self.log(section_title, section_url)
            soup = self.index_to_soup(section_url)
            articles = self.articles_from_soup(soup)
            if articles:
                feeds.append((section_title, articles))
        return feeds

    def is_accepted_entry(self, entry):
        # Those sections in the top nav bar that we will omit
        omit_list = [
            'videos', 'job-updates', 'book-reviews', 'events-corner', 'law-firms', 'sc-judgments', 
            'hindi.livelaw.in', 'lawschool', 'ibc-cases', 'tax-cases', 'arbitration-cases', 
            'sponsored', 'rti', 'environment', 'know-the-law', 'round-ups', 'interviews']
        is_accepted = True
        for omit_entry in omit_list:
            if entry['href'].endswith(omit_entry):
                is_accepted = False
                break
        return is_accepted


calibre_most_common_ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36'