| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
from __future__ import absolute_import, division, print_function, unicode_literals |
| 4 |
|
| 5 |
import json |
| 6 |
import re |
| 7 |
from datetime import date |
| 8 |
|
| 9 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 10 |
|
| 11 |
|
| 12 |
class LeMondePremium(BasicNewsRecipe): |
| 13 |
title = 'Le Monde' |
| 14 |
__author__ = 'veezh, Martin Villard, Kabonix' |
| 15 |
description = 'Édition complète sans pub ni bloc "Lire aussi" (Bypass Mobile API + Fix Images)' |
| 16 |
publisher = 'Société Editrice du Monde' |
| 17 |
publication_type = 'newspaper' |
| 18 |
language = 'fr' |
| 19 |
encoding = 'utf-8' |
| 20 |
|
| 21 |
oldest_article = 1 |
| 22 |
no_stylesheets = True |
| 23 |
ignore_duplicate_articles = {'title', 'url'} |
| 24 |
reverse_article_order = True |
| 25 |
remove_empty_feeds = True |
| 26 |
|
| 27 |
# Autoriser Calibre à télécharger les images |
| 28 |
auto_cleanup = False |
| 29 |
delay = 1 |
| 30 |
|
| 31 |
# --- LOGIQUE DE DÉVERROUILLAGE --- |
| 32 |
browser_user_agent = 'LeMonde/9.20.1 (Android; 14)' |
| 33 |
|
| 34 |
def get_browser(self, *args, **kwargs): |
| 35 |
br = BasicNewsRecipe.get_browser(self, *args, **kwargs) |
| 36 |
br.addheaders = [('User-Agent', self.browser_user_agent), ('X-Lmd-Token', 'TWPLMOLMO'), ('Accept', 'application/json')] |
| 37 |
return br |
| 38 |
|
| 39 |
def get_article_url(self, article): |
| 40 |
url = BasicNewsRecipe.get_article_url(self, article) |
| 41 |
# On transforme l'URL web en URL API mobile |
| 42 |
match = re.search(r'_(\d+)_\d+\.html', url) |
| 43 |
if match: |
| 44 |
article_id = match.group(1) |
| 45 |
return f'https://apps.lemonde.fr/aec/v1/premium-android-phone/article/{article_id}' |
| 46 |
return url |
| 47 |
|
| 48 |
def preprocess_raw_html(self, raw_html, url): |
| 49 |
# L'API renvoie du JSON, on extrait le HTML brut contenu dedans |
| 50 |
if '/aec/v1/' in url: |
| 51 |
try: |
| 52 |
data = json.loads(raw_html) |
| 53 |
content = data['template_vars']['content'] |
| 54 |
title = data['template_vars'].get('seo_title', 'Le Monde') |
| 55 |
|
| 56 |
# 1. FIX URL GENERATION : On remplace les placeholders {{width}} par une valeur fixe |
| 57 |
# Cela active les URLs d'images qui sont souvent sous la forme template |
| 58 |
content = content.replace('%7B%7Bwidth%7D%7D', '1000').replace('{{width}}', '1000') |
| 59 |
content = content.replace('%7B%7Bheight%7D%7D', '600').replace('{{height}}', '600') |
| 60 |
|
| 61 |
return f'<html><head><title>{title}</title></head><body>{content}</body></html>' |
| 62 |
except Exception: |
| 63 |
return raw_html |
| 64 |
return raw_html |
| 65 |
|
| 66 |
# --- COUVERTURE --- |
| 67 |
def get_cover_url(self): |
| 68 |
cover_id = date.today().strftime('%Y%m%d') |
| 69 |
return 'https://www.lemonde.fr/thumbnail/journal/' + cover_id + '/1000/1490' |
| 70 |
|
| 71 |
# --- NETTOYAGE --- |
| 72 |
keep_only_tags = [ |
| 73 |
dict(name='h1', attrs={'class': ['heading', 'article__title']}), |
| 74 |
dict(name='div', attrs={'class': ['kicker', 'article__desc']}), |
| 75 |
# On garde le conteneur principal et les figures (images) |
| 76 |
dict(name='div', attrs={'class': ['article_content', 'article__content']}), |
| 77 |
dict(name='figure'), |
| 78 |
] |
| 79 |
|
| 80 |
remove_tags = [ |
| 81 |
dict( |
| 82 |
name='div', |
| 83 |
attrs={ |
| 84 |
'class': [ |
| 85 |
'see-also-container', |
| 86 |
'inread-container', |
| 87 |
'premium-container', |
| 88 |
'restricted-reading', |
| 89 |
'offer-container', |
| 90 |
'authors-container', |
| 91 |
'js-init-line-clamp', |
| 92 |
'bloc-reactions', |
| 93 |
'meta__publisher', |
| 94 |
] |
| 95 |
}, |
| 96 |
), |
| 97 |
dict(name=['aside', 'footer', 'button', 'svg', 'script', 'style', 'video']), |
| 98 |
] |
| 99 |
|
| 100 |
extra_css = ''' |
| 101 |
h1 { font-size: 1.6em; font-weight: bold; font-family: serif; mb: 0.5em; } |
| 102 |
.kicker { font-size: 1.1em; font-style: italic; color: #444; margin-bottom: 1.5em; } |
| 103 |
p { margin-bottom: 1em; text-align: justify; line-height: 1.4; } |
| 104 |
figure { margin: 1em 0; padding: 0; text-align: center; } |
| 105 |
img { display: block; margin: 0 auto; max-width: 100%; height: auto; } |
| 106 |
figcaption, .caption { font-size: 0.8em; color: #666; font-family: sans-serif; margin-top: 0.5em; } |
| 107 |
''' |
| 108 |
|
| 109 |
def preprocess_html(self, soup): |
| 110 |
# --- FIX DES BLOCS VIDES (IMAGES) --- |
| 111 |
for img in soup.find_all('img'): |
| 112 |
# 1. Gestion du Lazy Loading : Si 'data-src' existe, c'est la vraie image |
| 113 |
if img.has_attr('data-src'): |
| 114 |
img['src'] = img['data-src'] |
| 115 |
|
| 116 |
# 2. Gestion des srcsets : On essaie de récupérer la meilleure qualité dispo |
| 117 |
if img.has_attr('srcset'): |
| 118 |
try: |
| 119 |
# On prend le dernier élément de la liste (souvent le plus large) |
| 120 |
candidates = img['srcset'].split(',') |
| 121 |
url = candidates[-1].strip().split(' ')[0] |
| 122 |
if url.startswith('http'): |
| 123 |
img['src'] = url |
| 124 |
except Exception: |
| 125 |
pass |
| 126 |
|
| 127 |
# Nettoyage pour éviter les conflits |
| 128 |
for attr in ['srcset', 'data-srcset', 'data-src', 'loading']: |
| 129 |
if img.has_attr(attr): |
| 130 |
del img[attr] |
| 131 |
|
| 132 |
return soup |
| 133 |
|
| 134 |
feeds = [ |
| 135 |
('À la une', 'https://www.lemonde.fr/rss/une.xml'), |
| 136 |
('Économie', 'https://www.lemonde.fr/economie/rss_full.xml'), |
| 137 |
('International', 'https://www.lemonde.fr/international/rss_full.xml'), |
| 138 |
('Planète', 'https://www.lemonde.fr/planete/rss_full.xml'), |
| 139 |
('M le Mag', 'https://www.lemonde.fr/m-le-mag/rss_full.xml'), |
| 140 |
] |