Actualiser api.php
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
$result = [
|
||||
'poster' => '',
|
||||
'title' => '',
|
||||
'format' => $format,
|
||||
];
|
||||
// DEBUG : Sauvegarde le contenu pour vérifier si on est bloqué
|
||||
file_put_contents(__DIR__ . '/debug_scrap.html', $html);
|
||||
|
||||
// DVDCover est un site WordPress - les articles ont la classe "post"
|
||||
// Chercher les liens vers les pages individuelles de covers
|
||||
preg_match_all('/<article[^>]*class=["\'][^"\']*post[^"\']*["\'][^>]*>.*?<a[^>]+href=["\']([^"\']+)["\'][^>]*>.*?<\/article>/is', $html, $articles);
|
||||
$dom = new DOMDocument();
|
||||
@$dom->loadHTML($html);
|
||||
$xpath = new DOMXPath($dom);
|
||||
|
||||
if (!empty($articles[1])) {
|
||||
foreach ($articles[1] as $coverPage) {
|
||||
if (strpos($coverPage, 'http') !== 0) {
|
||||
$coverPage = 'https://www.dvdcover.com' . $coverPage;
|
||||
// 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;
|
||||
}
|
||||
|
||||
$coverHtml = httpGet($coverPage, 8, $ua);
|
||||
foreach ($links as $link) {
|
||||
$coverPage = $link->nodeValue;
|
||||
|
||||
$coverHtml = httpGet($coverPage, 10, $ua);
|
||||
if (!$coverHtml) continue;
|
||||
|
||||
// Extraire le titre du cover
|
||||
$coverTitle = '';
|
||||
if (preg_match('/<h1[^>]*class=["\'][^"\']*entry-title[^"\']*["\'][^>]*>([^<]+)<\/h1>/i', $coverHtml, $titleMatch)) {
|
||||
$coverTitle = trim(strip_tags($titleMatch[1]));
|
||||
} elseif (preg_match('/<title[^>]*>([^<]+)<\/title>/i', $coverHtml, $titleMatch)) {
|
||||
$coverTitle = trim(strip_tags($titleMatch[1]));
|
||||
}
|
||||
$domFiche = new DOMDocument();
|
||||
@$domFiche->loadHTML($coverHtml);
|
||||
$xpathFiche = new DOMXPath($domFiche);
|
||||
|
||||
// Vérifier que le titre correspond au film recherché
|
||||
$coverTitleLower = strtolower($coverTitle);
|
||||
$searchTitleLower = strtolower($cleanTitle);
|
||||
// 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');
|
||||
|
||||
// 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;
|
||||
}
|
||||
foreach ($imgs as $img) {
|
||||
$src = $img->getAttribute('src');
|
||||
|
||||
// Si le score est trop bas, on ignore ce résultat
|
||||
if ($score < 30) {
|
||||
continue;
|
||||
}
|
||||
// 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) {
|
||||
|
||||
// Chercher l'image principale dans le contenu
|
||||
$poster = '';
|
||||
|
||||
// Méthode 1 : Image dans entry-content
|
||||
if (preg_match('/<div[^>]*class=["\'][^"\']*entry-content[^"\']*["\'][^>]*>.*?<img[^>]+src=["\']([^"\']+)["\'][^>]*>/is', $coverHtml, $imgMatch)) {
|
||||
$poster = $imgMatch[1];
|
||||
}
|
||||
|
||||
// Méthode 2 : Image dans figure
|
||||
if (empty($poster) && preg_match('/<figure[^>]*>.*?<img[^>]+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('/<img[^>]+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;
|
||||
return [
|
||||
'poster' => $src,
|
||||
'title' => $cleanTitle,
|
||||
'format' => $format
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Si on a une image valide, on la garde
|
||||
if (!empty($poster)) {
|
||||
$result['poster'] = $poster;
|
||||
$result['title'] = $coverTitle;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (!empty($result['poster'])) ? $result : null;
|
||||
return null;
|
||||
}
|
||||
|
||||
// ── API TMDB (uniquement pour les critiques) ──
|
||||
|
||||
Reference in New Issue
Block a user