diff --git a/api.php b/api.php index da18ea0..1dd5857 100644 --- a/api.php +++ b/api.php @@ -338,6 +338,98 @@ function fetchTmdbPosterAndSynopsis($title, $year = '', $pdo = null) { return $default; } +// ── FONCTION POUR RÉCUPÉRER LES DONNÉES DEPUIS BLU-RAY.COM ── +function fetchFromBlurayCom($ean) { + $empty = [ + 'title' => '', 'year' => '', 'director' => '', 'actors' => '', + 'poster' => '', 'description' => '', 'length' => '', + 'publisher' => '', 'format' => 'Blu-ray', 'number_of_discs' => 1, + 'aspect_ratio' => '' + ]; + + $ean = preg_replace('/[^0-9]/', '', (string)$ean); + if (strlen($ean) < 8) return $empty; + + // Recherche sur Blu-ray.com via l'EAN + $searchUrl = "https://www.blu-ray.com/movies/search.php?ean=" . urlencode($ean) . "&action=search"; + $html = httpGet($searchUrl, 10, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'); + + if (!$html) return $empty; + + // Extraire l'URL du film depuis les résultats de recherche + if (preg_match('/]+href="(\/movies\/[^\/]+\/(\d+)\/)"[^>]*>\s*]+src="([^"]+)"[^>]*>/i', $html, $matches)) { + $movieUrl = 'https://www.blu-ray.com' . $matches[1]; + $movieId = $matches[2]; + + // Récupérer la page du film pour plus de détails + $movieHtml = httpGet($movieUrl, 10); + if ($movieHtml) { + // Extraire le titre + if (preg_match('/]*class="h1[^"]*"[^>]*>([^<]+)<\/h1>/i', $movieHtml, $m)) { + $empty['title'] = trim(strip_tags($m[1])); + } elseif (preg_match('/([^-]+)-\s*Blu-ray\.com/i', $movieHtml, $m)) { + $empty['title'] = trim($m[1]); + } + + // Extraire l'année + if (preg_match('/\((\d{4})\)/', $movieHtml, $m)) { + $empty['year'] = $m[1]; + } + + // Extraire l'affiche (convertir en haute résolution) + if (preg_match('/<img[^>]+class="cover"[^>]+src="([^"]+)"/i', $movieHtml, $m)) { + $posterUrl = $m[1]; + // Convertir small/medium en large pour meilleure qualité + $posterUrl = str_replace('_small.jpg', '_large.jpg', $posterUrl); + $posterUrl = str_replace('_medium.jpg', '_large.jpg', $posterUrl); + $empty['poster'] = $posterUrl; + } + + // Extraire le réalisateur + if (preg_match('/Director:[^<]*<a[^>]*>([^<]+)<\/a>/i', $movieHtml, $m)) { + $empty['director'] = trim($m[1]); + } + + // Extraire les acteurs + if (preg_match_all('/<a[^>]*class="hovercard"[^>]*>([^<]+)<\/a>/i', $movieHtml, $actorMatches)) { + $actors = array_map('trim', array_slice($actorMatches[1], 0, 5)); + $empty['actors'] = implode(', ', $actors); + } + + // Extraire la durée (Runtime) + if (preg_match('/Runtime:[^<]*(\d+)\s*min/i', $movieHtml, $m)) { + $empty['length'] = $m[1] . ' min'; + } + + // Extraire le studio/éditeur + if (preg_match('/Studio:[^<]*<a[^>]*>([^<]+)<\/a>/i', $movieHtml, $m)) { + $empty['publisher'] = trim($m[1]); + } + + // Extraire le nombre de disques + if (preg_match('/Discs:[^<]*(\d+)/i', $movieHtml, $m)) { + $empty['number_of_discs'] = (int)$m[1]; + } + + // Extraire l'aspect ratio + if (preg_match('/Aspect[\s-]*Ratio:[^<]*(\d+:[\d.]+)/i', $movieHtml, $m)) { + $empty['aspect_ratio'] = trim($m[1]); + } + + // Extraire le format (Blu-ray, 4K, DVD, etc.) + if (preg_match('/<h2[^>]*>.*?(4K|Blu-ray|DVD|3D).*?<\/h2>/i', $movieHtml, $m)) { + $empty['format'] = trim($m[1]); + } elseif (strpos($movieUrl, '/4k/') !== false) { + $empty['format'] = '4K Ultra HD'; + } elseif (strpos($movieUrl, '/3d/') !== false) { + $empty['format'] = '3D Blu-ray'; + } + } + } + + return $empty; +} + // ── ROUTEUR PRINCIPAL ── $action = $_GET['action'] ?? ''; $data = json_decode(file_get_contents('php://input'), true) ?? []; @@ -404,7 +496,7 @@ case 'save_config': "; $result = $pdo->query($sql)->fetchAll(); // IMPORTANT : Utilisation du pointeur `&` pour que la modification soit effective dans $result - foreach ($result as &$row) { + foreach ($result as $row) { if ($row['rating'] !== null) { $ratingVal = (float)$row['rating']; $row['rating'] = ($ratingVal == floor($ratingVal)) ? (int)$ratingVal : $ratingVal; @@ -480,64 +572,46 @@ case 'import_batch': $pdo->beginTransaction(); $imported = 0; $skipped = 0; try { - if ($type === 'videotheque') { - // L'UPDATE est optimisé pour ne pas écraser les bonnes données avec des valeurs vides ou l'affiche par défaut - $stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director) - VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE - title=VALUES(title), year=VALUES(year), format=VALUES(format), - poster=IF(VALUES(poster)!='assets/img/default_physical_media.jpg', VALUES(poster), poster), - ean_isbn13=IF(VALUES(ean_isbn13)!='', VALUES(ean_isbn13), ean_isbn13), - description=IF(VALUES(description)!='', VALUES(description), description), - length=IF(VALUES(length)!='', VALUES(length), length), - number_of_discs=IF(VALUES(number_of_discs)!=1, VALUES(number_of_discs), number_of_discs), - aspect_ratio=IF(VALUES(aspect_ratio)!='', VALUES(aspect_ratio), aspect_ratio), - actors=IF(VALUES(actors)!='', VALUES(actors), actors), - publisher=IF(VALUES(publisher)!='', VALUES(publisher), publisher), - director=IF(VALUES(director)!='', VALUES(director), director)"); - - foreach ($items as $item) { - $ean = preg_replace('/[^0-9]/', '', (string)($item['ean'] ?? '')); + if ($type === 'videotheque') { + $stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director) + VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE + title=VALUES(title), year=VALUES(year), format=VALUES(format), poster=VALUES(poster), ean_isbn13=VALUES(ean_isbn13), description=VALUES(description), length=VALUES(length), number_of_discs=VALUES(number_of_discs), aspect_ratio=VALUES(aspect_ratio), actors=VALUES(actors), publisher=VALUES(publisher), director=VALUES(director)"); - // 1. Données de base (CSV ou fallback UPC si le titre est absent) - $title = trim($item['title'] ?? ''); - $publisher = trim($item['publisher'] ?? ''); - $length = trim($item['length'] ?? ''); - $discs = (int)($item['number_of_discs'] ?? 1) ?: 1; - $aspect = trim($item['aspect_ratio'] ?? ''); - $desc = trim($item['description'] ?? ''); - $actors = trim($item['actors'] ?? ''); - $year = ''; - - // Fallback API UPC uniquement si le titre manque dans le CSV - if (empty($title) && strlen($ean) >= 8) { - $phys = fetchPhysicalByEan($ean, $pdo); - $title = $phys['title'] ?? ''; - if (empty($publisher)) $publisher = $phys['publisher'] ?? ''; - if (empty($length)) $length = $phys['length'] ?? ''; - if ($discs == 1) $discs = $phys['number_of_discs'] ?? 1; - if (empty($aspect)) $aspect = $phys['aspect_ratio'] ?? ''; - $year = $phys['year'] ?? ''; + foreach ($items as $item) { + $ean = preg_replace('/[^0-9]/', '', (string)($item['ean'] ?? '')); + if (strlen($ean) < 8) { $skipped++; continue; } + + // 1. Données physiques via BLU-RAY.COM (prioritaire) + $blurayData = fetchFromBlurayCom($ean); + + $title = $blurayData['title'] ?? ''; + if (empty($title)) { $skipped++; continue; } + + $year = $blurayData['year'] ?? ''; + $format = $blurayData['format'] ?: 'Blu-ray'; + $publisher = $blurayData['publisher'] ?? ''; + $discs = $blurayData['number_of_discs'] ?: 1; + $aspect = $blurayData['aspect_ratio'] ?? ''; + $length = $blurayData['length'] ?? ''; + $poster = $blurayData['poster'] ?? ''; + + // 2. Données cinéma via TMDB (Synopsis, Titre, Réalisateur, Acteurs, Année) + $tmdb = fetchTmdbPosterAndSynopsis($title, $year, $pdo); + + // Fusion des données : TMDB complète Blu-ray.com + if (!empty($tmdb['description'])) $desc = $tmdb['description']; + else $desc = ''; + + $director = !empty($tmdb['director']) ? $tmdb['director'] : ($blurayData['director'] ?? ''); + $actors = !empty($tmdb['actors']) ? $tmdb['actors'] : ($blurayData['actors'] ?? ''); + + if (empty($year) && !empty($tmdb['year'])) $year = $tmdb['year']; + if (empty($length) && !empty($tmdb['length'])) $length = $tmdb['length']; + + $id = makeStableId('videotheque', $title, $year); + $stmt->execute([$id, $title, $year, $format, $poster, $ean, $desc, $length, $discs, $aspect, $actors, $publisher, $director]); + $imported++; } - - if (empty($title)) { $skipped++; continue; } - - $format = detectFormat($title); - - // 2. Données cinéma via TMDB (Affiche, Réalisateur, Acteurs, Synopsis, Année) - $tmdb = fetchTmdbPosterAndSynopsis($title, $year, $pdo); - $poster = $tmdb['poster']; - $director = $tmdb['director'] ?? ''; - - // On complète avec TMDB si les données du CSV sont vides - if (empty($desc)) $desc = $tmdb['description'] ?? ''; - if (empty($actors)) $actors = $tmdb['actors'] ?? ''; - if (empty($length) && !empty($tmdb['length'])) $length = $tmdb['length']; - if (empty($year) && !empty($tmdb['year'])) $year = $tmdb['year']; - - $id = makeStableId('videotheque', $title, $year); - $stmt->execute([$id, $title, $year, $format, $poster, $ean, $desc, $length, $discs, $aspect, $actors, $publisher, $director]); - $imported++; - } } else { // ── IMPORTATION CRITIQUES ── $stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) VALUES (?, ?, ?, ?, ?, ?, ?, ?)