__license__ = 'GPL v3'
__copyright__ = '2012-2013, Darko Miletic <darko.miletic at gmail.com>'
'''
www.nsfwcorp.com
'''

from calibre.web.feeds.news import BasicNewsRecipe


class NotSafeForWork(BasicNewsRecipe):
    title = 'Not Safe For Work Corporation'
    __author__ = 'Darko Miletic'
    description = 'Not Safe For Work Corporation'
    publisher = 'Not Safe For Work Corporation'
    category = 'news, politics, USA, World'
    no_stylesheets = True
    oldest_article = 15
    encoding = 'utf-8'
    needs_subscription = True
    auto_cleanup = False
    INDEX = 'https://www.nsfwcorp.com'
    LOGIN = INDEX + '/account/login/?next=%2F'
    SETTINGS = INDEX + '/account/settings/'
    use_embedded_content = True
    language = 'en'
    publication_type = 'magazine'
    masthead_url = 'http://assets.nsfwcorp.com/media/headers/nsfw_banner.jpg'
    extra_css              = """
        img{margin-top:0.5em; margin-bottom: 0.7em; display: block}
        .fontFace{font-family: 'LeagueGothicRegular',Arial,sans-serif;}
        .dispatchTitle{text-transform: uppercase; font-size: x-large; font-weight: bold}
        .dispatchSubtitle{font-size: x-large; font-weight: bold}
        #fromLine{color: gray; text-align:right}
        #toLine{color: gray}
        #toLine a{color: red}
        #fromLine a{color: red}
        .row a{color: red}
                             """

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

    def get_browser(self):
        br = BasicNewsRecipe.get_browser(self)
        br.open(self.INDEX)
        if self.username is not None and self.password is not None:
            br.open(self.LOGIN)
            br.select_form(nr=0)
            br['email'] = self.username
            br['password'] = self.password
            br.submit()
        return br

    def get_feeds(self):
        self.feeds = []
        soup = self.index_to_soup(self.SETTINGS)
        for item in soup.findAll('input', value=True, attrs={'type': 'text'}):
            if item['value'].startswith('https://www.nsfwcorp.com/feed/'):
                self.feeds.append(item['value'])
                return self.feeds
        return self.feeds
