Debian Codegraph @

recipes/le_equipe.recipe

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Recette Calibre — L'Équipe Premium
4 # Auteur : Kabonix
5 # Auth : eqn:EQN2014 (Basic ZXFuOkVRTjIwMTQ=) — extrait des DEX natifs de LEquipe v10.58.2
6 # API : https://dwh.lequipe.fr/api/v1/efr/news/{id}
7 # Cover: scraping https://www.lequipe.fr/abonnement/kiosque/ → Twipe (id journal = 1627)
8
9 from __future__ import absolute_import, division, print_function, unicode_literals
10
11 import datetime
12 import json
13 import re
14 import urllib.request
15
16 from calibre.web.feeds.news import BasicNewsRecipe
17
18
19 class LEquipePremium(BasicNewsRecipe):
20 title = "L'Équipe"
21 __author__ = 'Kabonix'
22 description = "Articles abonnés L'Équipe avec notes de matchs complètes"
23 publisher = "L'Équipe"
24 publication_type = 'newspaper'
25 language = 'fr'
26 encoding = 'utf-8'
27
28 oldest_article = 1
29 no_stylesheets = True
30 ignore_duplicate_articles = {'title', 'url'}
31 remove_empty_feeds = True
32 auto_cleanup = False
33 delay = 1
34
35 # ------------------------------------------------------------------ #
36 # Auth — hardcodé dans les DEX natifs de l'APK #
37 # ------------------------------------------------------------------ #
38 browser_user_agent = 'MOBILE-LEQUIPE/android/phone/10.58.2/premium/wifi/test'
39 auth_header = 'Basic ZXFuOkVRTjIwMTQ=' # eqn:EQN2014
40
41 # ------------------------------------------------------------------ #
42 # Couverture dynamique — scraping page kiosque Twipe #
43 # ID fixe du titre "L'Équipe journal" chez Twipe : 1627 #
44 # ------------------------------------------------------------------ #
45 def get_cover_url(self):
46 try:
47 req = urllib.request.Request('https://www.lequipe.fr/abonnement/kiosque/', headers={'User-Agent': 'Mozilla/5.0'})
48 html = urllib.request.urlopen(req, timeout=10).read().decode('utf-8')
49 m = re.search(
50 r'https://eqp-eqp-webreader-production\.twipemobile\.com'
51 r'/data/1627/covers/Preview-MEDIUM-\d+\.jpg[^"&]*',
52 html,
53 )
54 if m:
55 return m.group(0)
56 except Exception:
57 pass
58 # Fallback : miniature journal par date
59 today = datetime.datetime.now().strftime('%Y%m%d')
60 return f'https://www.lequipe.fr/thumbnail/journal/{today}/1000/1490'
61
62 # ------------------------------------------------------------------ #
63 # Browser avec headers d'auth #
64 # ------------------------------------------------------------------ #
65 def get_browser(self, *args, **kwargs):
66 br = BasicNewsRecipe.get_browser(self, *args, **kwargs)
67 br.addheaders = [
68 ('User-Agent', self.browser_user_agent),
69 ('Authorization', self.auth_header),
70 ('Accept', 'application/json'),
71 ]
72 return br
73
74 # ------------------------------------------------------------------ #
75 # Filtre RSS → URL API #
76 # Seuls les /Article/ sont premium ; /Actualites/ = brèves gratuites #
77 # ------------------------------------------------------------------ #
78 def get_article_url(self, article):
79 url = BasicNewsRecipe.get_article_url(self, article)
80 if not url:
81 return None
82 # Supprimer le fragment #at_medium=RSS_feeds
83 url = url.split('#')[0]
84 if '/Article/' not in url:
85 return None
86 m = re.search(r'/(\d+)$', url)
87 if m:
88 return f'https://dwh.lequipe.fr/api/v1/efr/news/{m.group(1)}'
89 return None
90
91 # ------------------------------------------------------------------ #
92 # Bloc notes de match #
93 # data_url → JSON Twipe avec titulaires, remplaçants, notes #
94 # ------------------------------------------------------------------ #
95 def parse_playing_field(self, data_url):
96 try:
97 req = urllib.request.Request(data_url, headers={'User-Agent': self.browser_user_agent})
98 raw = urllib.request.urlopen(req, timeout=10).read().decode('utf-8')
99 d = json.loads(raw)
100 except Exception:
101 return ''
102
103 html = '<hr style="margin:1em 0;"/>'
104 equipes = d.get('equipes', [])
105
106 # --- Score et note globale des équipes ---
107 if len(equipes) == 2:
108 e1, e2 = equipes[0], equipes[1]
109 html += (
110 f'<table width="100%" style="margin-bottom:8px;border-collapse:collapse;">'
111 f'<tr>'
112 f'<td style="background:{e1["couleur"]};color:{e1["couleur_texte"]};'
113 f'padding:6px;text-align:center;width:42%;font-weight:bold;">{e1["nom"]}</td>'
114 f'<td style="text-align:center;padding:6px;width:16%;font-size:1.3em;font-weight:bold;">'
115 f'{e1["score"]} – {e2["score"]}</td>'
116 f'<td style="background:{e2["couleur"]};color:{e2["couleur_texte"]};'
117 f'padding:6px;text-align:center;width:42%;font-weight:bold;">{e2["nom"]}</td>'
118 f'</tr>'
119 f'<tr>'
120 f'<td style="text-align:center;color:{e1["couleur"]};font-weight:bold;">Note : {e1["note"]}</td>'
121 f'<td></td>'
122 f'<td style="text-align:center;color:{e2["couleur"]};font-weight:bold;">Note : {e2["note"]}</td>'
123 f'</tr>'
124 f'</table>'
125 )
126
127 # --- Titulaires et remplaçants par équipe ---
128 banc = d.get('banc', [])
129 for idx, equipe in enumerate(equipes):
130 couleur = equipe['couleur']
131 couleur_texte = equipe['couleur_texte']
132 nom_equipe = equipe['nom']
133
134 titulaires = [j for j in d.get('titulaires', []) if j.get('couleur') == couleur]
135 banc_equipe = banc[idx] if idx < len(banc) else {}
136 remplacants = banc_equipe.get('remplacants', [])
137 entraineur = banc_equipe.get('entraineur', {})
138
139 html += (
140 f'<h3 style="background:{couleur};color:{couleur_texte};'
141 f'padding:4px 8px;margin-top:1em;">{nom_equipe}</h3>'
142 f'<table width="100%" cellspacing="1" style="font-size:0.9em;">'
143 f'<tr style="background:#ddd;">'
144 f'<th style="text-align:left;padding:3px;">Joueur</th>'
145 f'<th style="text-align:center;padding:3px;">Note</th>'
146 f'<th style="padding:3px;"></th>'
147 f'</tr>'
148 )
149
150 for j in titulaires:
151 note = j.get('note', '-')
152 nom = j.get('nom', '')
153 buts = ' ⚽' * len(j.get('buts', []))
154 sortie = f"↓{j['min_sortie']}'" if j.get('min_sortie') else ''
155 html += (
156 f'<tr>'
157 f'<td style="padding:3px;">{nom}{buts}</td>'
158 f'<td style="text-align:center;padding:3px;font-weight:bold;">{note}</td>'
159 f'<td style="color:gray;font-size:0.85em;padding:3px;">{sortie}</td>'
160 f'</tr>'
161 )
162
163 # Remplaçants notés uniquement
164 for j in remplacants:
165 note = j.get('note', '/')
166 if note and note not in ('/', ''):
167 nom = j.get('nom', '')
168 entree = f"↑{j['min_entree']}'" if j.get('min_entree') else ''
169 remplace = j.get('remplace', '')
170 buts = ' ⚽' * len(j.get('buts', []))
171 html += (
172 f'<tr style="color:#555;">'
173 f'<td style="padding:3px;font-style:italic;">'
174 f'{nom}{buts} <span style="font-size:0.8em;">(rempl. {remplace})</span>'
175 f'</td>'
176 f'<td style="text-align:center;padding:3px;">{note}</td>'
177 f'<td style="font-size:0.85em;padding:3px;">{entree}</td>'
178 f'</tr>'
179 )
180
181 # Entraîneur
182 if entraineur:
183 html += (
184 f'<tr style="background:#f0f0f0;">'
185 f'<td style="padding:3px;"><b>Entr. {entraineur.get("nom", "")}</b></td>'
186 f'<td style="text-align:center;padding:3px;font-weight:bold;">'
187 f'{entraineur.get("note", "-")}</td>'
188 f'<td></td>'
189 f'</tr>'
190 )
191
192 html += '</table>'
193
194 # Arbitre
195 arbitre = d.get('arbitre', {})
196 if arbitre:
197 html += f'<p style="color:gray;font-size:0.85em;margin-top:4px;">Arbitre : {arbitre.get("nom", "")} — Note : {arbitre.get("note", "-")}</p>'
198
199 html += '<hr style="margin:1em 0;"/>'
200 return html
201
202 # ------------------------------------------------------------------ #
203 # Conversion JSON API → HTML lisible #
204 # ------------------------------------------------------------------ #
205 def strip_links(self, html):
206 """Supprime les balises <a> en gardant le texte"""
207 return re.sub(r'<a[^>]*>(.*?)</a>', r'\1', html, flags=re.DOTALL)
208
209 def preprocess_raw_html(self, raw_html, url):
210 if '/api/v1/efr/news/' not in url:
211 return raw_html
212
213 try:
214 data = json.loads(raw_html)
215 except Exception:
216 return '<html><body><p>Erreur JSON</p></body></html>'
217
218 title = ''
219 image_html = ''
220 body = ''
221
222 for item in data.get('items', []):
223 obj = item.get('objet', {})
224 obj_type = obj.get('__type', '')
225
226 # --- En-tête article (titre + image) ---
227 if obj_type == 'article_feature':
228 title = obj.get('long_title', obj.get('title', ''))
229
230 media = obj.get('media', {})
231 img_url = media.get('url', '')
232 if img_url:
233 img_url = img_url.replace('{width}', '800').replace('{height}', '600').replace('{quality}', '85')
234 legende = media.get('legende', '')
235 image_html = (
236 f'<figure style="margin:0 0 1em 0;">'
237 f'<img src="{img_url}" style="max-width:100%;"/>'
238 f'<figcaption style="font-size:0.8em;color:#666;">{legende}</figcaption>'
239 f'</figure>'
240 )
241
242 # --- Corps de l'article ---
243 elif obj_type == 'article_body':
244 for p in obj.get('paragraphs', []):
245 layout = p.get('layout', '')
246 content = p.get('content', '')
247 ptitle = p.get('title', '')
248
249 if layout == 'chapo':
250 body += f'<p style="font-size:1.1em;font-style:italic;">{self.strip_links(content)}</p>'
251
252 elif layout == 'text':
253 if ptitle:
254 body += f'<h2>{ptitle}</h2>'
255 body += f'<p>{self.strip_links(content)}</p>'
256
257 elif layout == 'note':
258 note_obj = p.get('note')
259 if note_obj and note_obj.get('layout') == 'team':
260 # Titre de section équipe (ex : "Chelsea — Note : 3,8")
261 label = note_obj.get('label', '')
262 rating = note_obj.get('rating_label', note_obj.get('rating', ''))
263 color = note_obj.get('background_color', '#333')
264 tcolor = note_obj.get('text_color', '#fff')
265 body += f'<h2 style="background:{color};color:{tcolor};padding:4px 8px;margin-top:1.2em;">{label} — Note : {rating}</h2>'
266 elif content:
267 # Note individuelle joueur avec photo optionnelle
268 img_obj = p.get('image', {})
269 img_url = img_obj.get('url', '') if img_obj else ''
270 if img_url:
271 img_url = img_url.replace('{width}', '120').replace('{height}', '120').replace('{quality}', '80')
272 body += (
273 f'<table style="margin:0.5em 0;width:100%;">'
274 f'<tr>'
275 f'<td style="width:120px;vertical-align:top;">'
276 f'<img src="{img_url}" style="width:110px;"/>'
277 f'</td>'
278 f'<td style="vertical-align:top;padding-left:8px;">'
279 f'{self.strip_links(content)}</td>'
280 f'</tr></table>'
281 )
282 else:
283 body += f'<p>{self.strip_links(content)}</p>'
284
285 elif layout == 'playing_field':
286 data_url = p.get('data', '')
287 if data_url:
288 body += self.parse_playing_field(data_url)
289
290 elif layout == 'link':
291 pass # on ignore les "lire aussi"
292
293 # on ignore aussi layout_DFP / pub_DFP / pub
294
295 return f'<html><head><meta charset="utf-8"><title>{title}</title></head><body>{image_html}<h1>{title}</h1>{body}</body></html>'
296
297 # ------------------------------------------------------------------ #
298 # Flux RSS #
299 # ------------------------------------------------------------------ #
300 feeds = [
301 ('À la une', 'https://dwh.lequipe.fr/api/edito/rss?path=/'),
302 ('Football', 'https://dwh.lequipe.fr/api/edito/rss?path=/Football'),
303 ('Tennis', 'https://dwh.lequipe.fr/api/edito/rss?path=/Tennis'),
304 ('Cyclisme', 'https://dwh.lequipe.fr/api/edito/rss?path=/Cyclisme-sur-route'),
305 ('Rugby', 'https://dwh.lequipe.fr/api/edito/rss?path=/Rugby'),
306 ('Basket', 'https://dwh.lequipe.fr/api/edito/rss?path=/Basket'),
307 ('Formule 1', 'https://dwh.lequipe.fr/api/edito/rss?path=/Formule-1'),
308 ('Athlétisme', 'https://dwh.lequipe.fr/api/edito/rss?path=/Athletisme'),
309 ('Handball', 'https://dwh.lequipe.fr/api/edito/rss?path=/Handball'),
310 ]