#!/usr/bin/env python
# vim:fileencoding=utf-8
__license__ = 'GPL v3'
__copyright__ = '2010-2012, Darko Miletic <darko.miletic at gmail.com>, Rogelio Domínguez <rogelio.dominguez@gmail.com>'
'''
www.jornada.com.mx
'''

import re

try:
    from urllib.parse import urlencode
except ImportError:
    from urllib import urlencode
try:
    from urllib.parse import parse_qs, urlparse, urlunparse
except ImportError:
    from urlparse import parse_qs, urlparse, urlunparse
from calibre import strftime
from calibre.web.feeds.news import BasicNewsRecipe


class LaJornada_mx(BasicNewsRecipe):
    title = 'La Jornada (Mexico)'
    __author__ = 'Darko Miletic/Rogelio Domínguez'
    description = 'Noticias del diario mexicano La Jornada'
    publisher = 'DEMOS, Desarrollo de Medios, S.A. de C.V.'
    category = 'news, Mexico'
    oldest_article = 2
    max_articles_per_feed = 200
    no_stylesheets = True
    encoding = 'utf8'
    use_embedded_content = False
    language = 'es_MX'
    remove_empty_feeds = True
    cover_url = strftime('http://www.jornada.com.mx/%Y/%m/%d/portada.pdf')
    masthead_url = 'http://www.jornada.com.mx/v7.0/imagenes/la-jornada-trans.png'
    publication_type = 'newspaper'
    extra_css = '''
                                body{font-family: "Times New Roman",serif }
                                .cabeza{font-size: xx-large; font-weight: bold }
                                .documentFirstHeading{font-size: xx-large; font-weight: bold }
                                .credito-articulo{font-variant: small-caps; font-weight: bold }
                                .foto{text-align: center}
                                .pie-foto{font-size: 0.9em}
                                .credito{font-weight: bold; margin-left: 1em}
                                .credito-autor{font-variant: small-caps; font-weight: bold }
                                .credito-titulo{text-align: right}
                                .hemero{text-align: right; font-size: 0.9em; margin-bottom: 0.5em }
                                .loc{font-weight: bold}
                                .carton{text-align: center}
                                .credit{font-weight: bold}
                                .sumario{font-weight: bold; text-align: center}
                                .text{margin-top: 1.4em}
                                p.inicial{display: inline; font-size: xx-large; font-weight: bold}
                                p.s-s{display: inline; text-indent: 0}
                            '''

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

    recipe_specific_options = {
        'days': {
            'short': 'Oldest article to download from this news source. In days ',
            'long': 'For example, 0.5, gives you articles from the past 12 hours',
            'default': str(oldest_article),
        }
    }

    def __init__(self, *args, **kwargs):
        BasicNewsRecipe.__init__(self, *args, **kwargs)
        d = self.recipe_specific_options.get('days')
        if d and isinstance(d, str):
            self.oldest_article = float(d)

    preprocess_regexps = [
        (
            re.compile(r'<div class="inicial">(.*)</div><p class="s-s">', re.DOTALL | re.IGNORECASE),
            lambda match: '<p class="inicial">' + match.group(1) + '</p><p class="s-s">',
        )
    ]

    keep_only_tags = [
        dict(name='div', attrs={'class': ['documentContent', 'cabeza', 'sumarios', 'credito-articulo', 'text', 'carton']}),
        dict(name='div', attrs={'id': 'renderComments'}),
    ]
    remove_tags = [dict(name='div', attrs={'class': ['buttonbar', 'comment-cont']})]

    feeds = [
        ('Opinion', 'http://www.jornada.com.mx/rss/opinion.xml'),
        ('Cartones', 'http://www.jornada.com.mx/rss/cartones.xml'),
        ('Politica', 'http://www.jornada.com.mx/rss/politica.xml'),
        ('Economia', 'http://www.jornada.com.mx/rss/economia.xml'),
        ('Mundo', 'http://www.jornada.com.mx/rss/mundo.xml'),
        ('Estados', 'http://www.jornada.com.mx/rss/estados.xml'),
        ('Capital', 'http://www.jornada.com.mx/rss/capital.xml'),
        ('Sociedad y justicia', 'http://www.jornada.com.mx/rss/sociedad.xml'),
        ('Ciencias', 'http://www.jornada.com.mx/rss/ciencias.xml'),
        ('Cultura', 'http://www.jornada.com.mx/rss/cultura.xml'),
        ('Gastronomia', 'http://www.jornada.com.mx/rss/gastronomia.xml'),
        ('Espectaculos', 'http://www.jornada.com.mx/rss/espectaculos.xml'),
        ('Deportes', 'http://www.jornada.com.mx/rss/deportes.xml'),
        ('Ultimas noticias', 'http://www.jornada.com.mx/ultimas/news/RSS'),
    ]

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

    def get_article_url(self, article):
        # Get link to original article URL
        rurl = article.get('guid', None)
        if not rurl:
            # Use the "link" attribute as failover
            return article.get('link', None)
        # Remove "partner" query param
        u = urlparse(rurl)
        query = parse_qs(u.query)
        query.pop('partner', None)
        u = u._replace(query=urlencode(query, True))
        return urlunparse(u)
