From 9b3a84bb63fe5a5e34a31f09f2a1b44a3d8594ee Mon Sep 17 00:00:00 2001 From: Cedric Date: Thu, 2 Jul 2026 09:50:25 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 115 +++++++++++++++++++++++++++----------------------------- 1 file changed, 56 insertions(+), 59 deletions(-) diff --git a/api.php b/api.php index 536401c..d890ed5 100644 --- a/api.php +++ b/api.php @@ -225,13 +225,29 @@ function fetchPhysicalFromUpcitemdb($ean) { function fetchFromUpcIndex($ean) { $url = "https://www.upcindex.com/" . urlencode($ean); - $html = httpGet($url, 15); // Utilise votre fonction curl existante - - if ($html && preg_match('/]*>(.*?)<\/h1>/si', $html, $m)) { - $title = strip_tags($m[1]); - // Nettoyage spécifique : retire le suffixe "- UPC [chiffres]" souvent présent - $title = preg_replace('/ - UPC \d+$/', '', $title); - return trim($title); + $html = httpGet($url, 15); + + if ($html) { + // 1. Priorité absolue : la balise meta twitter:title qui contient le vrai titre propre + // Exemple : + if (preg_match('/]*name="twitter:title"[^>]*content="([^"]+)"/i', $html, $m)) { + // html_entity_decode gère les apostrophes encodées (') + return html_entity_decode(trim($m[1]), ENT_QUOTES | ENT_HTML5, 'UTF-8'); + } + + // 2. Alternative : on cherche dans les données structurées JSON-LD Schema.org + // Exemple : "name":"L'Arme fatale - \u00c9dition Sp\u00e9ciale" + if (preg_match('/"@type":"Product"[^}]+?"name":"([^"]+)"/is', $html, $m)) { + // json_decode permet de transformer proprement les \u00c9 en é, etc. + return json_decode('"' . $m[1] . '"'); + } + + // 3. Fallback sur le h1 (qui contient souvent le titre sale) + if (preg_match('/]*>(.*?)<\/h1>/si', $html, $m)) { + $title = strip_tags($m[1]); + $title = preg_replace('/ - UPC \d+$/', '', $title); + return trim($title); + } } return null; } @@ -878,48 +894,35 @@ case 'get_films': error_log("--- DÉBUT RECHERCHE EAN : $ean ---"); - // ✅ UTILISER fetchPhysicalByEan qui fonctionne (UPCitemdb → UPCMDB fallback) + // 1. Récupérer les données physiques $physicalData = fetchPhysicalByEan($ean, $pdo); - $title = $physicalData['title'] ?? ''; + $rawTitle = $physicalData['title'] ?? ''; + $directorFromEan = $physicalData['director'] ?? ''; - error_log("fetchPhysicalByEan a trouvé : " . ($title ?: 'RIEN')); - - if (empty($title)) { - echo json_encode(["success" => false, "error" => "EAN non trouvé dans les bases de données"]); - exit; - } - - // ✅ Nettoyage amélioré du titre pour TMDB - $cleanTitle = cleanUpcTitle($title); - error_log("Titre nettoyé pour TMDB : '$cleanTitle'"); - - // Recherche TMDB avec le titre nettoyé - $tmdbData = fetchTmdbPosterAndSynopsis($cleanTitle, $physicalData['year'] ?? '', $pdo); + error_log("UPCitemdb titre brut : '$rawTitle'"); + if ($directorFromEan) error_log("UPCitemdb réalisateur : '$directorFromEan'"); - // Si TMDB ne trouve rien, essayer avec le titre original nettoyé différemment - if (empty($tmdbData['title']) || $tmdbData['poster'] === 'assets/img/default_physical_media.jpg') { - // Tentative alternative : extraire juste le nom du film sans "DVD", "Blu-ray", etc. - $altTitle = preg_replace('/^(DVD|Blu-ray|Bluray|4K|Ultra HD|VHS)\s*/i', '', $title); - $altTitle = preg_replace('/\s*(New Blister|Import|Edition|Édition|Spéciale|Special|Collector).*$/i', '', $altTitle); - $altTitle = trim(preg_replace('/\s{2,}/', ' ', $altTitle)); - - if ($altTitle !== $cleanTitle) { - error_log("Tentative alternative TMDB avec : '$altTitle'"); - $tmdbDataAlt = fetchTmdbPosterAndSynopsis($altTitle, $physicalData['year'] ?? '', $pdo); - if (!empty($tmdbDataAlt['title']) && $tmdbDataAlt['poster'] !== 'assets/img/default_physical_media.jpg') { - $tmdbData = $tmdbDataAlt; - } - } - } - - // Déterminer le titre final - $finalTitle = !empty($tmdbData['title']) ? $tmdbData['title'] : $cleanTitle; - $poster = ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') ? $tmdbData['poster'] : ($physicalData['poster'] ?? ''); + // 2. Chercher sur TMDB avec validation par réalisateur + $tmdbData = smartSearchTmdbWithDirector( + $rawTitle, + $directorFromEan, + $physicalData['year'] ?? '', + $pdo + ); - error_log("Titre final : '$finalTitle' | Poster : " . ($poster ?: 'AUCUN')); + // 3. Fusionner les données + $finalTitle = !empty($tmdbData['title']) ? $tmdbData['title'] : $rawTitle; + $poster = ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') + ? $tmdbData['poster'] + : ($physicalData['poster'] ?? ''); + + error_log("=== RÉSULTAT FINAL ==="); + error_log("Titre FR : $finalTitle"); + error_log("Réalisateur : " . ($tmdbData['director'] ?: $directorFromEan)); - // Insertion dans la base - $id = makeStableId('videotheque', $finalTitle, $tmdbData['year'] ?? $physicalData['year'] ?? date('Y')); + // 4. Insertion en base + $year = $tmdbData['year'] ?? $physicalData['year'] ?? ''; + $id = makeStableId('videotheque', $finalTitle, $year); $stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year, poster, description, director, actors, ean_isbn13, format, length, publisher, number_of_discs, aspect_ratio) @@ -928,22 +931,16 @@ case 'get_films': title=VALUES(title), year=VALUES(year), poster=IF(VALUES(poster)!='', VALUES(poster), poster), description=IF(VALUES(description)!='', VALUES(description), description), director=IF(VALUES(director)!='', VALUES(director), director), - actors=IF(VALUES(actors)!='', VALUES(actors), actors), - format=IF(VALUES(format)!='', VALUES(format), format), - length=IF(VALUES(length)!='', VALUES(length), length), - publisher=IF(VALUES(publisher)!='', VALUES(publisher), publisher)"); + actors=IF(VALUES(actors)!='', VALUES(actors), actors)"); $stmt->execute([ - $id, - $finalTitle, - $tmdbData['year'] ?? $physicalData['year'] ?? '', - $poster, - $tmdbData['description'] ?? $physicalData['description'] ?? '', - $tmdbData['director'] ?? $physicalData['director'] ?? '', - $tmdbData['actors'] ?? $physicalData['actors'] ?? '', + $id, $finalTitle, $year, $poster, + $tmdbData['description'] ?? '', + $tmdbData['director'] ?: $directorFromEan, + $tmdbData['actors'] ?? '', $ean, - $physicalData['format'] ?? detectFormat($title), - $tmdbData['length'] ?? $physicalData['length'] ?? '', + $physicalData['format'] ?? detectFormat($rawTitle), + $tmdbData['length'] ?? '', $physicalData['publisher'] ?? '', $physicalData['number_of_discs'] ?? 1, $physicalData['aspect_ratio'] ?? '' @@ -952,8 +949,8 @@ case 'get_films': echo json_encode([ "success" => true, "title" => $finalTitle, - "poster" => $poster, - "year" => $tmdbData['year'] ?? $physicalData['year'] ?? '' + "director" => $tmdbData['director'] ?: $directorFromEan, + "year" => $year ]); break;