Actualiser api.php

This commit is contained in:
2026-06-25 12:56:18 +02:00
parent 68a9b184f7
commit 6fab31b749
+105 -53
View File
@@ -115,19 +115,33 @@ function extractYear($dateStr) {
return ''; return '';
} }
// ── NOUVEAU SCRAPER : DVDFr (Refonte 2026) ── // ── NOUVEAU SCRAPER : DVDFr (Version corrigée avec débogage) ──
function fetchDVDFrCover($title, $year = '', $format = 'bluray') { function fetchDVDFrCover($title, $year = '', $format = 'bluray') {
if (empty($title)) return null; 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'; $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); $cleanTitle = cleanTitle($title);
// 1. Simuler une recherche sur le nouveau site DVDFr // Essayer différentes URLs de recherche
$searchUrl = "https://www.dvdfr.com/search/search.php?titre=" . urlencode($cleanTitle); $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) { 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; return null;
} }
@@ -135,65 +149,103 @@ function fetchDVDFrCover($title, $year = '', $format = 'bluray') {
'poster' => '', 'poster' => '',
'title' => '', 'title' => '',
'format' => $format, 'format' => $format,
'debug_url' => $usedUrl,
]; ];
// 2. Parser le HTML de la recherche // Méthode 1 : Chercher les liens vers les fiches produits (pattern /dvd/f ou /dvd/ suivi de chiffres)
$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")]');
$ficheUrl = ''; $ficheUrl = '';
if (preg_match('/href=["\']([^"\']*\/dvd\/f?\d+[^"\']*\.html)["\']/i', $html, $matches)) {
if ($links->length > 0) { $ficheUrl = $matches[1];
foreach ($links as $link) { if (strpos($ficheUrl, 'http') !== 0) {
$href = $link->getAttribute('href'); $ficheUrl = 'https://www.dvdfr.com' . $ficheUrl;
$ficheUrl = strpos($href, 'http') === 0 ? $href : "https://www.dvdfr.com" . (strpos($href, '/') === 0 ? '' : '/') . ltrim($href, '/');
break;
} }
} }
// Si la recherche nous a redirigé directement sur la fiche du film (1 seul résultat) // Méthode 2 : Chercher dans les données JSON-LD ou scripts
if (empty($ficheUrl) && stripos($html, 'jaquette') !== false) { if (empty($ficheUrl) && preg_match('/"url"\s*:\s*"([^"]*\/dvd\/[^"]+)"/i', $html, $matches)) {
$ficheHtml = $html; $ficheUrl = $matches[1];
} elseif (!empty($ficheUrl)) { if (strpos($ficheUrl, 'http') !== 0) {
// Sinon on charge la page de la fiche $ficheUrl = 'https://www.dvdfr.com' . $ficheUrl;
$ficheHtml = httpGet($ficheUrl, 8, $ua); }
} else {
return null;
} }
// 4. Extraire la jaquette de la fiche produit if (!empty($ficheUrl)) {
if (!empty($ficheHtml)) { error_log("DVDFr: Fiche trouvée - $ficheUrl");
$domFiche = new DOMDocument(); $ficheHtml = httpGet($ficheUrl, 10, $ua);
@$domFiche->loadHTML($ficheHtml);
$xpathFiche = new DOMXPath($domFiche);
// Récupération du titre exact de l'édition if ($ficheHtml) {
$titles = $xpathFiche->query('//h1'); // Extraire le titre
if ($titles->length > 0) { if (preg_match('/<h1[^>]*>([^<]+)<\/h1>/i', $ficheHtml, $titleMatch)) {
$result['title'] = trim(strip_tags($titles->item(0)->nodeValue)); $result['title'] = trim(strip_tags($titleMatch[1]));
} } elseif (preg_match('/<title[^>]*>([^<]+)<\/title>/i', $ficheHtml, $titleMatch)) {
$result['title'] = trim(strip_tags($titleMatch[1]));
// On cherche une image qui contient cover, jaquette, ou dans le répertoire des éditions }
$images = $xpathFiche->query('//img');
foreach ($images as $img) { // Chercher les images de jaquettes
$src = $img->getAttribute('src'); // Pattern 1 : Images dans /images/dvd/ ou /covers/
if (preg_match('/cover|jaquette|front|\/images\/dvd\//i', $src)) { if (preg_match_all('/src=["\']([^"\']*(?:\/images\/dvd\/|\/covers\/|cover|jaquette)[^"\']*\.(?:jpg|jpeg|png|webp))["\']/i', $ficheHtml, $imgMatches)) {
// On exclut les miniatures techniques ou de design foreach ($imgMatches[1] as $img) {
if (strpos($src, 'logo') === false && strpos($src, 'icon') === false && strpos($src, 'stars') === false) { if (strpos($img, 'logo') === false &&
strpos($img, 'icon') === false &&
$imgUrl = strpos($src, 'http') === 0 ? $src : "https://www.dvdfr.com" . (strpos($src, '/') === 0 ? '' : '/') . ltrim($src, '/'); strpos($img, 'stars') === false &&
strpos($img, 'bg-') === false) {
// Transformation de l'URL pour choper la haute résolution (retirer le "small" ou "medium")
$imgUrl = preg_replace('/_s\./i', '.', $imgUrl); $imgUrl = strpos($img, 'http') === 0 ? $img : 'https://www.dvdfr.com' . (strpos($img, '/') === 0 ? '' : '/') . ltrim($img, '/');
$imgUrl = preg_replace('/_m\./i', '.', $imgUrl);
// Supprimer les suffixes de taille pour avoir la HD
$result['poster'] = $imgUrl; $imgUrl = preg_replace('/[_-](?:small|medium|thumb|mini|s|m|t)\./i', '.', $imgUrl);
break;
$result['poster'] = $imgUrl;
break;
}
} }
} }
// Pattern 2 : Meta Open Graph
if (empty($result['poster']) && preg_match('/<meta[^>]+property=["\']og:image["\'][^>]+content=["\']([^"\']+)["\']/i', $ficheHtml, $ogMatch)) {
$result['poster'] = $ogMatch[1];
}
// Pattern 3 : Chercher toutes les images et filtrer
if (empty($result['poster'])) {
preg_match_all('/<img[^>]+src=["\']([^"\']+\.(?:jpg|jpeg|png|webp))["\'][^>]*>/i', $ficheHtml, $allImages);
foreach ($allImages[1] as $img) {
$imgLower = strtolower($img);
if (strpos($imgLower, 'dvdfr.com') !== false &&
strpos($imgLower, 'logo') === false &&
strpos($imgLower, 'icon') === false &&
strpos($imgLower, 'banner') === false &&
strpos($imgLower, 'bg') === false &&
strpos($imgLower, 'button') === false &&
strpos($imgLower, 'social') === false) {
$imgUrl = strpos($img, 'http') === 0 ? $img : 'https://www.dvdfr.com' . (strpos($img, '/') === 0 ? '' : '/') . ltrim($img, '/');
$imgUrl = preg_replace('/[_-](?:small|medium|thumb|mini|s|m|t)\./i', '.', $imgUrl);
$result['poster'] = $imgUrl;
break;
}
}
}
}
}
// Fallback : chercher directement dans la page de recherche
if (empty($result['poster'])) {
preg_match_all('/src=["\']([^"\']+\.(?:jpg|jpeg|png|webp))["\']/i', $html, $searchImages);
foreach ($searchImages[1] as $img) {
$imgLower = strtolower($img);
if (strpos($imgLower, 'dvdfr.com') !== false &&
(strpos($imgLower, 'dvd') !== false || strpos($imgLower, 'cover') !== false) &&
strpos($imgLower, 'logo') === false &&
strpos($imgLower, 'icon') === false) {
$imgUrl = strpos($img, 'http') === 0 ? $img : 'https://www.dvdfr.com' . (strpos($img, '/') === 0 ? '' : '/') . ltrim($img, '/');
$imgUrl = preg_replace('/[_-](?:small|medium|thumb|mini|s|m|t)\./i', '.', $imgUrl);
$result['poster'] = $imgUrl;
break;
}
} }
} }