Actualiser api.php

This commit is contained in:
2026-06-24 13:07:00 +02:00
parent c58816ec90
commit b4b314321b
+23 -22
View File
@@ -420,7 +420,6 @@ switch ($action) {
else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); } else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); }
break; break;
// ── IMPORT PAR LOTS CSV (DVDfr + TMDB, SANS CACHE) ──
case 'import_batch': case 'import_batch':
checkAuth($pdo); checkAuth($pdo);
set_time_limit(0); set_time_limit(0);
@@ -430,9 +429,8 @@ switch ($action) {
$imported = 0; $imported = 0;
$debugLog = []; $debugLog = [];
$pdo->beginTransaction(); // 🔥 UNE SEULE transaction
try { try {
$pdo->beginTransaction(); // 🔥 UNE SEULE TRANSACTION
foreach ($items as $rowData) { foreach ($items as $rowData) {
$title = $rowData['title'] ?? $rowData['Name'] ?? $rowData['Title'] ?? 'Sans titre'; $title = $rowData['title'] ?? $rowData['Name'] ?? $rowData['Title'] ?? 'Sans titre';
$publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? ''; $publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? '';
@@ -473,20 +471,20 @@ switch ($action) {
$poster = $rowData['poster'] ?? ''; $poster = $rowData['poster'] ?? '';
$director = ''; $director = '';
// 🔥 1. DVDFr (priorité pour les métadonnées physiques) // 1. DVDFr (priorité pour les supports physiques FR)
if (!empty($ean)) { if (!empty($ean)) {
$dvdfrData = fetchDVDFr($ean, $pdo); $dvdfrData = fetchDVDFr($ean, $pdo);
if (!empty($dvdfrData)) { if (!empty($dvdfrData)) {
if (!empty($dvdfrData['poster'])) $poster = $dvdfrData['poster']; if (!empty($dvdfrData['poster'])) $poster = $dvdfrData['poster'];
if (!empty($dvdfrData['publisher'])) $publisher = $dvdfrData['publisher']; if (!empty($dvdfrData['publisher'])) $publisher = $dvdfrData['publisher'];
if (!empty($dvdfrData['format'])) $format = $dvdfrData['format']; if (!empty($dvdfrData['format'])) $format = $dvdfrData['format'];
if (!empty($dvdfrData['length'])) $length = $dvdfrData['length']; if (!empty($dvdfrData['length']) && empty($length)) $length = $dvdfrData['length'];
if (!empty($dvdfrData['aspect'])) $aspect = $dvdfrData['aspect']; if (!empty($dvdfrData['aspect']) && empty($aspect)) $aspect = $dvdfrData['aspect'];
if (!empty($dvdfrData['discs'])) $discs = (int)$dvdfrData['discs']; if (!empty($dvdfrData['discs']) && $discs === 1) $discs = (int)$dvdfrData['discs'];
} }
} }
// 🔥 2. TMDB (réalisateur, acteurs, synopsis) // 2. TMDB (réalisateur, synopsis, acteurs)
if ($tmdbApiKey && !empty($title)) { if ($tmdbApiKey && !empty($title)) {
$tmdbTitle = $title; $tmdbTitle = $title;
$tmdbTitle = preg_replace('/\s*[\[\(].*?[\]\)]\s*/', '', $tmdbTitle); $tmdbTitle = preg_replace('/\s*[\[\(].*?[\]\)]\s*/', '', $tmdbTitle);
@@ -497,12 +495,7 @@ switch ($action) {
$tmdbTitle = explode(' - ', $tmdbTitle)[0]; $tmdbTitle = explode(' - ', $tmdbTitle)[0];
$tmdbTitle = trim($tmdbTitle); $tmdbTitle = trim($tmdbTitle);
if ($tmdbTitle !== $title) {
$debugLog[] = "Titre nettoyé: '$title' -> '$tmdbTitle'";
}
$tmdbData = fetchTMDBFull($tmdbTitle, $year, $tmdbApiKey, $pdo); $tmdbData = fetchTMDBFull($tmdbTitle, $year, $tmdbApiKey, $pdo);
if (!$tmdbData && $tmdbTitle !== $title) { if (!$tmdbData && $tmdbTitle !== $title) {
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); $tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
} }
@@ -514,13 +507,9 @@ switch ($action) {
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length']; if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
if (!empty($tmdbData['overview'])) $description = $tmdbData['overview']; if (!empty($tmdbData['overview'])) $description = $tmdbData['overview'];
if (!empty($tmdbData['cast'])) $actors = implode(', ', $tmdbData['cast']); if (!empty($tmdbData['cast'])) $actors = implode(', ', $tmdbData['cast']);
} else {
$debugLog[] = "TMDB non trouvé pour: '$tmdbTitle'";
} }
} }
$debugLog[] = "Film: '$title' | Réal: '$director' | Durée: '$length' | Éditeur: '$publisher'";
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors) $sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
@@ -539,20 +528,32 @@ switch ($action) {
$stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]); $stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]);
} else { } else {
// Pour les critiques // ── IMPORT DES CRITIQUES ──
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? ''; $ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null; $rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
$review = $rowData['Review'] ?? $rowData['review'] ?? ''; $review = $rowData['Review'] ?? $rowData['review'] ?? '';
$director = ''; $poster = ''; $streaming = ''; $director = ''; $poster = ''; $streaming = '';
if ($tmdbApiKey && !empty($title)) { if ($tmdbApiKey && !empty($title)) {
// 🔥 Nettoyage du titre pour TMDB
$tmdbTitle = preg_replace('/\s*[\[\(].*?[\]\)]\s*/', '', $title);
$tmdbTitle = trim($tmdbTitle);
$tmdbData = fetchTMDBFull($tmdbTitle, $year, $tmdbApiKey, $pdo);
if (!$tmdbData && $tmdbTitle !== $title) {
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); $tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
}
if ($tmdbData) { if ($tmdbData) {
$director = $tmdbData['director']; $director = $tmdbData['director'] ?? '';
$poster = $tmdbData['poster']; $poster = $tmdbData['poster'] ?? '';
$streaming = $tmdbData['streaming']; $streaming = $tmdbData['streaming'] ?? '';
if(empty($year)) $year = $tmdbData['year']; if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
// 🔥 Récupération du titre français
if (!empty($tmdbData['title'])) $title = $tmdbData['title'];
} }
} }
if (empty($streaming)) $streaming = 'Support physique / Cinéma'; if (empty($streaming)) $streaming = 'Support physique / Cinéma';
$sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) $sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)