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 '';
}
// ── 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('/<h1[^>]*>([^<]+)<\/h1>/i', $ficheHtml, $titleMatch)) {
$result['title'] = trim(strip_tags($titleMatch[1]));
} elseif (preg_match('/<title[^>]*>([^<]+)<\/title>/i', $ficheHtml, $titleMatch)) {
$result['title'] = trim(strip_tags($titleMatch[1]));
}
// Chercher les images de jaquettes
// Pattern 1 : Images dans /images/dvd/ ou /covers/
if (preg_match_all('/src=["\']([^"\']*(?:\/images\/dvd\/|\/covers\/|cover|jaquette)[^"\']*\.(?:jpg|jpeg|png|webp))["\']/i', $ficheHtml, $imgMatches)) {
foreach ($imgMatches[1] as $img) {
if (strpos($img, 'logo') === false &&
strpos($img, 'icon') === false &&
strpos($img, 'stars') === false &&
strpos($img, 'bg-') === false) {
$imgUrl = strpos($img, 'http') === 0 ? $img : 'https://www.dvdfr.com' . (strpos($img, '/') === 0 ? '' : '/') . ltrim($img, '/');
// Supprimer les suffixes de taille pour avoir la HD
$imgUrl = preg_replace('/[_-](?:small|medium|thumb|mini|s|m|t)\./i', '.', $imgUrl);
$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;
}
}
}