From cb148b4c584164387f297d43fdf65c10126eee68 Mon Sep 17 00:00:00 2001 From: Cedric Date: Thu, 25 Jun 2026 11:57:19 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 133 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 78 insertions(+), 55 deletions(-) diff --git a/api.php b/api.php index acde442..6a74c0f 100644 --- a/api.php +++ b/api.php @@ -115,7 +115,7 @@ function extractYear($dateStr) { return ''; } -// ── DVDcover.com (scraping précis) ── +// ── DVDcover.com (avec validation du film) ── function fetchDVDCover($title, $year = '', $format = 'bluray') { if (empty($title)) return null; @@ -133,12 +133,12 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { $dcFormat = $formatMap[strtolower($format)] ?? 'blu-ray'; - // URL de recherche DVDcover - $searchUrl = "https://www.dvdcover.com/?s=" . urlencode($cleanTitle); + // Recherche plus spécifique avec titre + année + $searchUrl = "https://www.dvdcover.com/?s=" . urlencode($cleanTitle . " " . $year); $html = httpGet($searchUrl, 8, $ua); if (!$html) { - error_log("DVDCover: Échec recherche pour '$title'"); + error_log("DVDCover: Échec recherche pour '$title $year'"); return null; } @@ -148,87 +148,110 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') { 'format' => $format, ]; - // DVDcover utilise WordPress - les articles ont la classe "post" - // Chercher les liens vers les pages individuelles de covers - preg_match_all('/]*class=["\'][^"\']*post[^"\']*["\'][^>]*>.*?]+href=["\']([^"\']+)["\'][^>]*>.*?<\/article>/is', $html, $articles); + // Chercher tous les articles de covers + preg_match_all('/]*id=["\']post-\d+["\'][^>]*>(.*?)<\/article>/is', $html, $articles); if (!empty($articles[1])) { - // Parcourir les résultats - foreach ($articles[1] as $coverPage) { + $bestMatch = null; + $bestScore = 0; + + foreach ($articles[1] as $article) { + // Extraire le lien vers la page du cover + if (!preg_match('/]+href=["\']([^"\']+)["\'][^>]*>/i', $article, $linkMatch)) { + continue; + } + + $coverPage = $linkMatch[1]; if (strpos($coverPage, 'http') !== 0) { $coverPage = 'https://www.dvdcover.com' . $coverPage; } + // Extraire le titre du cover depuis l'article + $articleTitle = ''; + if (preg_match('/]*class=["\'][^"\']*entry-title[^"\']*["\'][^>]*>([^<]+)<\/h2>/i', $article, $titleMatch)) { + $articleTitle = trim(strip_tags($titleMatch[1])); + } + + // Calculer un score de correspondance + $score = 0; + $articleTitleLower = strtolower($articleTitle); + $searchTitleLower = strtolower($cleanTitle); + + // Le titre contient-il le film recherché ? + if (strpos($articleTitleLower, $searchTitleLower) !== false) { + $score += 50; + } + + // L'année correspond-elle ? + if (!empty($year) && strpos($articleTitle, $year) !== false) { + $score += 30; + } + + // Le format correspond-il ? + if (stripos($articleTitle, $format) !== false || + stripos($articleTitle, $dcFormat) !== false) { + $score += 20; + } + + // 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; - // Méthode 1 : Chercher l'image principale dans le contenu de l'article - // DVDcover met les jaquettes dans
ou
- if (preg_match('/]*>.*?]+src=["\']([^"\']+)["\'][^>]*>/is', $coverHtml, $m)) { - $img = $m[1]; - // Vérifier que c'est bien une jaquette (pas une image de thème) - if (strpos($img, '/wp-content/uploads/') !== false && - strpos($img, '/wp-content/themes/') === false && - (strpos($img, '.jpg') !== false || strpos($img, '.jpeg') !== false || strpos($img, '.webp') !== false)) { - $result['poster'] = $img; - } + // 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 : Chercher dans entry-content - if (empty($result['poster']) && preg_match('/]*class=["\'][^"\']*entry-content[^"\']*["\'][^>]*>.*?]+src=["\']([^"\']+)["\'][^>]*>/is', $coverHtml, $m)) { - $img = $m[1]; - if (strpos($img, '/wp-content/uploads/') !== false && - strpos($img, '/wp-content/themes/') === false) { - $result['poster'] = $img; - } + // 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 : Chercher toutes les images et prendre la plus grande - if (empty($result['poster'])) { + // 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])) { - // Prendre la première image qui n'est pas un logo ou icône 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) { - $result['poster'] = $img; + strpos($img, 'button') === false && + strpos($img, '300x200') === false && // Éviter les miniatures + strpos($img, '150x150') === false) { + $poster = $img; break; } } } } - // Extraire le titre - if (preg_match('/]*class=["\'][^"\']*entry-title[^"\']*["\'][^>]*>([^<]+)<\/h1>/i', $coverHtml, $titleMatch)) { - $result['title'] = trim(strip_tags($titleMatch[1])); - } elseif (preg_match('/]*>([^<]+)<\/title>/i', $coverHtml, $titleMatch)) { - $result['title'] = trim(strip_tags($titleMatch[1])); - } - - // Si on a trouvé une image, on arrête - if (!empty($result['poster'])) { - break; - } - } - } - - // Fallback : chercher directement dans la page de résultats - if (empty($result['poster'])) { - preg_match_all('/]+src=["\']([^"\']+\/wp-content\/uploads\/[^"\']+\.(?:jpg|jpeg|webp))["\'][^>]*>/i', $html, $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) { - $result['poster'] = $img; + // 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 (!empty($result['poster'])) ? $result : null;