| 1 |
import http.cookiejar |
| 2 |
import json |
| 3 |
import re |
| 4 |
import urllib.parse |
| 5 |
import urllib.request |
| 6 |
|
| 7 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 8 |
|
| 9 |
|
| 10 |
class LHumanite(BasicNewsRecipe): |
| 11 |
title = "L'Humanité" |
| 12 |
__author__ = 'Kabonix' |
| 13 |
language = 'fr' |
| 14 |
oldest_article = 7 |
| 15 |
max_articles_per_feed = 50 |
| 16 |
no_stylesheets = True |
| 17 |
remove_javascript = True |
| 18 |
auto_cleanup = False |
| 19 |
|
| 20 |
feeds = [ |
| 21 |
('À la une', 'https://www.humanite.fr/feed'), |
| 22 |
('Révélations', 'https://www.humanite.fr/mot-cle/revelations-humanite/feed'), |
| 23 |
] |
| 24 |
|
| 25 |
keep_only_tags = [ |
| 26 |
dict(id='article-reconstructed'), |
| 27 |
] |
| 28 |
|
| 29 |
# --- Couverture dynamique --- |
| 30 |
def _get_cover_url(self): |
| 31 |
""" |
| 32 |
Interroge l'API Immanens pour récupérer le doc-id |
| 33 |
du dernier numéro de L'Humanité (pub-id=3182). |
| 34 |
""" |
| 35 |
api_url = 'https://docimg-cdn.immanens.com/phnxc1/publications/logistic-code/PVN1/l-pub-id/3182/issues/last' |
| 36 |
try: |
| 37 |
req = urllib.request.Request( |
| 38 |
api_url, |
| 39 |
headers={ |
| 40 |
'User-Agent': 'Mozilla/5.0', |
| 41 |
'Accept': 'application/json', |
| 42 |
}, |
| 43 |
) |
| 44 |
with urllib.request.urlopen(req) as resp: |
| 45 |
data = json.loads(resp.read().decode('utf-8')) |
| 46 |
doc_id = data.get('docId') or data.get('id') or data.get('doc_id') |
| 47 |
if doc_id: |
| 48 |
self.log(f'📰 Cover doc-id trouvé : {doc_id}') |
| 49 |
return ( |
| 50 |
f'https://docimg-cdn.immanens.com/phnxc1/getcover' |
| 51 |
f'/logistic-code/PVN1/l-pub-id/3182/l-doc-id/{doc_id}' |
| 52 |
f'/doc-version/1/profile/cover-large.jpg' |
| 53 |
) |
| 54 |
except Exception as e: |
| 55 |
self.log(f'⚠️ API Immanens échouée : {e}') |
| 56 |
|
| 57 |
# Fallback : scraper la page kiosque pour trouver l'URL de cover |
| 58 |
try: |
| 59 |
kiosque_url = 'https://kiosque.humanite.fr/home/publication/shelf' |
| 60 |
req = urllib.request.Request(kiosque_url, headers={'User-Agent': 'Mozilla/5.0'}) |
| 61 |
with urllib.request.urlopen(req) as resp: |
| 62 |
html = resp.read().decode('utf-8', errors='ignore') |
| 63 |
# Cherche l'URL d'image Immanens dans le HTML initial (SSR partiel) |
| 64 |
m = re.search( |
| 65 |
r'https://docimg-cdn\.immanens\.com/phnxc1/getcover' |
| 66 |
r'/logistic-code/PVN1/l-pub-id/3182/l-doc-id/(\d+)' |
| 67 |
r'/doc-version/\d+/profile/cover-(?:medium|large)\.jpg', |
| 68 |
html, |
| 69 |
) |
| 70 |
if m: |
| 71 |
self.log(f'📰 Cover trouvée via scraping : {m.group(0)}') |
| 72 |
return m.group(0) |
| 73 |
except Exception as e: |
| 74 |
self.log(f'⚠️ Scraping kiosque échoué : {e}') |
| 75 |
|
| 76 |
# Fallback absolu : URL connue hardcodée en dernier recours |
| 77 |
self.log('⚠️ Utilisation cover fallback hardcodée') |
| 78 |
return 'https://docimg-cdn.immanens.com/phnxc1/getcover/logistic-code/PVN1/l-pub-id/3182/l-doc-id/433987/doc-version/4/profile/cover-large.jpg' |
| 79 |
|
| 80 |
def get_cover_url(self): |
| 81 |
return self._get_cover_url() |
| 82 |
|
| 83 |
# --- Bypass paywall --- |
| 84 |
def _fetch_full_content(self, url): |
| 85 |
cj = http.cookiejar.CookieJar() |
| 86 |
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) |
| 87 |
|
| 88 |
base_headers = { |
| 89 |
'User-Agent': ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'), |
| 90 |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', |
| 91 |
'Referer': 'https://www.humanite.fr/', |
| 92 |
'Accept-Language': 'fr-FR,fr;q=0.9', |
| 93 |
} |
| 94 |
|
| 95 |
req = urllib.request.Request(url, headers=base_headers) |
| 96 |
with opener.open(req) as resp: |
| 97 |
raw_html = resp.read().decode('utf-8', errors='ignore') |
| 98 |
|
| 99 |
title_match = re.search(r'<h1[^>]*class="[^"]*t-article-h1[^"]*"[^>]*>(.*?)</h1>', raw_html, re.DOTALL) |
| 100 |
title_html = title_match.group(0) if title_match else '<h1>Sans titre</h1>' |
| 101 |
|
| 102 |
chapo_match = re.search(r'<p[^>]*class="chapo"[^>]*>(.*?)</p>', raw_html, re.DOTALL) |
| 103 |
chapo_html = f'<p><strong>{chapo_match.group(1)}</strong></p>' if chapo_match else '' |
| 104 |
|
| 105 |
img_match = re.search(r'<div class="single__thumbnail.*?</div>\s*</div>', raw_html, re.DOTALL) |
| 106 |
if img_match: |
| 107 |
img_html = img_match.group(0) |
| 108 |
else: |
| 109 |
og_match = re.search(r'<meta property="og:image" content="([^"]+)"', raw_html) |
| 110 |
img_html = f'<img src="{og_match.group(1)}" alt=""/>' if og_match else '' |
| 111 |
|
| 112 |
pid_match = re.search(r'["\']?postId["\']?\s*[=:]\s*["\']?(\d+)', raw_html) |
| 113 |
nonce_match = re.search(r'["\']?ajaxNonce["\']?\s*[=:]\s*["\']([a-zA-Z0-9]+)["\']', raw_html) |
| 114 |
|
| 115 |
if not (pid_match and nonce_match): |
| 116 |
self.log(f'⚠️ Clés introuvables pour {url}') |
| 117 |
return None, title_html, chapo_html, img_html |
| 118 |
|
| 119 |
post_id = pid_match.group(1) |
| 120 |
nonce = nonce_match.group(1) |
| 121 |
|
| 122 |
ajax_headers = base_headers.copy() |
| 123 |
ajax_headers.update({ |
| 124 |
'X-Requested-With': 'XMLHttpRequest', |
| 125 |
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', |
| 126 |
'Origin': 'https://www.humanite.fr', |
| 127 |
'Referer': url, |
| 128 |
'Accept': 'application/json, text/javascript, */*; q=0.01', |
| 129 |
}) |
| 130 |
|
| 131 |
data = urllib.parse.urlencode({ |
| 132 |
'action': 'unlock-post-content', |
| 133 |
'security': nonce, |
| 134 |
'post_id': post_id, |
| 135 |
}).encode('utf-8') |
| 136 |
|
| 137 |
ajax_req = urllib.request.Request( |
| 138 |
'https://www.humanite.fr/wp-admin/admin-ajax.php', |
| 139 |
data=data, |
| 140 |
headers=ajax_headers, |
| 141 |
) |
| 142 |
with opener.open(ajax_req) as api_resp: |
| 143 |
payload = json.loads(api_resp.read().decode('utf-8', errors='ignore')) |
| 144 |
|
| 145 |
if payload.get('success'): |
| 146 |
return payload.get('data', ''), title_html, chapo_html, img_html |
| 147 |
|
| 148 |
return None, title_html, chapo_html, img_html |
| 149 |
|
| 150 |
def preprocess_raw_html(self, raw_html, url): |
| 151 |
try: |
| 152 |
full_content, title_html, chapo_html, img_html = self._fetch_full_content(url) |
| 153 |
|
| 154 |
return f''' |
| 155 |
<html> |
| 156 |
<head><meta charset="UTF-8"></head> |
| 157 |
<body> |
| 158 |
<div id="article-reconstructed"> |
| 159 |
{title_html} |
| 160 |
{chapo_html} |
| 161 |
{img_html} |
| 162 |
<div id="post-content"> |
| 163 |
{full_content or ''} |
| 164 |
</div> |
| 165 |
</div> |
| 166 |
</body> |
| 167 |
</html> |
| 168 |
''' |
| 169 |
except Exception as e: |
| 170 |
self.log(f'❌ Erreur sur {url} : {e}') |
| 171 |
return raw_html |