Actualiser api.php

This commit is contained in:
2026-06-25 11:49:11 +02:00
parent 40068e93c3
commit 37c5fa1b48
+65 -46
View File
@@ -115,7 +115,7 @@ function extractYear($dateStr) {
return ''; return '';
} }
// ── DVDcover.com (version corrigée) ── // ── DVDcover.com (scraping précis) ──
function fetchDVDCover($title, $year = '', $format = 'bluray') { function fetchDVDCover($title, $year = '', $format = 'bluray') {
if (empty($title)) return null; if (empty($title)) return null;
@@ -124,8 +124,8 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') {
// Mapping des formats // Mapping des formats
$formatMap = [ $formatMap = [
'4k ultra hd' => '4k', '4k ultra hd' => '4k-ultra-hd',
'4k' => '4k', '4k' => '4k-ultra-hd',
'blu-ray' => 'blu-ray', 'blu-ray' => 'blu-ray',
'bluray' => 'blu-ray', 'bluray' => 'blu-ray',
'dvd' => 'dvd', 'dvd' => 'dvd',
@@ -133,14 +133,10 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') {
$dcFormat = $formatMap[strtolower($format)] ?? 'blu-ray'; $dcFormat = $formatMap[strtolower($format)] ?? 'blu-ray';
// Construction URL de recherche // URL de recherche DVDcover
$searchPath = str_replace(' ', '-', strtolower($cleanTitle)); $searchUrl = "https://www.dvdcover.com/?s=" . urlencode($cleanTitle);
$searchPath = preg_replace('/[^a-z0-9-]/', '', $searchPath);
$searchUrl = "https://www.dvdcover.com/{$dcFormat}/search/{$searchPath}";
$html = httpGet($searchUrl, 8, $ua); $html = httpGet($searchUrl, 8, $ua);
if (!$html) { if (!$html) {
error_log("DVDCover: Échec recherche pour '$title'"); error_log("DVDCover: Échec recherche pour '$title'");
return null; return null;
@@ -152,59 +148,82 @@ function fetchDVDCover($title, $year = '', $format = 'bluray') {
'format' => $format, 'format' => $format,
]; ];
// Chercher les liens vers les pages de covers individuelles // DVDcover utilise WordPress - les articles ont la classe "post"
// DVDcover utilise des URLs comme /covers/title-year-format/ // Chercher les liens vers les pages individuelles de covers
if (preg_match_all('/href=["\']([^"\']*\/covers?\/[^"\']+\/[^"\']+)["\']/i', $html, $coverLinks)) { preg_match_all('/<article[^>]*class=["\'][^"\']*post[^"\']*["\'][^>]*>.*?<a[^>]+href=["\']([^"\']+)["\'][^>]*>.*?<\/article>/is', $html, $articles);
foreach ($coverLinks[1] as $coverPage) {
if (!empty($articles[1])) {
// Parcourir les résultats
foreach ($articles[1] as $coverPage) {
if (strpos($coverPage, 'http') !== 0) { if (strpos($coverPage, 'http') !== 0) {
$coverPage = 'https://www.dvdcover.com' . $coverPage; $coverPage = 'https://www.dvdcover.com' . $coverPage;
} }
$coverHtml = httpGet($coverPage, 8, $ua); $coverHtml = httpGet($coverPage, 8, $ua);
if ($coverHtml) { if (!$coverHtml) continue;
// Chercher les images dans /wp-content/uploads/ (vraies jaquettes)
// Ignorer les images de thème (/wp-content/themes/) // Méthode 1 : Chercher l'image principale dans le contenu de l'article
if (preg_match_all('/<img[^>]+src=["\']([^"\']*\/wp-content\/uploads\/[^"\']+\.jpg)["\'][^>]*>/i', $coverHtml, $imgMatches)) { // DVDcover met les jaquettes dans <figure> ou <div class="entry-content">
foreach ($imgMatches[1] as $img) { if (preg_match('/<figure[^>]*>.*?<img[^>]+src=["\']([^"\']+)["\'][^>]*>/is', $coverHtml, $m)) {
// Filtrer pour ne garder que les images de covers (pas les logos, etc.) $img = $m[1];
if (strpos($img, '/covers/') !== false || // Vérifier que c'est bien une jaquette (pas une image de thème)
strpos($img, '/bluray/') !== false || if (strpos($img, '/wp-content/uploads/') !== false &&
strpos($img, '/dvd/') !== false || strpos($img, '/wp-content/themes/') === false &&
strpos($img, '/4k/') !== false) { (strpos($img, '.jpg') !== false || strpos($img, '.jpeg') !== false || strpos($img, '.webp') !== false)) {
$result['poster'] = $img;
}
}
// Méthode 2 : Chercher dans entry-content
if (empty($result['poster']) && preg_match('/<div[^>]*class=["\'][^"\']*entry-content[^"\']*["\'][^>]*>.*?<img[^>]+src=["\']([^"\']+)["\'][^>]*>/is', $coverHtml, $m)) {
$img = $m[1];
if (strpos($img, '/wp-content/uploads/') !== false &&
strpos($img, '/wp-content/themes/') === false) {
$result['poster'] = $img;
}
}
// Méthode 3 : Chercher toutes les images et prendre la plus grande
if (empty($result['poster'])) {
preg_match_all('/<img[^>]+src=["\']([^"\']+\/wp-content\/uploads\/[^"\']+\.(?:jpg|jpeg|webp))["\'][^>]*>/i', $coverHtml, $allImages);
if (!empty($allImages[1])) {
// Prendre la première image qui n'est pas un logo ou icône
foreach ($allImages[1] as $img) {
if (strpos($img, 'logo') === false &&
strpos($img, 'icon') === false &&
strpos($img, 'banner') === false &&
strpos($img, 'bg') === false &&
strpos($img, 'button') === false) {
$result['poster'] = $img; $result['poster'] = $img;
break 2; break;
} }
} }
} }
}
// Fallback : chercher dans les balises meta og:image // Extraire le titre
if (empty($result['poster']) && preg_match('/<meta[^>]+property=["\']og:image["\'][^>]+content=["\']([^"\']+)["\']/i', $coverHtml, $m)) { if (preg_match('/<h1[^>]*class=["\'][^"\']*entry-title[^"\']*["\'][^>]*>([^<]+)<\/h1>/i', $coverHtml, $titleMatch)) {
$ogImage = $m[1]; $result['title'] = trim(strip_tags($titleMatch[1]));
// Vérifier que ce n'est pas une image de thème } elseif (preg_match('/<title[^>]*>([^<]+)<\/title>/i', $coverHtml, $titleMatch)) {
if (strpos($ogImage, '/wp-content/uploads/') !== false) { $result['title'] = trim(strip_tags($titleMatch[1]));
$result['poster'] = $ogImage; }
}
}
// Extraire le titre // Si on a trouvé une image, on arrête
if (empty($result['title']) && preg_match('/<title[^>]*>([^<]+) - DVDCover/i', $coverHtml, $titleMatch)) { if (!empty($result['poster'])) {
$result['title'] = trim($titleMatch[1]); break;
}
if (!empty($result['poster'])) {
break;
}
} }
} }
} }
// Fallback ultime : chercher directement les images uploads dans la page de recherche // Fallback : chercher directement dans la page de résultats
if (empty($result['poster'])) { if (empty($result['poster'])) {
if (preg_match_all('/<img[^>]+src=["\']([^"\']*\/wp-content\/uploads\/[^"\']+\.jpg)["\'][^>]*>/i', $html, $imgMatches)) { preg_match_all('/<img[^>]+src=["\']([^"\']+\/wp-content\/uploads\/[^"\']+\.(?:jpg|jpeg|webp))["\'][^>]*>/i', $html, $allImages);
foreach ($imgMatches[1] as $img) { if (!empty($allImages[1])) {
if (strpos($img, '/covers/') !== false || foreach ($allImages[1] as $img) {
strpos($img, '/bluray/') !== false || if (strpos($img, 'logo') === false &&
strpos($img, '/dvd/') !== false) { strpos($img, 'icon') === false &&
strpos($img, 'banner') === false &&
strpos($img, 'bg') === false) {
$result['poster'] = $img; $result['poster'] = $img;
break; break;
} }