Actualiser api.php
This commit is contained in:
@@ -116,45 +116,31 @@ function emptyPhysicalResult() {
|
||||
];
|
||||
}
|
||||
|
||||
// ── SCRAPPING FNAC (CORRIGÉ POUR PARSER LA PAGE DE RÉSULTATS) ──
|
||||
// ── SCRAPPING FNAC (CORRECTION RADICALE) ──
|
||||
function fetchFromFnac($ean) {
|
||||
$empty = emptyPhysicalResult();
|
||||
$url = "https://www.fnac.com/SearchResult/ResultList.aspx?Search=" . urlencode($ean);
|
||||
$html = httpGet($url, 15);
|
||||
if (!$html) return $empty;
|
||||
|
||||
// 1. Essayer de trouver le JSON-LD (méthode la plus fiable)
|
||||
if (preg_match_all('/<script[^>]*type="application\/ld\+json"[^>]*>(.*?)<\/script>/is', $html, $jsonMatches)) {
|
||||
foreach ($jsonMatches[1] as $jsonBlock) {
|
||||
if (preg_match('/"@type"\s*:\s*"Product"[^}]*?"name"\s*:\s*"([^"]+)"/is', $jsonBlock, $m)) {
|
||||
$empty['title'] = trim($m[1]);
|
||||
break;
|
||||
}
|
||||
if (preg_match('/"@type"\s*:\s*"ItemList".*?"name"\s*:\s*"([^"]+)"/is', $jsonBlock, $m)) {
|
||||
$empty['title'] = trim($m[1]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Si pas de JSON-LD, essayer les classes CSS courantes de la FNAC
|
||||
if (empty($empty['title'])) {
|
||||
if (preg_match('/<a[^>]*class="[^"]*js-ProductTitle[^"]*"[^>]*>([^<]+)<\/a>/i', $html, $m)) {
|
||||
$empty['title'] = trim(strip_tags($m[1]));
|
||||
} elseif (preg_match('/<h2[^>]*class="[^"]*f-product__name[^"]*"[^>]*>([^<]+)<\/h2>/i', $html, $m)) {
|
||||
$empty['title'] = trim(strip_tags($m[1]));
|
||||
} elseif (preg_match('/<a[^>]*class="[^"]*product-title[^"]*"[^>]*>([^<]+)<\/a>/i', $html, $m)) {
|
||||
$empty['title'] = trim(strip_tags($m[1]));
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Fallback : chercher le premier lien qui pointe vers une page produit FNAC (/A\d+ ou /tp\d+)
|
||||
if (empty($empty['title'])) {
|
||||
if (preg_match('/<a[^>]*href="\/(A\d+|tp\d+)[^"]*"[^>]*title="([^"]+)"[^>]*>/i', $html, $m)) {
|
||||
$empty['title'] = trim($m[2]);
|
||||
} elseif (preg_match('/<a[^>]*href="\/(A\d+|tp\d+)[^"]*"[^>]*>([^<]+)<\/a>/i', $html, $m)) {
|
||||
$empty['title'] = trim(strip_tags($m[2]));
|
||||
}
|
||||
// ✅ CORRECTION : On ignore totalement le JSON-LD qui est pollué par les bannières publicitaires (type "Project Hail Mary").
|
||||
// On cherche spécifiquement le premier lien HTML qui pointe vers une vraie fiche produit FNAC.
|
||||
// Les URLs de produits FNAC contiennent "/tp12345", "/A12345" ou "-s123.html".
|
||||
|
||||
// 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)) {
|
||||
$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)) {
|
||||
$empty['title'] = trim($m[1]);
|
||||
}
|
||||
// 3. Fallback sur les classes CSS courantes
|
||||
elseif (preg_match('/<h2[^>]*class="[^"]*f-product__name[^"]*"[^>]*>([^<]+)<\/h2>/i', $html, $m)) {
|
||||
$empty['title'] = trim(strip_tags($m[1]));
|
||||
}
|
||||
elseif (preg_match('/<a[^>]*class="[^"]*js-ProductTitle[^"]*"[^>]*>([^<]+)<\/a>/i', $html, $m)) {
|
||||
$empty['title'] = trim(strip_tags($m[1]));
|
||||
}
|
||||
|
||||
// Nettoyage du titre
|
||||
@@ -225,8 +211,6 @@ function fetchFromBlurayComByTitle($title) {
|
||||
|
||||
if (!$movieHtml) return $empty;
|
||||
|
||||
// On ne récupère PAS le titre ici pour éviter l'anglais, on garde celui de la FNAC/TMDB
|
||||
|
||||
if (preg_match('/<h3[^>]*>([^<]+)<\/h3>\s*(?: )?\((\d{4})\)/i', $movieHtml, $m)) $empty['year'] = $m[2];
|
||||
if (preg_match('/href="[^"]*studioid=\d+[^"]*"[^>]*>([^<]+)<\/a>/i', $movieHtml, $m)) $empty['publisher'] = trim($m[1]);
|
||||
if (preg_match('/(\d+)\s*min<\/span>/i', $movieHtml, $m)) $empty['length'] = $m[1] . ' min';
|
||||
|
||||
Reference in New Issue
Block a user