Actualiser api.php

This commit is contained in:
2026-06-24 14:16:55 +02:00
parent 6a1a094087
commit 055a7636cc
+109 -93
View File
@@ -105,17 +105,18 @@ function extractYear($dateStr) {
return ''; return '';
} }
// ── API DVDFr (NOUVELLE APPROCHE : Scraping HTML car l'API XML est cassée) ──
function fetchDVDFr($ean, $pdo) { function fetchDVDFr($ean, $pdo) {
if (empty($ean) || strlen($ean) < 8) { if (empty($ean) || strlen($ean) < 8) return null;
error_log("DVDFr: EAN invalide ou vide - '$ean'");
return null;
}
$ua = 'MonCinema/1.0 (collection privée; contact@moncineapp.fr)'; $cacheKey = 'dvdfr_new_' . md5($ean);
$cached = getCache($pdo, $cacheKey);
if ($cached) return $cached;
// Étape 1 : recherche par gencode $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
$searchUrl = "https://www.dvdfr.com/api/search.php?gencode=" . urlencode($ean);
error_log("DVDFr: Appel API search - URL: $searchUrl"); // Étape 1 : Recherche via le site DVDfr (page HTML)
$searchUrl = "https://www.dvdfr.com/search/?q=" . urlencode($ean);
$ch = curl_init($searchUrl); $ch = curl_init($searchUrl);
curl_setopt_array($ch, [ curl_setopt_array($ch, [
@@ -125,53 +126,53 @@ function fetchDVDFr($ean, $pdo) {
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => $ua, CURLOPT_USERAGENT => $ua,
CURLOPT_FOLLOWLOCATION => true, CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => ['Accept: application/xml, text/xml, */*'], CURLOPT_HTTPHEADER => [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7',
],
]); ]);
$res = curl_exec($ch); $html = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); curl_close($ch);
if (!$res) { if (!$html || $httpCode !== 200) {
error_log("DVDFr: Échec de la requête search - HTTP $httpCode"); error_log("DVDFr: Échec recherche HTML - HTTP $httpCode");
return null; return null;
} }
error_log("DVDFr: Réponse search reçue - " . substr($res, 0, 200)); // Étape 2 : Extraire le lien vers la fiche du film depuis les résultats de recherche
$dvdUrl = null;
libxml_use_internal_errors(true); // Chercher le premier résultat qui correspond à l'EAN
$xml = simplexml_load_string($res); if (preg_match('/<a[^>]+href=["\']([^"\']+\.html)["\'][^>]*class=["\'][^"\']*result[^"\']*["\'][^>]*>/i', $html, $matches)) {
$xmlErrors = libxml_get_errors(); $dvdUrl = $matches[1];
libxml_clear_errors(); if (strpos($dvdUrl, 'http') !== 0) {
$dvdUrl = 'https://www.dvdfr.com' . $dvdUrl;
}
}
if (!$xml) { // Méthode alternative : chercher directement l'EAN dans le HTML
error_log("DVDFr: Impossible de parser le XML search - Erreurs: " . json_encode($xmlErrors)); if (!$dvdUrl && preg_match('/href=["\']([^"\']*' . preg_quote($ean, '/') . '[^"\']*\.html)["\']/i', $html, $matches)) {
$dvdUrl = $matches[1];
if (strpos($dvdUrl, 'http') !== 0) {
$dvdUrl = 'https://www.dvdfr.com' . $dvdUrl;
}
}
// Méthode 3 : chercher n'importe quel lien vers une fiche DVD/Blu-ray
if (!$dvdUrl && preg_match('/<a[^>]+href=["\'](https:\/\/www\.dvdfr\.com\/(?:dvd|blu-ray)\/[^"\']+\.html)["\']/i', $html, $matches)) {
$dvdUrl = $matches[1];
}
if (!$dvdUrl) {
error_log("DVDFr: Aucune fiche trouvée pour EAN $ean");
return null; return null;
} }
// 🔥 Meilleure détection de la structure XML error_log("DVDFr: Fiche trouvée - $dvdUrl");
$dvdId = null;
if (isset($xml->dvd) && count($xml->dvd) > 0) { // Étape 3 : Récupérer la fiche complète
$dvdId = (string)$xml->dvd[0]->id; $ch2 = curl_init($dvdUrl);
} elseif (isset($xml->resultats) && isset($xml->resultats->dvd)) {
$dvdId = (string)$xml->resultats->dvd[0]->id;
} elseif (isset($xml->results) && count($xml->results) > 0) {
$dvdId = (string)$xml->results[0]->id;
}
if (empty($dvdId)) {
error_log("DVDFr: Aucun ID DVD trouvé dans la réponse search");
error_log("DVDFr: Structure XML: " . json_encode($xml));
return null;
}
error_log("DVDFr: ID DVD trouvé - $dvdId");
// Étape 2 : fiche complète
$ficheUrl = "https://www.dvdfr.com/api/dvd.php?id=" . urlencode($dvdId);
error_log("DVDFr: Appel API fiche - URL: $ficheUrl");
$ch2 = curl_init($ficheUrl);
curl_setopt_array($ch2, [ curl_setopt_array($ch2, [
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 8, CURLOPT_TIMEOUT => 8,
@@ -179,66 +180,81 @@ function fetchDVDFr($ean, $pdo) {
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => $ua, CURLOPT_USERAGENT => $ua,
CURLOPT_FOLLOWLOCATION => true, CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => ['Accept: application/xml, text/xml, */*'], CURLOPT_HTTPHEADER => [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: fr-FR,fr;q=0.9',
],
]); ]);
$res2 = curl_exec($ch2); $ficheHtml = curl_exec($ch2);
$httpCode2 = curl_getinfo($ch2, CURLINFO_HTTP_CODE);
curl_close($ch2); curl_close($ch2);
if (!$res2) { if (!$ficheHtml) {
error_log("DVDFr: Échec de la requête fiche - HTTP $httpCode2"); error_log("DVDFr: Impossible de charger la fiche");
return null; return null;
} }
error_log("DVDFr: Réponse fiche reçue - " . substr($res2, 0, 300)); // Étape 4 : Extraire les données depuis le HTML de la fiche
libxml_use_internal_errors(true);
$fiche = simplexml_load_string($res2);
$ficheErrors = libxml_get_errors();
libxml_clear_errors();
if (!$fiche) {
error_log("DVDFr: Impossible de parser le XML fiche - Erreurs: " . json_encode($ficheErrors));
return null;
}
// 🔥 Meilleure détection de la fiche DVD
$dvd = null;
if (isset($fiche->dvd) && count($fiche->dvd) > 0) {
$dvd = $fiche->dvd[0];
} elseif (isset($fiche->fiche)) {
$dvd = $fiche->fiche;
} elseif (isset($fiche->product)) {
$dvd = $fiche->product;
}
if (!$dvd) {
error_log("DVDFr: Structure de fiche non reconnue");
error_log("DVDFr: Structure XML fiche: " . json_encode($fiche));
return null;
}
// Extraction des données avec fallbacks
$poster = '';
if (isset($dvd->cover)) {
$poster = (string)$dvd->cover;
} elseif (isset($dvd->covers) && isset($dvd->covers->cover)) {
$poster = (string)$dvd->covers->cover[0];
} elseif (isset($dvd->image)) {
$poster = (string)$dvd->image;
}
$result = [ $result = [
'poster' => $poster, 'poster' => '',
'publisher' => isset($dvd->editeur) ? (string)$dvd->editeur : '', 'publisher' => '',
'format' => isset($dvd->type) ? (string)$dvd->type : '', 'format' => '',
'length' => isset($dvd->duree) ? (string)$dvd->duree : '', 'length' => '',
'aspect' => isset($dvd->formatimage) ? (string)$dvd->formatimage : '', 'aspect' => '',
'discs' => isset($dvd->nbdisques) ? (string)$dvd->nbdisques : '', 'discs' => '',
]; ];
error_log("DVDFr: Données extraites - " . json_encode($result)); // Extraction de l'affiche (jaquette)
if (preg_match('/<img[^>]+class=["\'][^"\']*cover[^"\']*["\'][^>]+src=["\']([^"\']+)["\']/i', $ficheHtml, $matches)) {
$result['poster'] = $matches[1];
} elseif (preg_match('/<img[^>]+alt=["\'][^"\']*(?:jaquette|cover|pochette)[^"\']*["\'][^>]+src=["\']([^"\']+)["\']/i', $ficheHtml, $matches)) {
$result['poster'] = $matches[1];
} elseif (preg_match('/src=["\']([^"\']+(?:cover|jaquette|pochette)[^"\']*\.(?:jpg|png|webp))["\']/i', $ficheHtml, $matches)) {
$result['poster'] = $matches[1];
}
// Extraction de l'éditeur
if (preg_match('/(?:éditeur|distributeur|studio)\s*[:<\/]>\s*([^<]+)/i', $ficheHtml, $matches)) {
$result['publisher'] = trim(strip_tags($matches[1]));
}
// Extraction du format (Blu-ray, DVD, 4K, etc.)
if (preg_match('/(4k\s*ultra\s*hd|ultra\s*hd|blu[\s-]?ray|dvd|coffret)/i', $ficheHtml, $matches)) {
$format = strtoupper(trim($matches[1]));
if (strpos($format, '4K') !== false || strpos($format, 'ULTRA') !== false) {
$result['format'] = '4K Ultra HD';
} elseif (strpos($format, 'BLU') !== false) {
$result['format'] = 'Blu-ray';
} elseif (strpos($format, 'DVD') !== false) {
$result['format'] = 'DVD';
} elseif (strpos($format, 'COFFRET') !== false) {
$result['format'] = 'Coffret';
}
}
// Extraction de la durée
if (preg_match('/(?:durée|duree|duration)\s*[:<\/]>\s*(\d+)\s*(?:min|mn|h)/i', $ficheHtml, $matches)) {
$result['length'] = trim($matches[1]) . ' min';
}
// Extraction de l'aspect ratio
if (preg_match('/(?:format\s*image|aspect\s*ratio|ratio)\s*[:<\/]>\s*([0-9.]+\s*[:\.]\s*[0-9.]+)/i', $ficheHtml, $matches)) {
$result['aspect'] = trim($matches[1]);
}
// Extraction du nombre de disques
if (preg_match('/(?:nombre\s*de\s*disques?|disques?|nb\s*disques?)\s*[:<\/]>\s*(\d+)/i', $ficheHtml, $matches)) {
$result['discs'] = trim($matches[1]);
}
// Nettoyage des données
$result = array_map(function($val) {
return trim(strip_tags(html_entity_decode($val, ENT_QUOTES | ENT_HTML5, 'UTF-8')));
}, $result);
// Mise en cache si on a au moins une donnée
if (!empty($result['poster']) || !empty($result['publisher'])) {
setCache($pdo, $cacheKey, $result, 'dvdfr');
}
return (!empty($result['poster']) || !empty($result['publisher'])) ? $result : null; return (!empty($result['poster']) || !empty($result['publisher'])) ? $result : null;
} }