#!/usr/bin/env python
# -*- coding: utf-8 -*-

__license__ = 'GPL v3'
__copyright__ = '30 October 2010, Jordi Balcells based on an earlier recipe by Darko Miletic <darko.miletic at gmail.com>'
'''
elperiodico.cat
'''

from calibre.ebooks.BeautifulSoup import Tag
from calibre.web.feeds.news import BasicNewsRecipe


def new_tag(soup, name, attrs=()):
    impl = getattr(soup, 'new_tag', None)
    if impl is not None:
        return impl(name, attrs=dict(attrs))
    return Tag(soup, name, attrs=attrs or None)


class ElPeriodico_cat(BasicNewsRecipe):
    title = 'El Periodico de Catalunya'
    __author__ = 'Jordi Balcells/Darko Miletic'
    description = 'Noticias desde Catalunya'
    publisher = 'elperiodico.com'
    category = 'news, politics, Spain, Catalunya'
    oldest_article = 2
    max_articles_per_feed = 100
    no_stylesheets = True
    use_embedded_content = False
    delay = 1
    encoding = 'cp1252'
    language = 'es'

    html2lrf_options = ['--comment', description, '--category', category, '--publisher', publisher]

    html2epub_options = 'publisher="' + publisher + '"\ncomments="' + description + '"\ntags="' + category + '"'

    feeds = [
        ('Portada', 'http://www.elperiodico.com/es/rss/rss_portada.xml'),
        ('Internacional', 'http://elperiodico.com/es/rss/internacional/rss.xml'),
        ('Sociedad', 'http://elperiodico.com/es/rss/sociedad/rss.xml'),
        ('Ciencia y Tecnolog\xeda', 'http://elperiodico.com/es/rss/ciencia-y-tecnologia/rss.xml'),
        ('Deportes', 'http://elperiodico.com/es/rss/deportes/rss.xml'),
        ('Gente', 'http://elperiodico.com/es/rss/gente/rss.xml'),
        ('Opini\xf3n', 'http://elperiodico.com/es/rss/opinion/rss.xml'),
        ('Pol\xedtica', 'http://elperiodico.com/es/rss/politica/rss.xml'),
        ('Barcelona', 'http://elperiodico.com/es/rss/barcelona/rss.xml'),
        ('Econom\xeda', 'http://elperiodico.com/es/rss/economia/rss.xml'),
        ('Cultura y espect\xe1culos', 'http://elperiodico.com/es/rss/cultura-y-espectaculos/rss.xml'),
        ('Tele', 'http://elperiodico.com/es/rss/cultura-y-espectaculos/rss.xml'),
    ]

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

    remove_tags = [dict(name='div', attrs={'class': ['opcionb', 'opcionb last', 'columna_noticia']}), dict(name='span', attrs={'class': 'opcionesnoticia'})]

    def print_version(self, url):
        return url.replace('/default.asp?', '/print.asp?')

    def preprocess_html(self, soup):
        mcharset = new_tag(soup, 'meta', [('http-equiv', 'Content-Type'), ('content', 'text/html; charset=utf-8')])
        soup.head.insert(0, mcharset)
        for item in soup.findAll(style=True):
            del item['style']
        return soup
