| 1 |
# -*- mode: python -*- |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
__license__ = 'GPL v3' |
| 5 |
__copyright__ = '2008-2026, Darko Miletic <darko.miletic at gmail.com>' |
| 6 |
''' |
| 7 |
pagina12.com.ar |
| 8 |
''' |
| 9 |
import json |
| 10 |
import re |
| 11 |
|
| 12 |
from calibre import strftime |
| 13 |
from calibre.ptempfile import PersistentTemporaryFile |
| 14 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 15 |
|
| 16 |
|
| 17 |
def find_global_content(raw_html): |
| 18 |
"""Return the parsed Fusion.globalContent dict, or None if not present.""" |
| 19 |
# `\s*=\s*` deliberately fails to match `Fusion.globalContentConfig=`, |
| 20 |
# because the char after "globalContent" there is 'C', not '='. |
| 21 |
m = re.search(r'Fusion\.globalContent\s*=\s*', raw_html) |
| 22 |
if m is None: |
| 23 |
return None |
| 24 |
try: |
| 25 |
# raw_decode parses exactly ONE JSON value starting at m.end() and |
| 26 |
# returns where it stopped, ignoring the ; and following assignments. |
| 27 |
# This is robust to '{' / '}' appearing inside the article text. |
| 28 |
obj, _end = json.JSONDecoder().raw_decode(raw_html, m.end()) |
| 29 |
except ValueError: |
| 30 |
return None |
| 31 |
return obj if isinstance(obj, dict) else None |
| 32 |
|
| 33 |
|
| 34 |
def ans_element_to_html(el): |
| 35 |
"""Render a single ANS content_element to an HTML string.""" |
| 36 |
if not isinstance(el, dict): |
| 37 |
return '' |
| 38 |
t = el.get('type') |
| 39 |
|
| 40 |
if t == 'text': |
| 41 |
c = el.get('content', '') |
| 42 |
return '<p>%s</p>' % c if c.strip() else '' |
| 43 |
|
| 44 |
if t == 'raw_html': |
| 45 |
# embeds (tweets, iframes, ads...). Keep or drop as you prefer. |
| 46 |
return el.get('content', '') |
| 47 |
|
| 48 |
if t == 'header': |
| 49 |
lvl = int(el.get('level', 2) or 2) |
| 50 |
lvl = min(max(lvl, 1), 6) |
| 51 |
return '<h%d>%s</h%d>' % (lvl, el.get('content', ''), lvl) |
| 52 |
|
| 53 |
if t == 'image': |
| 54 |
url = el.get('url', '') |
| 55 |
if not url: |
| 56 |
return '' |
| 57 |
cap = el.get('caption') or '' |
| 58 |
out = '<figure><img src="%s"/>' % url |
| 59 |
if cap: |
| 60 |
out += '<figcaption>%s</figcaption>' % cap |
| 61 |
return out + '</figure>' |
| 62 |
|
| 63 |
if t == 'list': |
| 64 |
tag = 'ol' if el.get('list_type') == 'ordered' else 'ul' |
| 65 |
items = [] |
| 66 |
for it in el.get('items', []): |
| 67 |
if isinstance(it, dict) and it.get('type') == 'text': |
| 68 |
items.append('<li>%s</li>' % it.get('content', '')) |
| 69 |
elif isinstance(it, dict): |
| 70 |
items.append('<li>%s</li>' % ans_element_to_html(it)) |
| 71 |
else: |
| 72 |
items.append('<li>%s</li>' % it) |
| 73 |
return '<%s>%s</%s>' % (tag, ''.join(items), tag) |
| 74 |
|
| 75 |
if t == 'blockquote': |
| 76 |
return '<blockquote>%s</blockquote>' % el.get('content', '') |
| 77 |
|
| 78 |
if t == 'quote': # pull quote: nested content_elements |
| 79 |
inner = ''.join(ans_element_to_html(e) for e in el.get('content_elements', [])) |
| 80 |
return '<blockquote>%s</blockquote>' % inner |
| 81 |
|
| 82 |
if t == 'oembed_response': |
| 83 |
raw = el.get('raw_oembed') or {} |
| 84 |
return raw.get('html', '') or el.get('html', '') |
| 85 |
|
| 86 |
if t == 'code': |
| 87 |
return '<pre>%s</pre>' % el.get('content', '') |
| 88 |
|
| 89 |
if t == 'divider': |
| 90 |
return '<hr/>' |
| 91 |
|
| 92 |
if t == 'correction': |
| 93 |
return '<p><em>%s</em></p>' % el.get('text', '') |
| 94 |
|
| 95 |
# Types intentionally skipped: custom_embed, gallery, reference, story, |
| 96 |
# video, table, interstitial_link, etc. Add handlers here if you need them. |
| 97 |
return '' |
| 98 |
|
| 99 |
|
| 100 |
class Pagina12(BasicNewsRecipe): |
| 101 |
title = 'Pagina - 12' |
| 102 |
__author__ = 'Darko Miletic' |
| 103 |
description = 'Noticias de Argentina y el resto del mundo' |
| 104 |
publisher = 'La Pagina S.A.' |
| 105 |
category = 'news, politics, Argentina' |
| 106 |
oldest_article = 2 |
| 107 |
no_stylesheets = True |
| 108 |
encoding = 'utf8' |
| 109 |
use_embedded_content = False |
| 110 |
language = 'es_AR' |
| 111 |
remove_empty_feeds = True |
| 112 |
publication_type = 'newspaper' |
| 113 |
auto_cleanup = False |
| 114 |
delay = 1 |
| 115 |
simultaneous_downloads = 1 |
| 116 |
timeout = 8 |
| 117 |
needs_subscription = 'optional' |
| 118 |
ignore_duplicate_articles = {'url'} |
| 119 |
articles_are_obfuscated = True |
| 120 |
temp_files = [] |
| 121 |
fetch_retries = 10 |
| 122 |
extra_css = ''' |
| 123 |
body{font-family: "Open Sans", sans-serif} |
| 124 |
.article-date{font-size: small; margin-bottom: 1em;} |
| 125 |
.article-title{font-size: x-large; font-weight: bold; display: block; margin-bottom: 1em; margin-top: 1em;} |
| 126 |
.article-main-media{display: block; margin-bottom: 1em;} |
| 127 |
.article-summary{margin-top:1em; margin-bottom: 1em; display:block} |
| 128 |
.article-author{font-family: "Archivo Narrow",Helvetica,sans-serif; color: gray; font-size: small; margin-top:1em; margin-bottom: 1em} |
| 129 |
img{margin-top:1em; margin-bottom: 1em; display:block} |
| 130 |
.article-text p:first-letter{display: inline; font-size: xx-large; font-weight: bold} |
| 131 |
.article-prefix{font-family: "Archivo Narrow",Helvetica,sans-serif; font-size: small; text-transform: uppercase;} |
| 132 |
''' |
| 133 |
|
| 134 |
conversion_options = {'comment': description, 'tags': category, 'publisher': publisher, 'language': language} |
| 135 |
|
| 136 |
def get_browser(self): |
| 137 |
br = BasicNewsRecipe.get_browser(self) |
| 138 |
br.open('https://www.pagina12.com.ar/') |
| 139 |
if self.username is not None and self.password is not None: |
| 140 |
br.open('https://auth.pagina12.com.ar/ingresar?redirect=https://www.pagina12.com.ar') |
| 141 |
br.select_form(id='login') |
| 142 |
br['email'] = self.username |
| 143 |
br['password'] = self.password |
| 144 |
br.submit() |
| 145 |
return br |
| 146 |
|
| 147 |
feeds = [ |
| 148 |
('Diario de hoy', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/edicion-impresa'), |
| 149 |
('Espectaculos', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/cultura-y-espectaculos/notas'), |
| 150 |
('Radar', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/radar/notas'), |
| 151 |
('Radar libros', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/radar-libros/notas'), |
| 152 |
('Cash', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/cash/notas'), |
| 153 |
('NO', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/no/notas'), |
| 154 |
('Las 12', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/las12/notas'), |
| 155 |
('Soy', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/soy/notas'), |
| 156 |
('M2', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/m2/notas'), |
| 157 |
('Rosario 12', 'https://www.pagina12.com.ar/arc/outboundfeeds/rss/suplementos/rosario12/notas'), |
| 158 |
] |
| 159 |
|
| 160 |
def get_cover_url(self): |
| 161 |
lurl = strftime('https://www.pagina12.com.ar/edicion-impresa/%d-%m-%Y') |
| 162 |
soup = self.index_to_soup(lurl) |
| 163 |
mydiv = soup.find('div', {'class': lambda x: x and 'printed-edition-cover' in x.split()}) |
| 164 |
if mydiv: |
| 165 |
for image in mydiv.findAll('img'): |
| 166 |
if image['src'].startswith('https://images.pagina12.com.ar/styles/width700/public/'): |
| 167 |
return image['src'] |
| 168 |
return None |
| 169 |
|
| 170 |
def get_obfuscated_article(self, url): |
| 171 |
result = None |
| 172 |
count = 0 |
| 173 |
while count < self.fetch_retries: |
| 174 |
try: |
| 175 |
response = self.browser.open(url, timeout=self.timeout) |
| 176 |
html = response.read() |
| 177 |
count = self.fetch_retries |
| 178 |
tfile = PersistentTemporaryFile('_fa.html') |
| 179 |
tfile.write(html) |
| 180 |
tfile.close() |
| 181 |
self.temp_files.append(tfile) |
| 182 |
result = tfile.name |
| 183 |
except Exception: |
| 184 |
print('Retrying download...') |
| 185 |
count += 1 |
| 186 |
return result |
| 187 |
|
| 188 |
def preprocess_raw_html(self, raw_html, url): |
| 189 |
data = find_global_content(raw_html) |
| 190 |
if not data or not data.get('content_elements'): |
| 191 |
# Not an article page, or structure changed: let Calibre try the DOM. |
| 192 |
return raw_html |
| 193 |
|
| 194 |
parts = [] |
| 195 |
|
| 196 |
headline = (data.get('headlines') or {}).get('basic') |
| 197 |
if headline: |
| 198 |
parts.append('<h1>%s</h1>' % headline) |
| 199 |
|
| 200 |
subhead = (data.get('subheadlines') or {}).get('basic') or (data.get('description') or {}).get('basic') |
| 201 |
if subhead: |
| 202 |
parts.append('<p class="subhead"><strong>%s</strong></p>' % subhead) |
| 203 |
|
| 204 |
credits = (data.get('credits') or {}).get('by') or [] |
| 205 |
names = [c.get('name') for c in credits if isinstance(c, dict) and c.get('name')] |
| 206 |
if names: |
| 207 |
parts.append('<p class="byline">%s</p>' % ', '.join(names)) |
| 208 |
|
| 209 |
date = data.get('display_date') or data.get('publish_date') |
| 210 |
if date: |
| 211 |
parts.append('<p class="date">%s</p>' % date) |
| 212 |
|
| 213 |
# lead image (promo_items.basic) |
| 214 |
promo = (data.get('promo_items') or {}).get('basic') or {} |
| 215 |
if promo.get('type') == 'image' and promo.get('url'): |
| 216 |
parts.append(ans_element_to_html(promo)) |
| 217 |
|
| 218 |
# body |
| 219 |
for el in data['content_elements']: |
| 220 |
html = ans_element_to_html(el) |
| 221 |
if html: |
| 222 |
parts.append(html) |
| 223 |
|
| 224 |
title = headline or self.title |
| 225 |
return '<html><head><meta charset="utf-8"/><title>%s</title></head><body>%s</body></html>' % (title, '\n'.join(parts)) |