From ec77829a2f14e1fa9f82138d3ce854b7946eb63d Mon Sep 17 00:00:00 2001 From: Cedric Date: Thu, 25 Jun 2026 13:12:01 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 137 +++++++++++++++++++------------------------------------- 1 file changed, 46 insertions(+), 91 deletions(-) diff --git a/api.php b/api.php index 46bd9a8..aa61d56 100644 --- a/api.php +++ b/api.php @@ -115,113 +115,68 @@ function extractYear($dateStr) { return ''; } -// ── DVDCover.com Scraper ── 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); - // URL de recherche DVDCover + // On encode proprement pour l'URL $searchUrl = "https://www.dvdcover.com/?s=" . urlencode($cleanTitle); - $html = httpGet($searchUrl, 8, $ua); + $html = httpGet($searchUrl, 10, $ua); if (!$html) { - error_log("DVDCover: Échec recherche pour '$title'"); + error_log("DVDCover: Erreur réseau pour '$title'"); + return null; + } + + // DEBUG : Sauvegarde le contenu pour vérifier si on est bloqué + file_put_contents(__DIR__ . '/debug_scrap.html', $html); + + $dom = new DOMDocument(); + @$dom->loadHTML($html); + $xpath = new DOMXPath($dom); + + // On cherche les liens dans les articles (Standard WordPress) + $links = $xpath->query('//article[contains(@class, "post")]//a/@href'); + + if ($links->length === 0) { + error_log("DVDCover: Aucun article trouvé pour '$title' (structure changée ?)"); return null; } - $result = [ - 'poster' => '', - 'title' => '', - 'format' => $format, - ]; - - // DVDCover est un site 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); - - if (!empty($articles[1])) { - foreach ($articles[1] as $coverPage) { - if (strpos($coverPage, 'http') !== 0) { - $coverPage = 'https://www.dvdcover.com' . $coverPage; - } + foreach ($links as $link) { + $coverPage = $link->nodeValue; + + $coverHtml = httpGet($coverPage, 10, $ua); + if (!$coverHtml) continue; + + $domFiche = new DOMDocument(); + @$domFiche->loadHTML($coverHtml); + $xpathFiche = new DOMXPath($domFiche); + + // On cherche toutes les images dans la zone de contenu + // WordPress utilise souvent la classe 'entry-content' ou 'post-content' + $imgs = $xpathFiche->query('//div[contains(@class, "entry-content")]//img | //article//img'); + + foreach ($imgs as $img) { + $src = $img->getAttribute('src'); - $coverHtml = httpGet($coverPage, 8, $ua); - if (!$coverHtml) continue; - - // Extraire le titre du cover - $coverTitle = ''; - if (preg_match('/]*class=["\'][^"\']*entry-title[^"\']*["\'][^>]*>([^<]+)<\/h1>/i', $coverHtml, $titleMatch)) { - $coverTitle = trim(strip_tags($titleMatch[1])); - } elseif (preg_match('/]*>([^<]+)<\/title>/i', $coverHtml, $titleMatch)) { - $coverTitle = trim(strip_tags($titleMatch[1])); - } - - // Vérifier que le titre correspond au film recherché - $coverTitleLower = strtolower($coverTitle); - $searchTitleLower = strtolower($cleanTitle); - - // Score de correspondance - $score = 0; - if (strpos($coverTitleLower, $searchTitleLower) !== false) { - $score += 50; - } - if (!empty($year) && strpos($coverTitle, $year) !== false) { - $score += 30; - } - if (stripos($coverTitle, $format) !== false) { - $score += 20; - } - - // Si le score est trop bas, on ignore ce résultat - if ($score < 30) { - continue; - } - - // Chercher l'image principale dans le contenu - $poster = ''; - - // Méthode 1 : Image dans entry-content - if (preg_match('/]*class=["\'][^"\']*entry-content[^"\']*["\'][^>]*>.*?]+src=["\']([^"\']+)["\'][^>]*>/is', $coverHtml, $imgMatch)) { - $poster = $imgMatch[1]; - } - - // Méthode 2 : Image dans figure - if (empty($poster) && preg_match('/]*>.*?]+src=["\']([^"\']+)["\'][^>]*>/is', $coverHtml, $imgMatch)) { - $poster = $imgMatch[1]; - } - - // Méthode 3 : Chercher toutes les images et prendre la première 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) { - // Exclure les images de thème, logos, etc. - 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 && - strpos($img, '150x150') === false) { - $poster = $img; - break; - } - } - } - } - - // Si on a une image valide, on la garde - if (!empty($poster)) { - $result['poster'] = $poster; - $result['title'] = $coverTitle; - break; + // On filtre les images : on veut une jaquette, pas un logo ou une vignette 150x150 + if (strpos($src, 'wp-content/uploads') !== false && + strpos($src, 'logo') === false && + strpos($src, 'icon') === false && + strpos($src, '150x150') === false) { + + return [ + 'poster' => $src, + 'title' => $cleanTitle, + 'format' => $format + ]; } } } - - return (!empty($result['poster'])) ? $result : null; + return null; } // ── API TMDB (uniquement pour les critiques) ──