Actualiser api.php

This commit is contained in:
2026-06-24 15:32:44 +02:00
parent dc756b4ce0
commit be124954e5
+141 -129
View File
@@ -105,92 +105,89 @@ function extractYear($dateStr) {
return ''; return '';
} }
// ── API DVDFr (SANS CACHE - Scraping HTML) ── // ── API DVDFr (réécriture complète) ──
function fetchDVDFr($ean, $pdo) { function fetchDVDFr($ean, $pdo) {
if (empty($ean) || strlen($ean) < 8) return null; if (empty($ean) || strlen((string)$ean) < 8) return null;
// 🔥 SUPPRESSION DES APPELS À getCache() QUI N'EXISTE PLUS $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36';
$baseHeaders = [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7',
'Accept-Encoding: gzip, deflate, br',
'Connection: keep-alive',
];
$ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'; // ── Helpers internes ──
$curlGet = function(string $url) use ($ua, $baseHeaders): ?string {
// Étape 1 : Recherche via le site DVDfr (page HTML) $ch = curl_init($url);
$searchUrl = "https://www.dvdfr.com/search/?q=" . urlencode($ean);
$ch = curl_init($searchUrl);
curl_setopt_array($ch, [ curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 8, CURLOPT_TIMEOUT => 10,
CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => $ua, CURLOPT_USERAGENT => $ua,
CURLOPT_FOLLOWLOCATION => true, CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => [ CURLOPT_MAXREDIRS => 5,
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', CURLOPT_ENCODING => '', // décompression auto (gzip, br…)
'Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7', CURLOPT_HTTPHEADER => $baseHeaders,
], CURLOPT_COOKIEFILE => '', // active le jar de cookies en mémoire
]); ]);
$html = curl_exec($ch); $body = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); curl_close($ch);
if (!$body || $code !== 200) {
error_log("DVDFr curlGet: HTTP $code pour $url");
return null;
}
return $body;
};
if (!$html || $httpCode !== 200) { $absoluteUrl = function(string $src): string {
error_log("DVDFr: Échec recherche HTML - HTTP $httpCode"); if (strpos($src, 'http') === 0) return $src;
if (strpos($src, '//') === 0) return 'https:' . $src;
if (strpos($src, '/') === 0) return 'https://www.dvdfr.com' . $src;
return 'https://www.dvdfr.com/' . $src;
};
// ── ÉTAPE 1 : trouver l'URL de la fiche via la recherche ──
$searchHtml = $curlGet('https://www.dvdfr.com/search/?q=' . urlencode($ean));
if (!$searchHtml) {
error_log("DVDFr: échec de la page de recherche pour EAN $ean");
return null; return null;
} }
// Étape 2 : Extraire le lien vers la fiche du film // Patterns par ordre de priorité
$dvdUrl = null; $ficheUrl = null;
$patterns = [
if (preg_match('/<a[^>]+href=["\']([^"\']+\.html)["\'][^>]*class=["\'][^"\']*result[^"\']*["\'][^>]*>/i', $html, $matches)) { // lien direct vers une fiche /dvd/ ou /blu-ray/ contenant l'EAN dans l'URL
$dvdUrl = $matches[1]; '@href=["\']([^"\']*(?:dvd|blu-ray|4k|vhs|cd|coffret)[^"\']+\.html)["\']@i',
if (strpos($dvdUrl, 'http') !== 0) { // toute fiche .html dans le domaine dvdfr.com
$dvdUrl = 'https://www.dvdfr.com' . $dvdUrl; '@href=["\'](?:https?://(?:www\.)?dvdfr\.com)?(/[^"\']+\.html)["\']@i',
];
foreach ($patterns as $pattern) {
if (preg_match($pattern, $searchHtml, $m)) {
$ficheUrl = $absoluteUrl($m[1]);
break;
} }
} }
if (!$dvdUrl && preg_match('/href=["\']([^"\']*' . preg_quote($ean, '/') . '[^"\']*\.html)["\']/i', $html, $matches)) { if (!$ficheUrl) {
$dvdUrl = $matches[1]; error_log("DVDFr: aucune fiche trouvée pour EAN $ean");
if (strpos($dvdUrl, 'http') !== 0) { return null;
$dvdUrl = 'https://www.dvdfr.com' . $dvdUrl;
}
} }
error_log("DVDFr: fiche → $ficheUrl");
if (!$dvdUrl && preg_match('/<a[^>]+href=["\'](https:\/\/www\.dvdfr\.com\/(?:dvd|blu-ray)\/[^"\']+\.html)["\']/i', $html, $matches)) { // ── ÉTAPE 2 : charger la fiche ──
$dvdUrl = $matches[1]; $html = $curlGet($ficheUrl);
} if (!$html) {
error_log("DVDFr: impossible de charger la fiche $ficheUrl");
if (!$dvdUrl) {
error_log("DVDFr: Aucune fiche trouvée pour EAN $ean");
return null; return null;
} }
error_log("DVDFr: Fiche trouvée - $dvdUrl"); // ── ÉTAPE 3 : extraction des données ──
// Étape 3 : Récupérer la fiche complète
$ch2 = curl_init($dvdUrl);
curl_setopt_array($ch2, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 8,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => $ua,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: fr-FR,fr;q=0.9',
],
]);
$ficheHtml = curl_exec($ch2);
curl_close($ch2);
if (!$ficheHtml) {
error_log("DVDFr: Impossible de charger la fiche");
return null;
}
// Étape 4 : Extraire les données depuis le HTML
$result = [ $result = [
'poster' => '', 'poster' => '',
'title' => '',
'publisher' => '', 'publisher' => '',
'format' => '', 'format' => '',
'length' => '', 'length' => '',
@@ -198,98 +195,113 @@ $result = [
'discs' => '', 'discs' => '',
]; ];
// 🔥 EXTRACTION ROBUSTE DE L'AFFICHE (plusieurs méthodes fallback) // --- 3a. AFFICHE ---
// Méthode 1 : Chercher toutes les images et filtrer par taille/URL // Priorité 1 : og:image (le plus fiable)
preg_match_all('/<img[^>]+src=["\']([^"\']+)["\'][^>]*>/i', $ficheHtml, $allImages); if (preg_match('/<meta[^>]+property=["\']og:image["\'][^>]+content=["\']([^"\']+)["\']/i', $html, $m) ||
if (!empty($allImages[1])) { preg_match('/<meta[^>]+content=["\']([^"\']+)["\'][^>]+property=["\']og:image["\']/i', $html, $m)) {
foreach ($allImages[1] as $imgUrl) { $result['poster'] = $m[1];
// Filtrer les images qui ressemblent à une jaquette error_log("DVDFr: affiche via og:image → " . $result['poster']);
if (preg_match('/(cover|jaquette|pochette|affiche|poster|front)/i', $imgUrl) || }
preg_match('/\.(jpg|jpeg|png|webp)$/i', $imgUrl)) {
// Vérifier que c'est bien une URL complète // Priorité 2 : JSON-LD
if (strpos($imgUrl, 'http') === 0) { if (empty($result['poster'])) {
$result['poster'] = $imgUrl; preg_match_all('/<script[^>]+type=["\']application\/ld\+json["\'][^>]*>(.*?)<\/script>/is', $html, $scripts);
error_log("DVDFr: Affiche trouvée (méthode 1) - $imgUrl"); foreach ($scripts[1] as $raw) {
break; $json = json_decode($raw, true);
} elseif (strpos($imgUrl, '//') === 0) { if (!$json) continue;
$result['poster'] = 'https:' . $imgUrl; $img = $json['image'] ?? null;
error_log("DVDFr: Affiche trouvée (méthode 1b) - " . $result['poster']); if ($img) {
break; $result['poster'] = is_array($img) ? $img[0] : $img;
} elseif (strpos($imgUrl, '/') === 0) { error_log("DVDFr: affiche via JSON-LD → " . $result['poster']);
$result['poster'] = 'https://www.dvdfr.com' . $imgUrl;
error_log("DVDFr: Affiche trouvée (méthode 1c) - " . $result['poster']);
break; break;
} }
} }
} }
}
// Méthode 2 : Chercher dans les balises meta (Open Graph) // Priorité 3 : image dans le bloc principal de la fiche (div.cover, div.product, etc.)
if (empty($result['poster']) && preg_match('/<meta[^>]+property=["\']og:image["\'][^>]+content=["\']([^"\']+)["\']/i', $ficheHtml, $matches)) { if (empty($result['poster'])) {
$result['poster'] = $matches[1]; // Chercher dans un conteneur typique d'une fiche produit
error_log("DVDFr: Affiche trouvée (méthode 2 - og:image) - " . $result['poster']); $coverBlock = '';
if (preg_match('/<(?:div|figure|section)[^>]+(?:class|id)=["\'][^"\']*(?:cover|product|fiche|jaquette|packshot)[^"\']*["\'][^>]*>(.*?)<\/(?:div|figure|section)>/is', $html, $m)) {
$coverBlock = $m[1];
} }
$searchArea = $coverBlock ?: $html;
// Méthode 3 : Chercher dans les balises link (rel="image_src") preg_match_all('/<img[^>]+(?:src|data-src|data-lazy-src|data-original)=["\']([^"\']+)["\'][^>]*>/i', $searchArea, $imgs);
if (empty($result['poster']) && preg_match('/<link[^>]+rel=["\']image_src["\'][^>]+href=["\']([^"\']+)["\']/i', $ficheHtml, $matches)) { foreach ($imgs[1] as $src) {
$result['poster'] = $matches[1]; // Exclusions : icônes, logos, UI, bannières pub
error_log("DVDFr: Affiche trouvée (méthode 3 - link image_src) - " . $result['poster']); if (preg_match('/(icon|logo|sprite|blank|spacer|pixel|tracking|banner|ad|pub|star|note|flag|arrow|btn|button|nav)/i', $src)) continue;
} // Doit avoir une extension image
if (!preg_match('/\.(jpe?g|png|webp)(\?[^"\']*)?$/i', $src)) continue;
// Méthode 4 : Chercher dans les données JSON-LD (structured data) // Exclure les data-URI
if (empty($result['poster']) && preg_match('/<script[^>]+type=["\']application\/ld\+json["\'][^>]*>([^<]+)<\/script>/i', $ficheHtml, $matches)) { if (strpos($src, 'data:') === 0) continue;
$jsonData = json_decode($matches[1], true); $result['poster'] = $absoluteUrl($src);
if ($jsonData && isset($jsonData['image'])) { error_log("DVDFr: affiche via img scan → " . $result['poster']);
$result['poster'] = is_array($jsonData['image']) ? $jsonData['image'][0] : $jsonData['image']; break;
error_log("DVDFr: Affiche trouvée (méthode 4 - JSON-LD) - " . $result['poster']);
} }
} }
// Extraction de l'éditeur (plusieurs méthodes) // --- 3b. TITRE ---
if (preg_match('/(?:éditeur|distributeur|studio)\s*[:<\/]>\s*([^<]+)/i', $ficheHtml, $matches)) { // og:title
$result['publisher'] = trim(strip_tags($matches[1])); if (preg_match('/<meta[^>]+property=["\']og:title["\'][^>]+content=["\']([^"\']+)["\']/i', $html, $m) ||
} elseif (preg_match('/<span[^>]+class=["\'][^"\']*publisher[^"\']*["\'][^>]*>([^<]+)<\/span>/i', $ficheHtml, $matches)) { preg_match('/<meta[^>]+content=["\']([^"\']+)["\'][^>]+property=["\']og:title["\']/i', $html, $m)) {
$result['publisher'] = trim(strip_tags($matches[1])); $result['title'] = html_entity_decode(trim($m[1]), ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
// fallback <h1>
if (empty($result['title']) && preg_match('/<h1[^>]*>([^<]+)<\/h1>/i', $html, $m)) {
$result['title'] = html_entity_decode(trim(strip_tags($m[1])), ENT_QUOTES | ENT_HTML5, 'UTF-8');
} }
// Extraction du format // --- 3c. MÉTADONNÉES : éditeur, format, durée, ratio, disques ---
if (preg_match('/(4k\s*ultra\s*hd|ultra\s*hd|blu[\s-]?ray|dvd|coffret)/i', $ficheHtml, $matches)) { // Nettoyeur de valeur brute
$format = strtoupper(trim($matches[1])); $clean = fn(string $s): string => trim(html_entity_decode(strip_tags($s), ENT_QUOTES | ENT_HTML5, 'UTF-8'));
if (strpos($format, '4K') !== false || strpos($format, 'ULTRA') !== false) {
// Éditeur
$publisherPatterns = [
'@(?:diteur|distributeur|studio|label)\s*</?\w*>\s*:?\s*</?\w*>\s*([^<]{2,60})@i',
'@(?:diteur|distributeur)\s*[:<]([^<]{2,60})@i',
'@<[^>]+class=["\'][^"\']*(?:publisher|editor|editeur)[^"\']*["\'][^>]*>\s*([^<]{2,60})\s*</@i',
];
foreach ($publisherPatterns as $pat) {
if (preg_match($pat, $html, $m)) {
$val = $clean($m[1]);
if (strlen($val) > 1) { $result['publisher'] = $val; break; }
}
}
// Format
if (preg_match('/\b(4[Kk]\s*[Uu]ltra\s*[Hh][Dd]|Ultra\s*HD|UHD)\b/', $html)) {
$result['format'] = '4K Ultra HD'; $result['format'] = '4K Ultra HD';
} elseif (strpos($format, 'BLU') !== false) { } elseif (preg_match('/\b(Blu-?ray)\b/i', $html)) {
$result['format'] = 'Blu-ray'; $result['format'] = 'Blu-ray';
} elseif (strpos($format, 'DVD') !== false) { } elseif (preg_match('/\bDVD\b/i', $html)) {
$result['format'] = 'DVD'; $result['format'] = 'DVD';
} elseif (strpos($format, 'COFFRET') !== false) { } elseif (preg_match('/\bVHS\b/i', $html)) {
$result['format'] = 'Coffret'; $result['format'] = 'VHS';
}
} }
// Extraction de la durée // Durée
if (preg_match('/(?:durée|duree|duration)\s*[:<\/]>\s*(\d+)\s*(?:min|mn|h)/i', $ficheHtml, $matches)) { if (preg_match('/(?:dur[ée]{1,2}e?|duration)\s*[:<\/]?\s*(\d{1,4})\s*(?:min(?:utes?)?|mn)/i', $html, $m)) {
$result['length'] = trim($matches[1]) . ' min'; $result['length'] = $m[1] . ' min';
} elseif (preg_match('/(\d{1,2})h\s*(\d{2})\s*(?:min)?/i', $html, $m)) {
$result['length'] = ((int)$m[1] * 60 + (int)$m[2]) . ' min';
} }
// Extraction de l'aspect ratio // Ratio d'aspect
if (preg_match('/(?:format\s*image|aspect\s*ratio|ratio)\s*[:<\/]>\s*([0-9.]+\s*[:\.]\s*[0-9.]+)/i', $ficheHtml, $matches)) { if (preg_match('/(?:format\s*(?:image|vid[eé]o)|aspect\s*ratio|ratio)\s*[:<]?\s*([\d,. ]+[:×xX/][\d,. ]+)/i', $html, $m)) {
$result['aspect'] = trim($matches[1]); $result['aspect'] = trim($m[1]);
} }
// Extraction du nombre de disques // Nombre de disques
if (preg_match('/(?:nombre\s*de\s*disques?|disques?|nb\s*disques?)\s*[:<\/]>\s*(\d+)/i', $ficheHtml, $matches)) { if (preg_match('/(?:nombre\s*de\s*disques?|nb\.?\s*disques?|disques?)\s*[:<]?\s*(\d+)/i', $html, $m)) {
$result['discs'] = trim($matches[1]); $result['discs'] = (int)$m[1];
} }
// Nettoyage des données error_log("DVDFr: résultat final → " . json_encode($result, JSON_UNESCAPED_UNICODE));
$result = array_map(function($val) {
return trim(strip_tags(html_entity_decode($val, ENT_QUOTES | ENT_HTML5, 'UTF-8')));
}, $result);
// 🔥 LOG DE DÉBOGAGE // Retourner null si rien d'utile récupéré
error_log("DVDFr: Données finales - " . json_encode($result)); $hasData = !empty($result['poster']) || !empty($result['publisher']) || !empty($result['title']);
return $hasData ? $result : null;
return (!empty($result['poster']) || !empty($result['publisher'])) ? $result : null;
} }
// ── 2. API TMDB (SANS CACHE - TITRE FRANÇAIS) ── // ── 2. API TMDB (SANS CACHE - TITRE FRANÇAIS) ──