From 1f0fce2c4de3888fd1d9446cdf11751b0b81f7a5 Mon Sep 17 00:00:00 2001 From: Cedric Date: Thu, 25 Jun 2026 15:42:21 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 77 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 30 deletions(-) diff --git a/api.php b/api.php index b0a6205..e4f097a 100644 --- a/api.php +++ b/api.php @@ -121,7 +121,6 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { if (empty($cleanTitle)) return null; // API REST WordPress de DVDcover - // Endpoint : /wp-json/wp/v2/posts?search=TITRE&_embed $searchUrl = "https://www.dvdcover.com/wp-json/wp/v2/posts?search=" . urlencode($cleanTitle) . "&_embed&per_page=10"; $json = httpGet($searchUrl, 10); @@ -140,31 +139,22 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { $bestPost = null; $bestScore = 0; - // Parcourir les posts et scorer leur pertinence foreach ($posts as $post) { $postTitle = $post['title']['rendered'] ?? ''; $postTitleLower = strtolower($postTitle); $score = 0; - - // Le titre du post contient-il le film recherché ? if (strpos($postTitleLower, $cleanTitleLower) !== false) { $score += 50; } - - // Correspondance de l'année if (!empty($year) && strpos($postTitle, $year) !== false) { $score += 30; } - - // Correspondance du format $formatLower = strtolower($format); if (stripos($postTitle, $format) !== false || stripos($postTitle, str_replace('-', ' ', $format)) !== false) { $score += 20; } - - // Bonus si le titre contient "cover" ou "jaquette" if (stripos($postTitle, 'cover') !== false) { $score += 10; } @@ -173,12 +163,9 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { $bestScore = $score; $bestPost = $post; } - - // Score parfait, on arrête if ($score >= 100) break; } - // Si aucun post ne correspond assez bien, prendre le premier if (!$bestPost && !empty($posts)) { $bestPost = $posts[0]; } @@ -191,10 +178,8 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { // Méthode 1 : Image mise en avant (_embedded.wp:featuredmedia) if (isset($bestPost['_embedded']['wp:featuredmedia'][0])) { $featured = $bestPost['_embedded']['wp:featuredmedia'][0]; - // Chercher la plus grande taille disponible if (!empty($featured['media_details']['sizes'])) { $sizes = $featured['media_details']['sizes']; - // Ordre de préférence : full > large > medium > thumbnail $preferredSizes = ['full', 'large', 'medium', 'thumbnail']; foreach ($preferredSizes as $size) { if (isset($sizes[$size]['source_url'])) { @@ -203,7 +188,6 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { } } } - // Fallback : source_url direct if (empty($poster) && !empty($featured['source_url'])) { $poster = $featured['source_url']; } @@ -212,10 +196,8 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { // Méthode 2 : Images dans le contenu du post if (empty($poster) && !empty($bestPost['content']['rendered'])) { $content = $bestPost['content']['rendered']; - // Chercher les balises img if (preg_match_all('/]+src=["\']([^"\']+)["\'][^>]*>/i', $content, $matches)) { foreach ($matches[1] as $img) { - // Filtrer les images non pertinentes if (strpos($img, 'logo') === false && strpos($img, 'icon') === false && strpos($img, 'banner') === false && @@ -230,16 +212,6 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { } } - // Méthode 3 : Images embarquées dans _embedded.wp:embedded - if (empty($poster) && isset($bestPost['_embedded']['wp:embedded'])) { - foreach ($bestPost['_embedded']['wp:embedded'] as $embedded) { - if (!empty($embedded['source_url'])) { - $poster = $embedded['source_url']; - break; - } - } - } - if (empty($poster)) { error_log("DVDCover API: Pas d'image trouvée pour le post ID " . ($bestPost['id'] ?? '?')); return null; @@ -250,10 +222,30 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { $poster = 'https://www.dvdcover.com' . (strpos($poster, '/') === 0 ? '' : '/') . ltrim($poster, '/'); } - // Supprimer les suffixes de taille WordPress pour avoir la version originale - // Ex: image-300x200.jpg → image.jpg + // Supprimer les suffixes de taille WordPress $poster = preg_replace('/-\d+x\d+(\.\w+)$/', '$1', $poster); + // VÉRIFICATION : Tester si l'image est accessible + // Essayer différentes variantes d'URL + $urlVariants = [ + $poster, + str_replace('dvdcover.com', 'www.dvdcover.com', $poster), + str_replace('https://www.dvdcover.com', 'https://dvdcover.com', $poster), + ]; + + foreach ($urlVariants as $testUrl) { + if (urlExists($testUrl, 3)) { + error_log("DVDCover API: Image valide trouvée - $testUrl"); + return [ + 'poster' => $testUrl, + 'title' => $bestPost['title']['rendered'] ?? $cleanTitle, + 'format' => $format, + ]; + } + } + + // Si aucune URL ne fonctionne, retourner quand même la première (peut-être un problème temporaire) + error_log("DVDCover API: Aucune URL d'image valide trouvée, retour de la première URL"); return [ 'poster' => $poster, 'title' => $bestPost['title']['rendered'] ?? $cleanTitle, @@ -261,6 +253,31 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { ]; } +// Fonction pour vérifier si une URL existe +function urlExists($url, $timeout = 3) { + if (!function_exists('curl_init')) { + $ctx = stream_context_create(['http' => ['timeout' => $timeout, 'method' => 'HEAD']]); + $result = @get_headers($url, 0, $ctx); + return ($result && strpos($result[0], '200') !== false); + } + + $ch = curl_init($url); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_NOBODY => true, + CURLOPT_TIMEOUT => $timeout, + CURLOPT_CONNECTTIMEOUT => 2, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', + CURLOPT_FOLLOWLOCATION => true, + ]); + curl_exec($ch); + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + return ($code >= 200 && $code < 400); +} + // ── API TMDB (uniquement pour les critiques) ── function fetchTMDBFull($title, $year, $apiKey, $pdo) { if (empty($apiKey) || empty($title)) return null;