| 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 |
from urllib.parse import urlparse |
| 11 |
|
| 12 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 13 |
|
| 14 |
|
| 15 |
class TeleramaPremium(BasicNewsRecipe): |
| 16 |
title = 'Télérama' |
| 17 |
__author__ = 'Kabonix' |
| 18 |
description = 'Édition complète (API Bypass) - Cover HD & Lecture Pure' |
| 19 |
publisher = 'Télérama' |
| 20 |
language = 'fr' |
| 21 |
encoding = 'utf-8' |
| 22 |
|
| 23 |
oldest_article = 7 |
| 24 |
max_articles_per_feed = 50 |
| 25 |
no_stylesheets = True |
| 26 |
ignore_duplicate_articles = {'title', 'url'} |
| 27 |
|
| 28 |
# On laisse les images des articles tranquilles |
| 29 |
scale_news_images = None |
| 30 |
|
| 31 |
# --- API --- |
| 32 |
headers = {'User-Agent': 'Telerama/4.3.5 (Android; 14)', 'X-Lmd-Token': 'TWPLMOLMO', 'Accept': 'application/json'} |
| 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) --- |
| 41 |
def get_cover_url(self): |
| 42 |
cover_url = None |
| 43 |
try: |
| 44 |
self.log('🔍 Recherche de la dernière couverture...') |
| 45 |
# On va sur la page kiosque |
| 46 |
soup = self.index_to_soup('https://www.telerama.fr/kiosque/telerama') |
| 47 |
|
| 48 |
# On cherche le premier élément "popin-link" avec une data-cover-url |
| 49 |
# Le premier de la liste est toujours le dernier numéro paru |
| 50 |
link = soup.find('a', attrs={'class': 'popin-link', 'data-cover-url': True}) |
| 51 |
|
| 52 |
if link: |
| 53 |
url = link['data-cover-url'] |
| 54 |
# L'URL contient /180/ (basse déf). On passe en HD /1200/ |
| 55 |
# Ex: .../0/0/180/0/... -> .../0/0/1200/0/... |
| 56 |
cover_url = url.replace('/180/', '/1200/') |
| 57 |
self.log(f'✅ Couverture trouvée : {cover_url}') |
| 58 |
else: |
| 59 |
self.log('⚠️ Aucune couverture trouvée dans le kiosque.') |
| 60 |
except Exception as e: |
| 61 |
self.log(f'❌ Erreur récupération couverture : {e}') |
| 62 |
|
| 63 |
return cover_url |
| 64 |
|
| 65 |
# --- BYPASS API --- |
| 66 |
def get_article_url(self, article): |
| 67 |
url = article.get('link', article.get('url', '')) |
| 68 |
path = urlparse(url).path |
| 69 |
return 'https://apps.telerama.fr/tlr/v1/premium-android-phone/element?id={}'.format(path) |
| 70 |
|
| 71 |
# --- JSON TO HTML --- |
| 72 |
def preprocess_raw_html(self, raw_html, url): |
| 73 |
if '/tlr/v1/' in url: |
| 74 |
try: |
| 75 |
data = json.loads(raw_html) |
| 76 |
content = '' |
| 77 |
title = 'Télérama' |
| 78 |
|
| 79 |
if 'templates' in data and 'raw_content' in data['templates']: |
| 80 |
content = data['templates']['raw_content']['content'] |
| 81 |
elif 'body' in data: |
| 82 |
content = data['body'] |
| 83 |
|
| 84 |
if 'template_vars' in data: |
| 85 |
title = data['template_vars'].get('share_title', 'Article Télérama') |
| 86 |
|
| 87 |
if not content: |
| 88 |
return '<html><body><h2>Contenu vide</h2></body></html>' |
| 89 |
|
| 90 |
# Nettoyage préventif |
| 91 |
content = content.replace('{{{ scripts_bottom }}}', '') |
| 92 |
content = re.sub(r'>\s*[•·]\s*<', '><', content) |
| 93 |
|
| 94 |
# Fix images |
| 95 |
content = content.replace('{{width}}', '1200').replace('{{height}}', '') |
| 96 |
content = content.replace('%7B%7Bwidth%7D%7D', '1200') |
| 97 |
|
| 98 |
html = f'<html><head><title>{title}</title></head><body><h1 id="main-title">{title}</h1>{content}</body></html>' |
| 99 |
return html |
| 100 |
|
| 101 |
except Exception as e: |
| 102 |
self.log(f'Erreur JSON : {e}') |
| 103 |
return raw_html |
| 104 |
return raw_html |
| 105 |
|
| 106 |
# --- NETTOYAGE --- |
| 107 |
def preprocess_html(self, soup): |
| 108 |
# 1. Suppression doublons structurels |
| 109 |
for header in soup.find_all(attrs={'class': re.compile(r'article__page-header|header__article', re.I)}): |
| 110 |
header.decompose() |
| 111 |
for ns in soup.find_all('noscript'): |
| 112 |
ns.decompose() |
| 113 |
|
| 114 |
# 2. Suppression "À lire aussi" |
| 115 |
for p in soup.find_all(['p', 'h3', 'h4', 'div', 'aside']): |
| 116 |
text = p.get_text().strip() |
| 117 |
if re.search(r'^(À|A) lire aussi', text, re.IGNORECASE): |
| 118 |
p.decompose() |
| 119 |
|
| 120 |
# 3. Nettoyage Méta TV et Puces |
| 121 |
for tag in soup.find_all(['p', 'div', 'span', 'li', 'ul']): |
| 122 |
text = tag.get_text().strip() |
| 123 |
normalized_text = re.sub(r'\s+', ' ', text) |
| 124 |
|
| 125 |
# Puces seules |
| 126 |
if re.match(r'^[\s\n\r•·|\-.]+$', text): |
| 127 |
tag.decompose() |
| 128 |
continue |
| 129 |
# Mots clés TV seuls |
| 130 |
if re.match(r'^(Direct|Inédit|Replay|\s)+$', normalized_text, re.IGNORECASE): |
| 131 |
tag.decompose() |
| 132 |
continue |
| 133 |
# Mots clés TV avec séparateurs |
| 134 |
if re.search(r'(Direct|Inédit|Replay)\s*[•·-]', text, re.IGNORECASE): |
| 135 |
tag.decompose() |
| 136 |
|
| 137 |
# 4. SUPPRESSION DES LIENS (Lecture Pure) |
| 138 |
for a in soup.find_all('a'): |
| 139 |
a.unwrap() |
| 140 |
|
| 141 |
return soup |
| 142 |
|
| 143 |
keep_only_tags = [ |
| 144 |
dict(name='h1', attrs={'id': 'main-title'}), |
| 145 |
dict(attrs={'class': ['article__page-content', 'article-body']}), |
| 146 |
] |
| 147 |
|
| 148 |
remove_tags = [ |
| 149 |
dict(attrs={'class': re.compile(r'paywall|premium-banner|banner|pubstack|marketing', re.I)}), |
| 150 |
dict(attrs={'class': re.compile(r'sharing|social|bookmark|button|btn|openapp|listBtns', re.I)}), |
| 151 |
dict(attrs={'class': re.compile(r'OUTBRAIN|forecast|overlay', re.I)}), |
| 152 |
dict(name=['script', 'style', 'nav', 'footer', 'button', 'iframe']), |
| 153 |
] |
| 154 |
|
| 155 |
extra_css = ''' |
| 156 |
h1 { |
| 157 |
font-family: "Georgia", serif; |
| 158 |
font-size: 1.5em; |
| 159 |
font-weight: bold; |
| 160 |
text-align: center; |
| 161 |
margin-bottom: 0.5em; |
| 162 |
color: #111; |
| 163 |
} |
| 164 |
.article__label-subscriber { |
| 165 |
display: block; background-color: #ffe600; color: #000; font-weight: bold; |
| 166 |
font-size: 0.8em; text-transform: uppercase; padding: 4px 8px; |
| 167 |
margin: 0 auto 1em auto; width: fit-content; border-radius: 4px; |
| 168 |
} |
| 169 |
.article__chapeau { font-weight: bold; font-style: italic; margin: 1.5em 0; font-size: 1.1em; color: #444; } |
| 170 |
p { text-align: justify; line-height: 1.5; margin-bottom: 1em; } |
| 171 |
figure { margin: 1.5em 0; } |
| 172 |
img { display: block; margin: 0 auto; max-width: 100%; height: auto; } |
| 173 |
figcaption, .media__caption, .media__legend { font-size: 0.75em; color: #666; text-align: center; font-style: italic; margin-top: 0.5em; } |
| 174 |
.author { font-weight: bold; margin-top: 2em; border-top: 1px solid #eee; padding-top: 1em; color: #333; } |
| 175 |
a { color: inherit; text-decoration: none; pointer-events: none; } |
| 176 |
''' |
| 177 |
|
| 178 |
feeds = [ |
| 179 |
('À la une', 'https://www.telerama.fr/rss/une.xml'), |
| 180 |
('Cinéma', 'https://www.telerama.fr/rss/cinema.xml'), |
| 181 |
('Séries', 'https://www.telerama.fr/rss/series.xml'), |
| 182 |
('Télévision', 'https://www.telerama.fr/rss/television.xml'), |
| 183 |
('Musique', 'https://www.telerama.fr/rss/musique.xml'), |
| 184 |
('Livres', 'https://www.telerama.fr/rss/livres.xml'), |
| 185 |
] |