__license__   = 'GPL v3'
__copyright__ = '2008-2011, Darko Miletic <darko.miletic at gmail.com>'
'''
newyorker.com
'''

from calibre.web.feeds.news import BasicNewsRecipe

class NewYorker(BasicNewsRecipe):
    title                 = 'The New Yorker'
    __author__            = 'Darko Miletic'
    description           = 'The best of US journalism'
    oldest_article        = 15
    language              = 'en'
    max_articles_per_feed = 100
    no_stylesheets        = True
    use_embedded_content  = False
    publisher             = 'Conde Nast Publications'
    category              = 'news, politics, USA'
    encoding              = 'cp1252'
    publication_type      = 'magazine'
    masthead_url          = 'http://www.newyorker.com/css/i/hed/logo.gif'
    extra_css             = """
                                body {font-family: "Times New Roman",Times,serif}
                                .articleauthor{color: #9F9F9F; 
                                               font-family: Arial, sans-serif;
                                               font-size: small; 
                                               text-transform: uppercase}
                                .rubric,.dd,h6#credit{color: #CD0021;
                                        font-family: Arial, sans-serif;
                                        font-size: small;
                                        text-transform: uppercase}
                                .descender:first-letter{display: inline; font-size: xx-large; font-weight: bold}
                                .dd,h6#credit{color: gray}
                                .c{display: block}
                                .caption,h2#articleintro{font-style: italic}
                                .caption{font-size: small}
                            """

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

    keep_only_tags = [
                        dict(name='div', attrs={'class':'headers'})
                       ,dict(name='div', attrs={'id':['articleheads','items-container','articleRail','articletext','photocredits']})
                     ]
    remove_tags    = [
                         dict(name=['meta','iframe','base','link','embed','object'])
                        ,dict(attrs={'class':['utils','socialUtils','articleRailLinks','icons'] })
                        ,dict(attrs={'id':['show-header','show-footer'] })
                     ]
    remove_attributes = ['lang']
    feeds             = [(u'The New Yorker', u'http://www.newyorker.com/services/mrss/feeds/everything.xml')]

    def print_version(self, url):
        return url + '?printable=true'

    def image_url_processor(self, baseurl, url):
        return url.strip()

    def get_cover_url(self):
        cover_url = None
        soup = self.index_to_soup('http://www.newyorker.com/magazine/toc/')
        cover_item = soup.find('img',attrs={'id':'inThisIssuePhoto'})
        if cover_item:
           cover_url = 'http://www.newyorker.com' + cover_item['src'].strip()
        return cover_url

    def preprocess_html(self, soup):
        for item in soup.findAll(style=True):
            del item['style']
        auth = soup.find(attrs={'id':'articleauthor'})
        if auth:
           alink = auth.find('a')
           if alink and alink.string is not None:
              txt = alink.string
              alink.replaceWith(txt)
        return soup
