From 3364c5885ce565431db8b7ea32326d46e01c0903 Mon Sep 17 00:00:00 2001 From: Cedric Date: Thu, 25 Jun 2026 12:05:00 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 145 +++++++++++++++++++++++++++----------------------------- 1 file changed, 71 insertions(+), 74 deletions(-) diff --git a/api.php b/api.php index 6a74c0f..4dc1279 100644 --- a/api.php +++ b/api.php @@ -115,12 +115,13 @@ function extractYear($dateStr) { return ''; } -// ── DVDcover.com (avec validation du film) ── +// ── DVDcover.com (avec vérification du titre) ── function fetchDVDCover($title, $year = '', $format = 'bluray') { if (empty($title)) return null; $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'; $cleanTitle = cleanTitle($title); + $cleanTitleLower = strtolower($cleanTitle); // Mapping des formats $formatMap = [ @@ -133,12 +134,12 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { $dcFormat = $formatMap[strtolower($format)] ?? 'blu-ray'; - // Recherche plus spécifique avec titre + année - $searchUrl = "https://www.dvdcover.com/?s=" . urlencode($cleanTitle . " " . $year); + // URL de recherche DVDcover + $searchUrl = "https://www.dvdcover.com/?s=" . urlencode($cleanTitle); $html = httpGet($searchUrl, 8, $ua); if (!$html) { - error_log("DVDCover: Échec recherche pour '$title $year'"); + error_log("DVDCover: Échec recherche pour '$title'"); return null; } @@ -148,15 +149,15 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { 'format' => $format, ]; - // Chercher tous les articles de covers - preg_match_all('/]*id=["\']post-\d+["\'][^>]*>(.*?)<\/article>/is', $html, $articles); + // Chercher tous les articles/résultats + preg_match_all('/]*>(.*?)<\/article>/is', $html, $articles); if (!empty($articles[1])) { $bestMatch = null; $bestScore = 0; foreach ($articles[1] as $article) { - // Extraire le lien vers la page du cover + // Extraire le lien vers la page if (!preg_match('/]+href=["\']([^"\']+)["\'][^>]*>/i', $article, $linkMatch)) { continue; } @@ -166,95 +167,91 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { $coverPage = 'https://www.dvdcover.com' . $coverPage; } - // Extraire le titre du cover depuis l'article - $articleTitle = ''; + // Extraire le titre de l'article + $pageTitle = ''; if (preg_match('/]*class=["\'][^"\']*entry-title[^"\']*["\'][^>]*>([^<]+)<\/h2>/i', $article, $titleMatch)) { - $articleTitle = trim(strip_tags($titleMatch[1])); + $pageTitle = trim(strip_tags($titleMatch[1])); + } elseif (preg_match('/title=["\']([^"\']+)["\']/i', $article, $titleMatch)) { + $pageTitle = trim($titleMatch[1]); } - // Calculer un score de correspondance - $score = 0; - $articleTitleLower = strtolower($articleTitle); - $searchTitleLower = strtolower($cleanTitle); + if (empty($pageTitle)) continue; - // Le titre contient-il le film recherché ? - if (strpos($articleTitleLower, $searchTitleLower) !== false) { - $score += 50; + // Calculer un score de correspondance + $pageTitleLower = strtolower($pageTitle); + $score = 0; + + // Le titre recherché est-il dans le titre de la page ? + if (strpos($pageTitleLower, $cleanTitleLower) !== false) { + $score += 100; } // L'année correspond-elle ? - if (!empty($year) && strpos($articleTitle, $year) !== false) { - $score += 30; + if (!empty($year) && strpos($pageTitle, $year) !== false) { + $score += 50; } // Le format correspond-il ? - if (stripos($articleTitle, $format) !== false || - stripos($articleTitle, $dcFormat) !== false) { - $score += 20; + if (stripos($pageTitle, $format) !== false || + ($format === 'Blu-ray' && stripos($pageTitle, 'bluray') !== false) || + ($format === '4K Ultra HD' && (stripos($pageTitle, '4k') !== false || stripos($pageTitle, 'ultra') !== false))) { + $score += 25; } - // Si le score est trop bas, on ignore ce résultat - if ($score < 30) { - continue; - } - - // Récupérer la page du cover - $coverHtml = httpGet($coverPage, 8, $ua); - if (!$coverHtml) continue; - - // Chercher l'image principale - $poster = ''; - - // Méthode 1 : Image dans le contenu principal - if (preg_match('/]*class=["\'][^"\']*entry-content[^"\']*["\'][^>]*>.*?]+src=["\']([^"\']+\/wp-content\/uploads\/[^"\']+\.(?:jpg|jpeg|webp))["\'][^>]*>/is', $coverHtml, $imgMatch)) { - $poster = $imgMatch[1]; - } - - // Méthode 2 : Image dans figure - if (empty($poster) && preg_match('/]*>.*?]+src=["\']([^"\']+\/wp-content\/uploads\/[^"\']+\.(?:jpg|jpeg|webp))["\'][^>]*>/is', $coverHtml, $imgMatch)) { - $poster = $imgMatch[1]; - } - - // Méthode 3 : Première image valide - if (empty($poster)) { - preg_match_all('/]+src=["\']([^"\']+\/wp-content\/uploads\/[^"\']+\.(?:jpg|jpeg|webp))["\'][^>]*>/i', $coverHtml, $allImages); - if (!empty($allImages[1])) { - foreach ($allImages[1] as $img) { - if (strpos($img, 'logo') === false && - strpos($img, 'icon') === false && - strpos($img, 'banner') === false && - strpos($img, 'bg') === false && - strpos($img, 'button') === false && - strpos($img, '300x200') === false && // Éviter les miniatures - strpos($img, '150x150') === false) { - $poster = $img; - break; + // Si le score est suffisant, visiter la page + if ($score >= 50) { + $coverHtml = httpGet($coverPage, 8, $ua); + if ($coverHtml) { + // Chercher l'image principale + $poster = ''; + + // Méthode 1 : Chercher dans le contenu de l'article + if (preg_match('/]*class=["\'][^"\']*entry-content[^"\']*["\'][^>]*>.*?]+src=["\']([^"\']+\/wp-content\/uploads\/[^"\']+\.(?:jpg|jpeg|webp))["\'][^>]*>/is', $coverHtml, $imgMatch)) { + $poster = $imgMatch[1]; + } + // Méthode 2 : Chercher toutes les images uploads + elseif (preg_match_all('/]+src=["\']([^"\']+\/wp-content\/uploads\/[^"\']+\.(?:jpg|jpeg|webp))["\'][^>]*>/i', $coverHtml, $allImages)) { + foreach ($allImages[1] as $img) { + // Exclure les images non pertinentes + if (strpos($img, 'logo') === false && + strpos($img, 'icon') === false && + strpos($img, 'banner') === false && + strpos($img, 'bg') === false && + strpos($img, 'button') === false && + strpos($img, 'sidebar') === false) { + $poster = $img; + break; + } + } + } + + if (!empty($poster)) { + // Extraire le titre de la page + $finalTitle = ''; + if (preg_match('/]*class=["\'][^"\']*entry-title[^"\']*["\'][^>]*>([^<]+)<\/h1>/i', $coverHtml, $titleMatch)) { + $finalTitle = trim(strip_tags($titleMatch[1])); + } + + // Garder le meilleur résultat + if ($score > $bestScore) { + $bestScore = $score; + $bestMatch = [ + 'poster' => $poster, + 'title' => $finalTitle ?: $pageTitle, + 'format' => $format, + ]; } } - } - } - - // Si on a une image et un bon score, on la garde - if (!empty($poster) && $score > $bestScore) { - $bestScore = $score; - $bestMatch = [ - 'poster' => $poster, - 'title' => $articleTitle, - ]; - - // Si score parfait, on arrête tout de suite - if ($score >= 100) { - break; } } } if ($bestMatch) { - $result = $bestMatch; + return $bestMatch; } } - return (!empty($result['poster'])) ? $result : null; + return null; } // ── API TMDB (uniquement pour les critiques) ──