#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>

from __future__ import absolute_import, division, print_function, unicode_literals

import re

from calibre.web.feeds.news import BasicNewsRecipe


def classes(classes):
    q = frozenset(classes.split(' '))
    return dict(attrs={
        'class': lambda x: x and frozenset(x.split()).intersection(q)})


class TheHindu(BasicNewsRecipe):
    title = u'The Business Line'
    language = 'en_IN'

    oldest_article = 7
    __author__ = 'Dhiru'
    max_articles_per_feed = 100
    no_stylesheets = True

    keep_only_tags = [
        dict(name='h1'),
        classes('textbyline article-image contentbody'),
    ]
    remove_tags = [
    ]

    extra_css = '.photo-caption { font-size: smaller }'

    def preprocess_html(self, soup, *a):
        for img in soup.findAll(attrs={'data-proxy-image': True}):
            img['src'] = re.sub(r'/alternates/[^/]+', '/alternates/LANDSCAPE_730', img['data-proxy-image'], flags=re.I)
        return soup

    def parse_index(self):
        soup = self.index_to_soup(
            'https://www.thehindubusinessline.com/todays-paper/tp-index')
        div = soup.find(attrs={'class': 'left-column'})
        feeds = []
        current_section = None
        current_articles = []
        for x in div.findAll(['h2', 'li']):
            if current_section and x.name == 'li':
                a = x.find('a', href=True)
                if a is not None:
                    title = self.tag_to_string(a)
                    current_articles.append({'url': a['href'], 'title': title, 'date': '', 'description': ''})
                    self.log('\t' + title)
            if x.name == 'h2':
                if current_section and current_articles:
                    feeds.append((current_section, current_articles))
                current_section = self.tag_to_string(x).strip().capitalize()
                self.log(current_section)
                current_articles = []
        return feeds
