from calibre.web.feeds.news import BasicNewsRecipe


class ElFaroDeVigo(BasicNewsRecipe):
    title = 'El Faro de Vigo'
    oldest_article = 1
    max_articles_per_feed = 100
    __author__ = 'Jefferson Frantz'
    description = 'Noticias de Vigo'
    timefmt = ' [%d %b, %Y]'
    language = 'es'
    encoding = 'cp1252'
    no_stylesheets = True
    remove_javascript = True

    feeds = [
        ('Galicia', 'http://www.farodevigo.es/elementosInt/rss/4'),
        ('España', 'http://www.farodevigo.es/elementosInt/rss/6'),
        ('Mundo', 'http://www.farodevigo.es/elementosInt/rss/7'),
        ('Economía', 'http://www.farodevigo.es/elementosInt/rss/10'),
        ('Sociedad y Cultura', 'http://www.farodevigo.es/elementosInt/rss/8'),
        ('Sucesos', 'http://www.farodevigo.es/elementosInt/rss/9'),
        ('Deportes', 'http://www.farodevigo.es/elementosInt/rss/11'),
        ('Agenda', 'http://www.farodevigo.es/elementosInt/rss/21'),
        ('Gente', 'http://www.farodevigo.es/elementosInt/rss/24'),
        ('Televisión', 'http://www.farodevigo.es/elementosInt/rss/25'),
        ('Ciencia y Tecnología', 'http://www.farodevigo.es/elementosInt/rss/26'),
    ]

    extra_css = '''.noticia_texto{ font-family: sans-serif; font-size: medium; text-align: justify }
                                h1{font-family: serif; font-size: x-large; font-weight: bold; color: #000000; text-align: center}
                                h2{font-family: serif; font-size: medium; font-weight: bold; color: #000000; text-align: left}
                                .enlacenegrita10{font-family: serif; font-size: small; font-weight: bold; color: #000000; text-align: left}
                                .noticia_titular{font-family: serif; font-size: x-large; font-weight: bold; color: #000000; text-align: center}'''

    def preprocess_html(self, soup):
        for item in soup.findAll(style=True):
            del item['style']

        url = 'http://estaticos00.farodevigo.es//elementosWeb/mediaweb/images/compartir/barrapunto.gif'
        fitem = soup.find('img', src=url)
        if fitem:
            par = fitem.parent
            par.extract()
        url = 'http://estaticos01.farodevigo.es//elementosWeb/mediaweb/images/compartir/barrapunto.gif'
        fitem = soup.find('img', src=url)
        if fitem:
            par = fitem.parent
            par.extract()
        url = 'http://estaticos02.farodevigo.es//elementosWeb/mediaweb/images/compartir/barrapunto.gif'
        fitem = soup.find('img', src=url)
        if fitem:
            par = fitem.parent
            par.extract()

        return self.adeify_images(soup)

    def postprocess_html(self, soup, first_fetch):
        divs = soup.findAll(True, {'class': 'enlacenegrita10'})
        for div in divs:
            div['align'] = 'left'

        return soup

    keep_only_tags = [dict(name='div', attrs={'class': ['noticias']})]

    remove_tags = [
        dict(name=['object', 'link', 'script', 'ul', 'iframe', 'ol']),
        dict(name='div', attrs={'class': ['noticiadd2', 'cintillo2', 'noticiadd', 'noticiadd2']}),
        dict(name='div', attrs={'class': ['imagen_derecha', 'noticiadd3', 'extraHTML']}),
    ]
