__license__ = 'GPL v3'
__copyright__ = '2008 - 2011, Darko Miletic <darko.miletic at gmail.com>'
'''
thenation.com
'''
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 Thenation(BasicNewsRecipe):
    title = 'The Nation'
    __author__ = 'Darko Miletic'
    description = 'Unconventional Wisdom Since 1865'
    publisher = 'The Nation'
    category = 'news, politics, USA'
    oldest_article = 120
    encoding = 'utf-8'
    max_articles_per_feed = 100
    no_stylesheets = True
    language = 'en'
    use_embedded_content = False
    delay = 1
    login_url = 'https://www.thenation.com/login/'
    publication_type = 'magazine'
    needs_subscription = 'optional'
    exra_css = '''
                              body{font-family: Arial,Helvetica,sans-serif;}
                              .print-created{font-size: small;}
                              .caption{display: block; font-size: x-small;}
                            '''

    conversion_options = {'comment': description, 'tags': category, 'publisher': publisher, 'language': language}

    keep_only_tags = [
        classes('title subtitle byline article-body-inner'),
    ]
    remove_tags = [
        dict(name=['link', 'iframe', 'base', 'meta', 'object', 'embed', 'script']),
        classes('email-signup-module current-issue related-newarticle related-multi series-modules'),
    ]
    remove_attributes = ['lang']

    feeds = [('Articles', 'http://www.thenation.com/rss/articles')]

    def get_browser(self):
        br = BasicNewsRecipe.get_browser(self)
        br.open('http://www.thenation.com/')
        if self.username is not None and self.password is not None:
            br.open(self.login_url)
            br.select_form(predicate=lambda form: form.attrs.get('id') == 'lightboxform')
            br['e_mail'] = self.username
            br['pwd'] = self.password
            # The form uses reCaptcha and JS so no go
            # br.submit()
        return br

    def get_cover_url(self):
        soup = self.index_to_soup('http://www.thenation.com/issue/')
        item = soup.find('div', attrs={'id': 'cover-wrapper'})
        if item:
            return item.img['src']
        return None
