| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
from __future__ import absolute_import, division, print_function, unicode_literals |
| 4 |
|
| 5 |
__license__ = 'GPL v3' |
| 6 |
__copyright__ = '2026, Kabonix' |
| 7 |
|
| 8 |
import json |
| 9 |
import re |
| 10 |
|
| 11 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 12 |
|
| 13 |
|
| 14 |
class LeParisienPremium(BasicNewsRecipe): |
| 15 |
title = 'Le Parisien' |
| 16 |
__author__ = 'Kabonix' |
| 17 |
description = 'Édition complète via API Gateway - Cover Dynamique Kiosque' |
| 18 |
publisher = 'Le Parisien' |
| 19 |
language = 'fr' |
| 20 |
encoding = 'utf-8' |
| 21 |
|
| 22 |
oldest_article = 2 |
| 23 |
max_articles_per_feed = 50 |
| 24 |
no_stylesheets = True |
| 25 |
ignore_duplicate_articles = {'title', 'url'} |
| 26 |
scale_news_images = None |
| 27 |
|
| 28 |
# --- CONFIGURATION IDENTITÉ APK --- |
| 29 |
headers = { |
| 30 |
'User-Agent': 'LeParisien/11.0.1 (Android 14)', |
| 31 |
'Accept': 'application/json', |
| 32 |
} |
| 33 |
|
| 34 |
def get_browser(self, *args, **kwargs): |
| 35 |
br = BasicNewsRecipe.get_browser(self, *args, **kwargs) |
| 36 |
for name, val in self.headers.items(): |
| 37 |
br.addheaders.append((name, val)) |
| 38 |
return br |
| 39 |
|
| 40 |
# --- COUVERTURE DYNAMIQUE (Ta demande Kiosque) --- |
| 41 |
def get_cover_url(self): |
| 42 |
cover_url = None |
| 43 |
try: |
| 44 |
self.log('🔍 Recherche de la Une du jour sur le kiosque...') |
| 45 |
soup = self.index_to_soup('https://www.kiosque.leparisien.fr/') |
| 46 |
|
| 47 |
# On cherche l'image avec la classe 'main-image' |
| 48 |
img = soup.find('img', class_=re.compile(r'main-image', re.I)) |
| 49 |
|
| 50 |
if img and img.has_attr('src'): |
| 51 |
url = img['src'] |
| 52 |
# On ajoute le protocole si manquant (// -> https://) |
| 53 |
if url.startswith('//'): |
| 54 |
url = 'https:' + url |
| 55 |
|
| 56 |
# OPTIMISATION : On tente de passer de MEDIUM à LARGE pour la HD |
| 57 |
# On remplace 'MEDIUM' par 'LARGE' dans l'URL Twipe |
| 58 |
cover_url = url.replace('-MEDIUM-', '-LARGE-') |
| 59 |
self.log(f'✅ Une trouvée : {cover_url}') |
| 60 |
else: |
| 61 |
self.log('⚠️ Image de Une non trouvée sur la page.') |
| 62 |
except Exception as e: |
| 63 |
self.log(f'❌ Erreur cover : {e}') |
| 64 |
|
| 65 |
return cover_url |
| 66 |
|
| 67 |
# --- BYPASS API --- |
| 68 |
def get_article_url(self, article): |
| 69 |
url = article.get('link', article.get('url', '')) |
| 70 |
# Extraction de l'ID Arc Publishing (ex: 5KHO7QK6GVHD5HNF5B735ENVJI) |
| 71 |
match = re.search(r'-([A-Z0-9]{20,})\.php$', url) |
| 72 |
if match: |
| 73 |
article_id = match.group(1) |
| 74 |
return f'https://gateway-api.leparisien.fr/v1/contents/articles/{article_id}' |
| 75 |
return url |
| 76 |
|
| 77 |
# --- EXTRACTION JSON --- |
| 78 |
def preprocess_raw_html(self, raw_html, url): |
| 79 |
if '/v1/contents/articles/' in url: |
| 80 |
try: |
| 81 |
data = json.loads(raw_html) |
| 82 |
story = data.get('story', {}) |
| 83 |
|
| 84 |
title = story.get('headlines', {}).get('basic', 'Le Parisien') |
| 85 |
lead = story.get('subheadlines', {}).get('basic', '') |
| 86 |
main_img = story.get('promo_items', {}).get('basic', {}).get('url', '') |
| 87 |
content = story.get('bodyContent', '') |
| 88 |
|
| 89 |
html = f'<html><head><title>{title}</title></head><body>' |
| 90 |
html += f'<h1 id="main-title" style="text-align:center">{title}</h1>' |
| 91 |
if lead: |
| 92 |
html += f'<p style="font-weight:bold; font-style:italic">{lead}</p>' |
| 93 |
if main_img: |
| 94 |
html += f'<div style="text-align:center"><img src="{main_img}"></div>' |
| 95 |
|
| 96 |
html += content |
| 97 |
html += '</body></html>' |
| 98 |
return html |
| 99 |
except Exception as e: |
| 100 |
self.log(f'Erreur JSON : {e}') |
| 101 |
return raw_html |
| 102 |
return raw_html |
| 103 |
|
| 104 |
# --- NETTOYAGE --- |
| 105 |
def preprocess_html(self, soup): |
| 106 |
# 1. Virer les titres en double (on garde notre h1 id="main-title") |
| 107 |
for h1 in soup.find_all('h1'): |
| 108 |
if h1.get('id') != 'main-title': |
| 109 |
h1.decompose() |
| 110 |
|
| 111 |
# 2. Virer les parasites (À lire aussi, Vidéos, etc.) |
| 112 |
for tag in soup.find_all(['p', 'div', 'span', 'b']): |
| 113 |
text = tag.get_text().strip() |
| 114 |
if re.match(r'^(À lire aussi|Vidéo|PODCAST\.|Direct|Inédit|Replay)', text, re.IGNORECASE): |
| 115 |
tag.decompose() |
| 116 |
# Virer les puces isolées |
| 117 |
if text in ['•', '·']: |
| 118 |
tag.decompose() |
| 119 |
|
| 120 |
# 3. Supprimer tous les liens (Unwrap) |
| 121 |
for a in soup.find_all('a'): |
| 122 |
a.unwrap() |
| 123 |
|
| 124 |
# 4. Virer iframes et scripts résiduels |
| 125 |
for tag in soup.find_all(['iframe', 'script', 'noscript']): |
| 126 |
tag.decompose() |
| 127 |
|
| 128 |
return soup |
| 129 |
|
| 130 |
# --- FEEDS --- |
| 131 |
feeds = [ |
| 132 |
('À la une', 'https://feeds.leparisien.fr/leparisien/rss'), |
| 133 |
('Politique', 'https://feeds.leparisien.fr/leparisien/rss/politique'), |
| 134 |
('Société', 'https://feeds.leparisien.fr/leparisien/rss/societe'), |
| 135 |
('International', 'https://feeds.leparisien.fr/leparisien/rss/international'), |
| 136 |
('Économie', 'https://feeds.leparisien.fr/leparisien/rss/economie'), |
| 137 |
('Faits divers', 'https://feeds.leparisien.fr/leparisien/rss/faits-divers'), |
| 138 |
] |
| 139 |
|
| 140 |
extra_css = ''' |
| 141 |
h1 { font-family: "Georgia", serif; font-size: 1.4em; margin-bottom: 20px; color: #111; } |
| 142 |
p.paragraph { text-align: justify; line-height: 1.5; margin-bottom: 15px; } |
| 143 |
img { display: block; margin: 10px auto; max-width: 100%; height: auto; } |
| 144 |
''' |