Debian Codegraph @

recipes/courrierinternational.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3
4 __license__ = 'GPL v3'
5 __copyright__ = '2009, Mathieu Godlewski <mathieu at godlewski.fr>, 2015, Rémi Vanicat <vanicat at debian.org>, 2026, Kabonix'
6 '''
7 Courrier International Premium Unlocked
8 '''
9
10 import json
11 import re
12
13 from calibre.web.feeds.news import BasicNewsRecipe
14
15
16 class CourrierInternational(BasicNewsRecipe):
17 title = 'Courrier International'
18 __author__ = 'Mathieu Godlewski, Kabonix'
19 description = 'Global news in french - Edition intégrale via API Mobile Bypass'
20 oldest_article = 7
21 language = 'fr'
22 encoding = 'utf-8'
23
24 # --- CONFIGURATION DU BYPASS PREMIUM ---
25 # On utilise l'identité de l'application Android 3.5.4
26 browser_user_agent = 'CourrierInternational/3.5.4 (Android; 14)'
27
28 def get_browser(self, *args, **kwargs):
29 br = BasicNewsRecipe.get_browser(self, *args, **kwargs)
30 # On injecte le Token universel du groupe Le Monde
31 br.addheaders = [('User-Agent', self.browser_user_agent), ('X-Lmd-Token', 'TWPLMOLMO'), ('Accept', 'application/json')]
32 return br
33
34 # --- NOUVEAU : GESTION DE LA COUVERTURE ---
35 def get_cover_url(self):
36 cover_url = None
37 try:
38 self.log('🔍 Recherche de la couverture du dernier numéro...')
39 # On charge la page kiosque
40 soup = self.index_to_soup('https://www.courrierinternational.com/magazine')
41
42 # On cherche le premier article de type "magazine" dans la liste
43 # Le sélecteur correspond au HTML que tu as fourni
44 first_mag = soup.find('div', class_='magazines-list').find('article', class_='item')
45
46 if first_mag:
47 img = first_mag.find('img')
48 if img and img.has_attr('src'):
49 url = img['src']
50 # L'URL est en 320x0 (ex: .../320x0/2026/...)
51 # On la passe en HD (1280x0) pour la liseuse
52 cover_url = url.replace('/320x0/', '/1280x0/')
53 self.log(f'✅ Couverture trouvée et améliorée : {cover_url}')
54
55 if not cover_url:
56 self.log('⚠️ Aucune couverture trouvée.')
57
58 except Exception as e:
59 self.log(f'❌ Erreur lors de la récupération de la couverture : {e}')
60
61 return cover_url
62
63 def get_article_url(self, article):
64 url = BasicNewsRecipe.get_article_url(self, article)
65 # On extrait l'ID à la fin de l'URL (ex: ..._240469)
66 match = re.search(r'_(\d+)$', url)
67 if match:
68 article_id = match.group(1)
69 # On redirige Calibre vers l'API mobile Premium
70 return f'https://apps.courrierinternational.com/cri/v1/premium-android-phone/article?id={article_id}'
71 return url
72
73 def preprocess_raw_html(self, raw_html, url):
74 # Si on est sur l'API, on décode le JSON pour extraire le HTML intégral
75 if '/cri/v1/' in url:
76 try:
77 data = json.loads(raw_html)
78 # Le texte est dans templates -> raw_content -> content
79 content = data['templates']['raw_content']['content']
80
81 # Fix des images (placeholders {{width}})
82 content = content.replace('{{width}}', '1200').replace('{{height}}', '800')
83 content = content.replace('%7B%7Bwidth%7D%7D', '1200')
84
85 return f'<html><body>{content}</body></html>'
86 except Exception as e:
87 self.log(f'Erreur de décodage pour {url}: {e!s}')
88 return raw_html
89 return raw_html
90
91 # ---------------------------------------
92
93 max_articles_per_feed = 50
94 ignore_duplicate_articles = {'title'}
95 no_stylesheets = True
96
97 keep_only_tags = [
98 dict(name='article'),
99 dict(name='span', attrs={'class': 'strapline'}),
100 dict(name='h1', attrs={'class': 'article-title'}),
101 dict(name='p', attrs={'class': 'article-lede'}),
102 dict(name='div', attrs={'class': 'article-text'}),
103 ]
104
105 remove_tags = [
106 dict(
107 attrs={
108 'class': [
109 'asset-read-more',
110 'article-secondary',
111 'article-aside',
112 'item',
113 'source-logo',
114 'source-lang',
115 'info-time',
116 'article-paywall',
117 'article-readmore',
118 'article-outbrain',
119 'ci-services',
120 'article-tools',
121 ]
122 }
123 )
124 ]
125
126 extra_css = '''
127 .strapline { color: #cc0000; font-weight: bold; text-transform: uppercase; display: block; margin-bottom: 5px; }
128 .article-title { font-size: 1.6em; font-weight: bold; }
129 .article-lede { font-weight: bold; margin-bottom: 1em; color: #333; }
130 .article-text { line-height: 1.5; }
131 .caption, .credit { font-size: 0.8em; font-style: italic; color: #666; }
132 p { margin-bottom: 1em; }
133 img { display: block; margin: 1em auto; max-width: 100%; }
134 '''
135
136 def preprocess_html(self, soup):
137 # Gestion des images via srcset
138 for img in soup.find_all('img'):
139 if img.has_attr('srcset'):
140 best_link = img['srcset'].split(',')[-1].strip().split(' ')[0]
141 img['src'] = best_link
142 # Fix des liens relatifs
143 if img.has_attr('src') and img['src'].startswith('/'):
144 img['src'] = 'https://www.courrierinternational.com' + img['src']
145
146 for link in soup.findAll('a', href=re.compile(r'^/')):
147 link['href'] = 'https://www.courrierinternational.com' + link['href']
148 return soup
149
150 feeds = [
151 ('France', 'https://www.courrierinternational.com/feed/rubrique/france/rss.xml'),
152 ('Geopolitique', 'https://www.courrierinternational.com/feed/rubrique/geopolitique/rss.xml'),
153 ('Economie', 'https://www.courrierinternational.com/feed/rubrique/economie/rss.xml'),
154 ('Société', 'https://www.courrierinternational.com/feed/rubrique/societe/rss.xml'),
155 ('Politique', 'https://www.courrierinternational.com/feed/rubrique/politique/rss.xml'),
156 ('Sciences & Environnement', 'https://www.courrierinternational.com/feed/rubrique/science-environnement/rss.xml'),
157 ('Culture', 'https://www.courrierinternational.com/feed/rubrique/culture/rss.xml'),
158 ('Expat', 'https://www.courrierinternational.com/feed/rubrique/expat/rss.xml'),
159 ('Autres', 'https://www.courrierinternational.com/feed/all/rss.xml'),
160 ]