diff --git a/api.php b/api.php index 98062e4..062efe0 100644 --- a/api.php +++ b/api.php @@ -115,19 +115,33 @@ function extractYear($dateStr) { return ''; } -// ── NOUVEAU SCRAPER : DVDFr (Refonte 2026) ── +// ── NOUVEAU SCRAPER : DVDFr (Version corrigée avec débogage) ── function fetchDVDFrCover($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); - // 1. Simuler une recherche sur le nouveau site DVDFr - $searchUrl = "https://www.dvdfr.com/search/search.php?titre=" . urlencode($cleanTitle); + // Essayer différentes URLs de recherche + $searchUrls = [ + "https://www.dvdfr.com/search?q=" . urlencode($cleanTitle), + "https://www.dvdfr.com/dvd/recherche.html?search=" . urlencode($cleanTitle), + "https://www.dvdfr.com/catalogue/recherche?query=" . urlencode($cleanTitle), + ]; + + $html = null; + $usedUrl = ''; + + foreach ($searchUrls as $url) { + $html = httpGet($url, 10, $ua); + if ($html && strlen($html) > 1000) { + $usedUrl = $url; + break; + } + } - $html = httpGet($searchUrl, 8, $ua); if (!$html) { - error_log("DVDFr Scraper: Échec de connexion pour '$title'"); + error_log("DVDFr: Impossible de récupérer la page de recherche pour '$title'"); return null; } @@ -135,65 +149,103 @@ function fetchDVDFrCover($title, $year = '', $format = 'bluray') { 'poster' => '', 'title' => '', 'format' => $format, + 'debug_url' => $usedUrl, ]; - // 2. Parser le HTML de la recherche - $dom = new DOMDocument(); - @$dom->loadHTML($html); - $xpath = new DOMXPath($dom); - - // 3. Chercher le lien vers la fiche de l'édition (les liens DVDFr commencent souvent par /dvd/f) - $links = $xpath->query('//a[contains(@href, "/dvd/f")]'); + // Méthode 1 : Chercher les liens vers les fiches produits (pattern /dvd/f ou /dvd/ suivi de chiffres) $ficheUrl = ''; - - if ($links->length > 0) { - foreach ($links as $link) { - $href = $link->getAttribute('href'); - $ficheUrl = strpos($href, 'http') === 0 ? $href : "https://www.dvdfr.com" . (strpos($href, '/') === 0 ? '' : '/') . ltrim($href, '/'); - break; + if (preg_match('/href=["\']([^"\']*\/dvd\/f?\d+[^"\']*\.html)["\']/i', $html, $matches)) { + $ficheUrl = $matches[1]; + if (strpos($ficheUrl, 'http') !== 0) { + $ficheUrl = 'https://www.dvdfr.com' . $ficheUrl; } } - // Si la recherche nous a redirigé directement sur la fiche du film (1 seul résultat) - if (empty($ficheUrl) && stripos($html, 'jaquette') !== false) { - $ficheHtml = $html; - } elseif (!empty($ficheUrl)) { - // Sinon on charge la page de la fiche - $ficheHtml = httpGet($ficheUrl, 8, $ua); - } else { - return null; + // Méthode 2 : Chercher dans les données JSON-LD ou scripts + if (empty($ficheUrl) && preg_match('/"url"\s*:\s*"([^"]*\/dvd\/[^"]+)"/i', $html, $matches)) { + $ficheUrl = $matches[1]; + if (strpos($ficheUrl, 'http') !== 0) { + $ficheUrl = 'https://www.dvdfr.com' . $ficheUrl; + } } - // 4. Extraire la jaquette de la fiche produit - if (!empty($ficheHtml)) { - $domFiche = new DOMDocument(); - @$domFiche->loadHTML($ficheHtml); - $xpathFiche = new DOMXPath($domFiche); + if (!empty($ficheUrl)) { + error_log("DVDFr: Fiche trouvée - $ficheUrl"); + $ficheHtml = httpGet($ficheUrl, 10, $ua); - // Récupération du titre exact de l'édition - $titles = $xpathFiche->query('//h1'); - if ($titles->length > 0) { - $result['title'] = trim(strip_tags($titles->item(0)->nodeValue)); - } - - // On cherche une image qui contient cover, jaquette, ou dans le répertoire des éditions - $images = $xpathFiche->query('//img'); - foreach ($images as $img) { - $src = $img->getAttribute('src'); - if (preg_match('/cover|jaquette|front|\/images\/dvd\//i', $src)) { - // On exclut les miniatures techniques ou de design - if (strpos($src, 'logo') === false && strpos($src, 'icon') === false && strpos($src, 'stars') === false) { - - $imgUrl = strpos($src, 'http') === 0 ? $src : "https://www.dvdfr.com" . (strpos($src, '/') === 0 ? '' : '/') . ltrim($src, '/'); - - // Transformation de l'URL pour choper la haute résolution (retirer le "small" ou "medium") - $imgUrl = preg_replace('/_s\./i', '.', $imgUrl); - $imgUrl = preg_replace('/_m\./i', '.', $imgUrl); - - $result['poster'] = $imgUrl; - break; + if ($ficheHtml) { + // Extraire le titre + if (preg_match('/