import json
import re

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


class outlook(BasicNewsRecipe):
    title = 'Outlook Magazine'
    __author__ = 'unkn0wn'
    description = (
        'Outlook covers the latest India news, analysis, business news and long-form stories on culture,'
        ' money market and personal finance. Read India\'s best online magazine.'
    )
    language = 'en_IN'
    use_embedded_content = False
    no_stylesheets = True
    remove_javascript = True
    remove_attributes = ['height', 'width', 'style']
    ignore_duplicate_articles = {'url'}
    resolve_internal_links = True

    keep_only_tags = [classes('__story_detail')]
    remove_tags = [
        classes(
            'social_sharing_article left_trending left-sticky __tag_links next_prev_stories	downarrow uparrow more_from_author_links next prev'
        )
    ]

    def parse_index(self):
        soup = self.index_to_soup('https://www.outlookindia.com/magazine')
        div = soup.find('div', attrs={'class':'wrapper'})
        a = div.find('a', href=lambda x: x and x.startswith('/magazine/issue/'))
        url = a['href']
        self.log('Downloading issue:', url)
        soup = self.index_to_soup('https://www.outlookindia.com' + url)
        cover = soup.find(**classes('listingPage_lead_story'))
        self.cover_url = cover.find('img', attrs={'src': True})['src']
        ans = []

        for h3 in soup.findAll(['h3', 'h4'],
                               attrs={'class': 'tk-kepler-std-condensed-subhead'}):
            a = h3.find('a', href=lambda x: x)
            url = a['href']
            title = self.tag_to_string(a)
            desc = ''
            p = h3.find_next_sibling('p')
            if p:
                desc = self.tag_to_string(p)
            self.log('\t', title)
            self.log('\t', desc)
            self.log('\t\t', url)
            ans.append({'title': title, 'url': url, 'description': desc})
        return [('Articles', ans)]

    def preprocess_raw_html(self, raw, *a):
        return raw
        m = re.search('<!-- NewsArticle Schema -->.*?script.*?>', raw, flags=re.DOTALL)
        raw = raw[m.end():].lstrip()
        data = json.JSONDecoder().raw_decode(raw)[0]
        title = data['headline']
        body = data['articleBody']
        body = body.replace('\r\n', '<p>')
        author = ' and '.join(x['name'] for x in data['author'])
        image = desc = ''
        if data.get('image'):
            image = '<p><img src="{}">'.format(data['image']['url'])
        if data.get('description'):
            desc = '<h2>' + data['description'] + '</h2>'
        html = '<html><body><h1>' + title + '</h1>' + desc + '<h3>' + author + '</h3>' + image + '<p>' + body
        return html


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'