| 1 |
__license__ = 'GPL v3' |
| 2 |
__copyright__ = '2026, Rodrigo Pazos' |
| 3 |
|
| 4 |
from calibre.ptempfile import PersistentTemporaryFile |
| 5 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 6 |
|
| 7 |
|
| 8 |
class Cenital(BasicNewsRecipe): |
| 9 |
title = 'Cenital' |
| 10 |
__author__ = 'Rodrigo Pazos' |
| 11 |
description = ( |
| 12 |
'Revista semanal con el último número de cada newsletter semanal de Cenital: ' |
| 13 |
'análisis político, económico, internacional y cultural argentino. ' |
| 14 |
'Incluye: Mundo propio, Off the record, Rollover, Receta para el desastre, ' |
| 15 |
'Una calle me separa, El hilo conductor, Sistema 2 y Kohan.' |
| 16 |
) |
| 17 |
publisher = 'Cenital' |
| 18 |
language = 'es_AR' |
| 19 |
category = 'politics, economics, Argentina, news' |
| 20 |
publication_type = 'magazine' |
| 21 |
|
| 22 |
oldest_article = 30 |
| 23 |
max_articles_per_feed = 1 |
| 24 |
timefmt = ' [%a, %d %b %Y]' |
| 25 |
|
| 26 |
# Use full content from RSS <content:encoded> — avoids fetching each article individually |
| 27 |
use_embedded_content = True |
| 28 |
|
| 29 |
no_stylesheets = True |
| 30 |
remove_javascript = True |
| 31 |
auto_cleanup = True |
| 32 |
compress_news_images = True |
| 33 |
|
| 34 |
masthead_url = 'https://cenital.com/wp-content/uploads/2020/04/cenital-logo.png' |
| 35 |
|
| 36 |
# 8 weekly newsletters (Mon–Fri dailies "Primera mañana" and "Antes de mañana" excluded) |
| 37 |
feeds = [ |
| 38 |
('Mundo propio', 'https://cenital.com/secciones/newsletters/mundo-propio/feed/'), |
| 39 |
('Off the record', 'https://cenital.com/secciones/newsletters/off-the-record/feed/'), |
| 40 |
('Rollover', 'https://cenital.com/secciones/newsletters/rollover/feed/'), |
| 41 |
('Receta para el desastre', 'https://cenital.com/secciones/newsletters/receta-para-el-desastre/feed/'), |
| 42 |
('Una calle me separa', 'https://cenital.com/secciones/newsletters/una-calle-me-separa/feed/'), |
| 43 |
('El hilo conductor', 'https://cenital.com/secciones/newsletters/el-hilo-conductor/feed/'), |
| 44 |
('Sistema 2', 'https://cenital.com/secciones/newsletters/sistema-dos/feed/'), |
| 45 |
('Kohan', 'https://cenital.com/secciones/newsletters/kohan/feed/'), |
| 46 |
] |
| 47 |
|
| 48 |
remove_tags = [ |
| 49 |
dict(name=['script', 'style', 'iframe', 'form']), |
| 50 |
dict( |
| 51 |
attrs={ |
| 52 |
'class': lambda x: ( |
| 53 |
x |
| 54 |
and any( |
| 55 |
c in x |
| 56 |
for c in [ |
| 57 |
'wp-block-subscribe', |
| 58 |
'wp-block-buttons', |
| 59 |
'newsletter', |
| 60 |
'subscription', |
| 61 |
'suscripcion', |
| 62 |
] |
| 63 |
) |
| 64 |
) |
| 65 |
} |
| 66 |
), |
| 67 |
] |
| 68 |
|
| 69 |
remove_attributes = ['style', 'onclick', 'data-src'] |
| 70 |
|
| 71 |
extra_css = ''' |
| 72 |
h1, h2 { font-size: 1.4em; margin-bottom: 0.3em; } |
| 73 |
p { line-height: 1.5; margin-bottom: 0.8em; } |
| 74 |
img { max-width: 100%; height: auto; } |
| 75 |
''' |
| 76 |
|
| 77 |
def get_cover_url(self): |
| 78 |
""" |
| 79 |
Builds a magazine-style cover: 2x4 grid of the featured image from each |
| 80 |
newsletter's latest article, with a dark overlay and the Cenital logo centred. |
| 81 |
Falls back to the static wide image if PIL is unavailable. |
| 82 |
""" |
| 83 |
from io import BytesIO |
| 84 |
from urllib.request import Request, urlopen |
| 85 |
from xml.etree import ElementTree as ET |
| 86 |
|
| 87 |
try: |
| 88 |
from PIL import Image |
| 89 |
except ImportError: |
| 90 |
return 'https://cenital.com/wp-content/uploads/2021/12/cenital_wide.jpg' |
| 91 |
|
| 92 |
MEDIA_NS = '{http://search.yahoo.com/mrss/}' |
| 93 |
HEADERS = {'User-Agent': 'Mozilla/5.0'} |
| 94 |
W, H = 800, 1200 |
| 95 |
COLS = 2 |
| 96 |
ROWS = len(self.feeds) // COLS # 4 rows for 8 feeds |
| 97 |
CELL_W = W // COLS |
| 98 |
CELL_H = H // ROWS |
| 99 |
|
| 100 |
cover = Image.new('RGB', (W, H), (15, 15, 15)) |
| 101 |
col, row = 0, 0 |
| 102 |
|
| 103 |
for _name, feed_url in self.feeds: |
| 104 |
if row >= ROWS: |
| 105 |
break |
| 106 |
try: |
| 107 |
root = ET.fromstring(urlopen(Request(feed_url, headers=HEADERS), timeout=15).read()) |
| 108 |
channel = root.find('channel') |
| 109 |
item = channel.find('item') if channel is not None else None |
| 110 |
if item is None: |
| 111 |
raise ValueError('empty feed') |
| 112 |
|
| 113 |
media = item.find(MEDIA_NS + 'content') |
| 114 |
img_url = media.get('url') if media is not None else None |
| 115 |
if not img_url: |
| 116 |
raise ValueError('no media:content') |
| 117 |
|
| 118 |
tile = Image.open(BytesIO(urlopen(Request(img_url, headers=HEADERS), timeout=15).read())).convert('RGB').resize((CELL_W, CELL_H), Image.LANCZOS) |
| 119 |
cover.paste(tile, (col * CELL_W, row * CELL_H)) |
| 120 |
|
| 121 |
except Exception: |
| 122 |
pass # leave that cell dark — don't abort the whole cover |
| 123 |
|
| 124 |
col += 1 |
| 125 |
if col >= COLS: |
| 126 |
col = 0 |
| 127 |
row += 1 |
| 128 |
|
| 129 |
# Semi-transparent dark overlay so the white logo is always readable |
| 130 |
overlay = Image.new('RGBA', (W, H), (0, 0, 0, 150)) |
| 131 |
cover = Image.alpha_composite(cover.convert('RGBA'), overlay) |
| 132 |
|
| 133 |
# Cenital logo centred |
| 134 |
try: |
| 135 |
logo = Image.open( |
| 136 |
BytesIO(urlopen(Request('https://cenital.com/wp-content/uploads/2020/04/cenital-logo.png', headers=HEADERS), timeout=15).read()) |
| 137 |
).convert('RGBA') |
| 138 |
logo_w = int(W * 0.65) |
| 139 |
logo_h = int(logo.height * logo_w / logo.width) |
| 140 |
logo = logo.resize((logo_w, logo_h), Image.LANCZOS) |
| 141 |
cover.paste(logo, ((W - logo_w) // 2, (H - logo_h) // 2), logo) |
| 142 |
except Exception: |
| 143 |
pass |
| 144 |
|
| 145 |
with PersistentTemporaryFile(suffix='.jpg') as tmp: |
| 146 |
pass |
| 147 |
cover.convert('RGB').save(tmp.name, 'JPEG', quality=80) |
| 148 |
return tmp.name |