__license__ = 'GPL v3'
__copyright__ = '2009-2010, Darko Miletic <darko.miletic at gmail.com>'
'''
spectator.org
'''

from calibre.web.feeds.news import BasicNewsRecipe
from css_selectors import Select


class TheAmericanSpectator(BasicNewsRecipe):
    title = 'The American Spectator'
    __author__ = 'Kovid Goyal'
    description = 'News from USA'
    oldest_article = 7
    max_articles_per_feed = 100
    no_stylesheets = True
    use_embedded_content = False
    language = 'en'
    auto_cleanup = True
    encoding = 'utf-8'

    def parse_index(self):
        root = self.index_to_soup(
            'http://spectator.org/issues/current', as_tree=True)
        select = Select(root)
        main = tuple(select('div#block-system-main'))[0]
        feeds = []
        for div in select('div.item-list', main):
            for h3 in div.xpath('./h3'):
                section_title = self.tag_to_string(h3)
                self.log('\n' + section_title)
                break
            else:
                continue
            articles = []
            for li in div.xpath('descendant::li'):
                for x in select('div.views-field-title', li):
                    title = self.tag_to_string(x)
                    break
                else:
                    raise ValueError('No article title found')
                url = 'http://spectator.org' + li.xpath('./a/@href')[0]
                desc = ''
                for x in select('div.views-field-field-short-summary', li):
                    desc = self.tag_to_string(x)
                    break
                articles.append(
                    {'title': title, 'url': url, 'description': desc})
                self.log('\t', title, 'at', url)
            feeds.append((section_title, articles))
        return feeds
