# -*- mode: python -*- # -*- coding: utf-8 -*- __license__ = 'GPL v3' __copyright__ = '2008-2026, Darko Miletic ' ''' pagina12.com.ar ''' import json import re from calibre import strftime from calibre.ptempfile import PersistentTemporaryFile from calibre.web.feeds.news import BasicNewsRecipe def find_global_content(raw_html): """Return the parsed Fusion.globalContent dict, or None if not present.""" # `\s*=\s*` deliberately fails to match `Fusion.globalContentConfig=`, # because the char after "globalContent" there is 'C', not '='. m = re.search(r'Fusion\.globalContent\s*=\s*', raw_html) if m is None: return None try: # raw_decode parses exactly ONE JSON value starting at m.end() and # returns where it stopped, ignoring the ; and following assignments. # This is robust to '{' / '}' appearing inside the article text. obj, _end = json.JSONDecoder().raw_decode(raw_html, m.end()) except ValueError: return None return obj if isinstance(obj, dict) else None def ans_element_to_html(el): """Render a single ANS content_element to an HTML string.""" if not isinstance(el, dict): return '' t = el.get('type') if t == 'text': c = el.get('content', '') return '

%s

' % c if c.strip() else '' if t == 'raw_html': # embeds (tweets, iframes, ads...). Keep or drop as you prefer. return el.get('content', '') if t == 'header': lvl = int(el.get('level', 2) or 2) lvl = min(max(lvl, 1), 6) return '%s' % (lvl, el.get('content', ''), lvl) if t == 'image': url = el.get('url', '') if not url: return '' cap = el.get('caption') or '' out = '
' % url if cap: out += '
%s
' % cap return out + '
' if t == 'list': tag = 'ol' if el.get('list_type') == 'ordered' else 'ul' items = [] for it in el.get('items', []): if isinstance(it, dict) and it.get('type') == 'text': items.append('
  • %s
  • ' % it.get('content', '')) elif isinstance(it, dict): items.append('
  • %s
  • ' % ans_element_to_html(it)) else: items.append('
  • %s
  • ' % it) return '<%s>%s' % (tag, ''.join(items), tag) if t == 'blockquote': return '
    %s
    ' % el.get('content', '') if t == 'quote': # pull quote: nested content_elements inner = ''.join(ans_element_to_html(e) for e in el.get('content_elements', [])) return '
    %s
    ' % inner if t == 'oembed_response': raw = el.get('raw_oembed') or {} return raw.get('html', '') or el.get('html', '') if t == 'code': return '
    %s
    ' % el.get('content', '') if t == 'divider': return '
    ' if t == 'correction': return '

    %s

    ' % el.get('text', '') # Types intentionally skipped: custom_embed, gallery, reference, story, # video, table, interstitial_link, etc. Add handlers here if you need them. return '' class Pagina12(BasicNewsRecipe): title = 'Pagina - 12' __author__ = 'Darko Miletic' description = 'Noticias de Argentina y el resto del mundo' publisher = 'La Pagina S.A.' category = 'news, politics, Argentina' oldest_article = 2 no_stylesheets = True encoding = 'utf8' use_embedded_content = False language = 'es_AR' remove_empty_feeds = True publication_type = 'newspaper' auto_cleanup = False delay = 1 simultaneous_downloads = 1 timeout = 8 needs_subscription = 'optional' ignore_duplicate_articles = {'url'} articles_are_obfuscated = True temp_files = [] fetch_retries = 10 extra_css = ''' body{font-family: "Open Sans", sans-serif} .article-date{font-size: small; margin-bottom: 1em;} .article-title{font-size: x-large; font-weight: bold; display: block; margin-bottom: 1em; margin-top: 1em;} .article-main-media{display: block; margin-bottom: 1em;} .article-summary{margin-top:1em; margin-bottom: 1em; display:block} .article-author{font-family: "Archivo Narrow",Helvetica,sans-serif; color: gray; font-size: small; margin-top:1em; margin-bottom: 1em} img{margin-top:1em; margin-bottom: 1em; display:block} .article-text p:first-letter{display: inline; font-size: xx-large; font-weight: bold} .article-prefix{font-family: "Archivo Narrow",Helvetica,sans-serif; font-size: small; text-transform: uppercase;} ''' conversion_options = {'comment': description, 'tags': category, 'publisher': publisher, 'language': language} def get_browser(self): br = BasicNewsRecipe.get_browser(self) br.open('https://www.pagina12.com.ar/') if self.username is not None and self.password is not None: br.open('https://auth.pagina12.com.ar/ingresar?redirect=https://www.pagina12.com.ar') br.select_form(id='login') br['email'] = self.username br['password'] = self.password br.submit() return br feeds = [ ('Diario de hoy', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/edicion-impresa'), ('Espectaculos', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/cultura-y-espectaculos/notas'), ('Radar', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/radar/notas'), ('Radar libros', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/radar-libros/notas'), ('Cash', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/cash/notas'), ('NO', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/no/notas'), ('Las 12', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/las12/notas'), ('Soy', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/soy/notas'), ('M2', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/m2/notas'), ('Rosario 12', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/rosario12/notas'), ] def get_cover_url(self): lurl = strftime('https://www.pagina12.com.ar/edicion-impresa/%d-%m-%Y') soup = self.index_to_soup(lurl) mydiv = soup.find('div', {'class': lambda x: x and 'printed-edition-cover' in x.split()}) if mydiv: for image in mydiv.findAll('img'): if image['src'].startswith('https://images.pagina12.com.ar/styles/width700/public/'): return image['src'] return None def get_obfuscated_article(self, url): result = None count = 0 while count < self.fetch_retries: try: response = self.browser.open(url, timeout=self.timeout) html = response.read() count = self.fetch_retries tfile = PersistentTemporaryFile('_fa.html') tfile.write(html) tfile.close() self.temp_files.append(tfile) result = tfile.name except Exception: print('Retrying download...') count += 1 return result def preprocess_raw_html(self, raw_html, url): data = find_global_content(raw_html) if not data or not data.get('content_elements'): # Not an article page, or structure changed: let Calibre try the DOM. return raw_html parts = [] headline = (data.get('headlines') or {}).get('basic') if headline: parts.append('

    %s

    ' % headline) subhead = (data.get('subheadlines') or {}).get('basic') or (data.get('description') or {}).get('basic') if subhead: parts.append('

    %s

    ' % subhead) credits = (data.get('credits') or {}).get('by') or [] names = [c.get('name') for c in credits if isinstance(c, dict) and c.get('name')] if names: parts.append('

    %s

    ' % ', '.join(names)) date = data.get('display_date') or data.get('publish_date') if date: parts.append('

    %s

    ' % date) # lead image (promo_items.basic) promo = (data.get('promo_items') or {}).get('basic') or {} if promo.get('type') == 'image' and promo.get('url'): parts.append(ans_element_to_html(promo)) # body for el in data['content_elements']: html = ans_element_to_html(el) if html: parts.append(html) title = headline or self.title return '%s%s' % (title, '\n'.join(parts))