from __future__ import with_statement
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'

from calibre.web.feeds.news import BasicNewsRecipe

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

    oldest_article        = 7
    __author__            = 'Kovid Goyal'
    max_articles_per_feed = 100
    no_stylesheets = True

    auto_cleanup = True


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

    def parse_index(self):
        soup = self.index_to_soup('http://www.thehindu.com/todays-paper/')
        div = soup.find('div', attrs={'id':'left-column'})
        soup.find(id='subnav-tpbar').extract()



        current_section = None
        current_articles = []
        feeds = []
        for x in div.findAll(['a', 'span']):
            if x.name == 'span' and x['class'] == 's-link':
                # Section heading found
                if current_articles and current_section:
                    feeds.append((current_section, current_articles))
                current_section = self.tag_to_string(x)
                current_articles = []
                self.log('\tFound section:', current_section)
            elif x.name == 'a':

                        title = self.tag_to_string(x)
                        url = x.get('href', False)
                        if not url or not title:
                            continue
                        self.log('\t\tFound article:', title)
                        self.log('\t\t\t', url)
                        current_articles.append({'title': title, 'url':url,
                            'description':'', 'date':''})

        if current_articles and current_section:
             feeds.append((current_section, current_articles))

        return feeds

