#!/usr/pkg/bin/python3.13

__license__ = 'GPL v3'
__author__ = '2010, Gustavo Azambuja <hola at gazambuja.com>'
'''
Muy Interesante
'''

from calibre.web.feeds.news import BasicNewsRecipe


class General(BasicNewsRecipe):
    title = 'Cosmopolitan'
    __author__ = 'Gustavo Azambuja'
    description = 'Revista Cosmopolitan, Edicion Espanola'
    language = 'es'
    timefmt = '[%a, %d %b, %Y]'
    use_embedded_content = False
    recursion = 1
    encoding = 'utf8'
    remove_javascript = True
    no_stylesheets = True
    conversion_options = {'linearize_tables': True}

    oldest_article = 180
    max_articles_per_feed = 100
    keep_only_tags = [
        dict(id=['contenido']),
        dict(name='td', attrs={'class': ['contentheading', 'txt_articulo']})
    ]
    remove_tags = [
        dict(name='div', attrs={'class': ['breadcrumb', 'bloque1', 'article', 'bajo_title',
                                          'tags_articles', 'otrosenlaces_title', 'otrosenlaces_parent', 'compartir']}),
        dict(name='div', attrs={'id': 'comment'}),
        dict(name='table', attrs={'class': 'pagenav'}),
        dict(name=['object', 'link'])
    ]
    remove_attributes = ['width', 'height', 'style', 'font', 'color']

    extra_css = '''
                h1{font-family:Geneva, Arial, Helvetica, sans-serif;color:#154B7A;}
                h3{font-size: 14px;color:#999999; font-family:Geneva, Arial, Helvetica, sans-serif;font-weight: bold;}
                h2{color:#666666; font-family:Geneva, Arial, Helvetica, sans-serif;font-size:small;}
                img {float:left; clear:both; margin:10px}
                p {font-family:Arial,Helvetica,sans-serif;}
                '''
    feeds = [
        (u'Articulos', u'http://feeds.feedburner.com/cosmohispano/FSSt')
    ]

    def preprocess_html(self, soup):
        attribs = ['style', 'font', 'valign', 'colspan', 'width', 'height', 'rowspan', 'summary', 'align', 'cellspacing', 'cellpadding', 'frames', 'rules', 'border' ]  # noqa
        for item in soup.body.findAll(name=['table', 'td', 'tr', 'th', 'caption', 'thead', 'tfoot', 'tbody', 'colgroup', 'col']):
            item.name = 'div'
            for attrib in attribs:
                item[attrib] = ''
                del item[attrib]
        return soup

    def get_cover_url(self):
        index = 'http://www.cosmohispano.com/revista'
        soup = self.index_to_soup(index)
        link_item = soup.find('img', attrs={'class': 'img_portada'})
        if link_item:
            cover_url = "http://www.cosmohispano.com" + link_item['src']
        return cover_url
