Actualiser api.php

This commit is contained in:
2026-07-02 11:53:09 +02:00
parent c46a961f31
commit 3d956a697f
+19 -8
View File
@@ -116,26 +116,39 @@ function emptyPhysicalResult() {
]; ];
} }
// ── SCRAPPING FNAC (CORRECTION RADICALE) ── // ── SCRAPPING FNAC (CORRECTION v2 : vérification EAN anti-faux-positif) ──
function fetchFromFnac($ean) { function fetchFromFnac($ean) {
$empty = emptyPhysicalResult(); $empty = emptyPhysicalResult();
$url = "https://www.fnac.com/SearchResult/ResultList.aspx?Search=" . urlencode($ean); $url = "https://www.fnac.com/SearchResult/ResultList.aspx?Search=" . urlencode($ean);
$html = httpGet($url, 15); $html = httpGet($url, 15);
if (!$html) return $empty; if (!$html) return $empty;
// ✅ CORRECTION : On ignore totalement le JSON-LD qui est pollué par les bannières publicitaires (type "Project Hail Mary"). // 🔍 LOG DE DIAGNOSTIC — à garder tant que le bug n'est pas confirmé résolu.
// On cherche spécifiquement le premier lien HTML qui pointe vers une vraie fiche produit FNAC. // Permet de vérifier si FNAC renvoie vraiment une page de résultats
// Les URLs de produits FNAC contiennent "/tp12345", "/A12345" ou "-s123.html". // ou une page anti-bot / fallback générique (souvent la vraie cause
// d'un titre "aléatoire" du type "Project Hail Mary").
@file_put_contents(
__DIR__ . '/debug_fnac_' . preg_replace('/[^0-9]/', '', $ean) . '.html',
$html
);
// ✅ GARDE-FOU : si l'EAN recherché n'apparaît nulle part dans le HTML
// retourné, ce n'est pas une vraie page de résultats pour cet EAN
// (bannière promo, page anti-bot, page d'accueil de secours...).
// On refuse le résultat plutôt que de propager un titre erroné.
$eanDigits = preg_replace('/[^0-9]/', '', (string)$ean);
if ($eanDigits && strpos($html, $eanDigits) === false) {
error_log("fetchFromFnac: EAN $eanDigits absent du HTML retourné — page rejetée (probable anti-bot/fallback).");
return $empty;
}
// 1. Chercher un lien avec un titre ou du texte interne // 1. Chercher un lien avec un titre ou du texte interne
if (preg_match('/<a[^>]*href="\/(?:[^"]*\/)?(?:tp\d+|A\d+|[^"]*-s\d+\.html)"[^>]*>([^<]+)<\/a>/i', $html, $m)) { if (preg_match('/<a[^>]*href="\/(?:[^"]*\/)?(?:tp\d+|A\d+|[^"]*-s\d+\.html)"[^>]*>([^<]+)<\/a>/i', $html, $m)) {
$empty['title'] = trim(strip_tags($m[1])); $empty['title'] = trim(strip_tags($m[1]));
} }
// 2. Chercher un lien avec un attribut title
elseif (preg_match('/<a[^>]*href="\/(?:[^"]*\/)?(?:tp\d+|A\d+|[^"]*-s\d+\.html)"[^>]*title="([^"]+)"[^>]*>/i', $html, $m)) { elseif (preg_match('/<a[^>]*href="\/(?:[^"]*\/)?(?:tp\d+|A\d+|[^"]*-s\d+\.html)"[^>]*title="([^"]+)"[^>]*>/i', $html, $m)) {
$empty['title'] = trim($m[1]); $empty['title'] = trim($m[1]);
} }
// 3. Fallback sur les classes CSS courantes
elseif (preg_match('/<h2[^>]*class="[^"]*f-product__name[^"]*"[^>]*>([^<]+)<\/h2>/i', $html, $m)) { elseif (preg_match('/<h2[^>]*class="[^"]*f-product__name[^"]*"[^>]*>([^<]+)<\/h2>/i', $html, $m)) {
$empty['title'] = trim(strip_tags($m[1])); $empty['title'] = trim(strip_tags($m[1]));
} }
@@ -143,14 +156,12 @@ function fetchFromFnac($ean) {
$empty['title'] = trim(strip_tags($m[1])); $empty['title'] = trim(strip_tags($m[1]));
} }
// Nettoyage du titre
if (!empty($empty['title'])) { if (!empty($empty['title'])) {
$empty['title'] = html_entity_decode($empty['title'], ENT_QUOTES | ENT_HTML5, 'UTF-8'); $empty['title'] = html_entity_decode($empty['title'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
$empty['title'] = preg_replace('/\s*\|\s*Fnac.*$/i', '', $empty['title']); $empty['title'] = preg_replace('/\s*\|\s*Fnac.*$/i', '', $empty['title']);
$empty['title'] = trim($empty['title']); $empty['title'] = trim($empty['title']);
} }
// Récupération de l'image
if (preg_match('/<meta[^>]*property="og:image"[^>]*content="([^"]+)"/i', $html, $m)) { if (preg_match('/<meta[^>]*property="og:image"[^>]*content="([^"]+)"/i', $html, $m)) {
$empty['poster'] = trim($m[1]); $empty['poster'] = trim($m[1]);
} elseif (preg_match('/<img[^>]*class="[^"]*js-ProductImage[^"]*"[^>]*src="([^"]+)"/i', $html, $m)) { } elseif (preg_match('/<img[^>]*class="[^"]*js-ProductImage[^"]*"[^>]*src="([^"]+)"/i', $html, $m)) {