Actualiser api.php

This commit is contained in:
2026-06-25 13:23:02 +02:00
parent ec5a26f78b
commit 974c756790
+56 -26
View File
@@ -120,8 +120,6 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') {
$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);
// On encode proprement pour l'URL
$searchUrl = "https://www.dvdcover.com/?s=" . urlencode($cleanTitle); $searchUrl = "https://www.dvdcover.com/?s=" . urlencode($cleanTitle);
$html = httpGet($searchUrl, 10, $ua); $html = httpGet($searchUrl, 10, $ua);
@@ -130,54 +128,86 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') {
return null; 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 = new DOMDocument();
@$dom->loadHTML($html); @$dom->loadHTML($html);
$xpath = new DOMXPath($dom); $xpath = new DOMXPath($dom);
// On cherche les liens dans les articles (Standard WordPress) // Les résultats sont dans des <h2><a>
$links = $xpath->query('//article[contains(@class, "post")]//a/@href'); $links = $xpath->query('//h2//a');
if ($links->length === 0) { if ($links->length === 0) {
error_log("DVDCover: Aucun article trouvé pour '$title' (structure changée ?)"); error_log("DVDCover: Aucun lien H2 trouvé pour '$title'");
return null; return null;
} }
foreach ($links as $link) { // On cherche le meilleur lien : correspondance titre + année si possible
$coverPage = $link->nodeValue; $bestUrl = null;
$cleanTitleLower = strtolower($cleanTitle);
$coverHtml = httpGet($coverPage, 10, $ua); foreach ($links as $a) {
if (!$coverHtml) continue; $text = strtolower(trim($a->textContent));
$href = $a->getAttribute('href');
// Priorité : correspond au titre ET à l'année
if (!empty($year) && strpos($text, strtolower($cleanTitleLower)) !== false && strpos($text, $year) !== false) {
$bestUrl = $href;
break;
}
// Fallback : correspond au titre seul (premier trouvé)
if ($bestUrl === null && strpos($text, $cleanTitleLower) !== false) {
$bestUrl = $href;
}
}
// Dernier fallback : premier lien de la liste
if (!$bestUrl) {
$bestUrl = $links->item(0)->getAttribute('href');
}
if (!$bestUrl) return null;
// On scrape la page de la fiche
$coverHtml = httpGet($bestUrl, 10, $ua);
if (!$coverHtml) return null;
$domFiche = new DOMDocument(); $domFiche = new DOMDocument();
@$domFiche->loadHTML($coverHtml); @$domFiche->loadHTML($coverHtml);
$xpathFiche = new DOMXPath($domFiche); $xpathFiche = new DOMXPath($domFiche);
// On cherche toutes les images dans la zone de contenu // On cherche la plus grande image dans wp-content/uploads (pas les thumbnails -300x)
// WordPress utilise souvent la classe 'entry-content' ou 'post-content' $imgs = $xpathFiche->query('//img[contains(@src,"wp-content/uploads")]');
$imgs = $xpathFiche->query('//div[contains(@class, "entry-content")]//img | //article//img'); $bestSrc = null;
$bestSize = 0;
foreach ($imgs as $img) { foreach ($imgs as $img) {
$src = $img->getAttribute('src'); $src = $img->getAttribute('src');
// On filtre les images : on veut une jaquette, pas un logo ou une vignette 150x150 // Ignorer logos, icônes et thumbnails redimensionnés
if (strpos($src, 'wp-content/uploads') !== false && if (strpos($src, 'logo') !== false) continue;
strpos($src, 'logo') === false && if (strpos($src, 'icon') !== false) continue;
strpos($src, 'icon') === false &&
strpos($src, '150x150') === false) { // Préférer les images sans suffixe de redimensionnement (ex: -300x201)
$isThumb = preg_match('/-\d+x\d+\./', $src);
// Tenter de récupérer la version pleine taille (supprimer le suffixe -WxH)
$fullSrc = preg_replace('/-\d+x\d+(\.\w+)$/', '$1', $src);
// Scorer : pleine taille > thumbnail
$score = $isThumb ? 1 : 10;
if ($score > $bestSize) {
$bestSize = $score;
$bestSrc = $fullSrc;
}
}
if (!$bestSrc) return null;
return [ return [
'poster' => $src, 'poster' => $bestSrc,
'title' => $cleanTitle, 'title' => $cleanTitle,
'format' => $format 'format' => $format
]; ];
} }
}
}
return null;
}
// ── API TMDB (uniquement pour les critiques) ── // ── API TMDB (uniquement pour les critiques) ──
function fetchTMDBFull($title, $year, $apiKey, $pdo) { function fetchTMDBFull($title, $year, $apiKey, $pdo) {