Actualiser api.php
This commit is contained in:
@@ -455,35 +455,52 @@ switch ($action) {
|
|||||||
// ── IMPORT PAR LOTS CSV (CROISEMENT UPC + TMDB) ──
|
// ── IMPORT PAR LOTS CSV (CROISEMENT UPC + TMDB) ──
|
||||||
case 'import_batch':
|
case 'import_batch':
|
||||||
checkAuth($pdo);
|
checkAuth($pdo);
|
||||||
set_time_limit(0); // Empêche le timeout PHP
|
set_time_limit(0);
|
||||||
$items = $data['items'] ?? [];
|
$items = $data['items'] ?? [];
|
||||||
$type = $data['type'] ?? 'videotheque';
|
$type = $data['type'] ?? 'videotheque';
|
||||||
$tmdbApiKey = getTmdbApiKey($pdo);
|
$tmdbApiKey = getTmdbApiKey($pdo);
|
||||||
$imported = 0;
|
$imported = 0;
|
||||||
|
$debugLog = []; // 🔥 LOG DE DÉBOGAGE
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$pdo->beginTransaction(); // 🔥 UNE SEULE transaction
|
$pdo->beginTransaction();
|
||||||
|
|
||||||
foreach ($items as $rowData) {
|
foreach ($items as $index => $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'] ?? '';
|
||||||
$year = extractYear($publishDate);
|
$year = extractYear($publishDate);
|
||||||
$id = makeStableId($type, $title, $year);
|
$id = makeStableId($type, $title, $year);
|
||||||
|
|
||||||
if ($type === 'videotheque') {
|
if ($type === 'videotheque') {
|
||||||
// Normalisation des données
|
// 🔥 EXTRACTION CORRECTE DES ACTEURS DEPUIS LE CSV
|
||||||
|
$csvActors = '';
|
||||||
|
if (!empty($rowData['ensemble'])) {
|
||||||
|
$csvActors = $rowData['ensemble'];
|
||||||
|
} elseif (!empty($rowData['creators'])) {
|
||||||
|
$csvActors = $rowData['creators'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$actors = '';
|
||||||
|
if (!empty($csvActors)) {
|
||||||
|
$actorsArray = array_map('trim', explode(',', $csvActors));
|
||||||
|
$actors = implode(', ', array_slice($actorsArray, 0, 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalisation EAN
|
||||||
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
|
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
|
||||||
if (!empty($ean)) {
|
if (!empty($ean)) {
|
||||||
$eanFloat = floatval($ean);
|
$eanFloat = floatval($ean);
|
||||||
if ($eanFloat > 0) $ean = (string) round($eanFloat);
|
if ($eanFloat > 0) $ean = (string) round($eanFloat);
|
||||||
$ean = preg_replace('/[^0-9]/', '', $ean);
|
$ean = preg_replace('/[^0-9]/', '', $ean);
|
||||||
}
|
}
|
||||||
|
|
||||||
$lengthRaw = $rowData['length'] ?? '';
|
$lengthRaw = $rowData['length'] ?? '';
|
||||||
$length = '';
|
$length = '';
|
||||||
if ($lengthRaw !== '' && $lengthRaw !== null) {
|
if ($lengthRaw !== '' && $lengthRaw !== null) {
|
||||||
$lengthVal = floatval($lengthRaw);
|
$lengthVal = floatval($lengthRaw);
|
||||||
if ($lengthVal > 0) $length = (string) round($lengthVal);
|
if ($lengthVal > 0) $length = (string) round($lengthVal);
|
||||||
}
|
}
|
||||||
|
|
||||||
$discsRaw = $rowData['number_of_discs'] ?? '';
|
$discsRaw = $rowData['number_of_discs'] ?? '';
|
||||||
$discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1;
|
$discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1;
|
||||||
|
|
||||||
@@ -494,13 +511,6 @@ case 'import_batch':
|
|||||||
$poster = $rowData['poster'] ?? '';
|
$poster = $rowData['poster'] ?? '';
|
||||||
$director = '';
|
$director = '';
|
||||||
|
|
||||||
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
|
|
||||||
$actors = '';
|
|
||||||
if (!empty($csvActors)) {
|
|
||||||
$actorsArray = array_map('trim', explode(',', $csvActors));
|
|
||||||
$actors = implode(', ', array_slice($actorsArray, 0, 4));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1. UPCitemDB
|
// 1. UPCitemDB
|
||||||
if (!empty($ean)) {
|
if (!empty($ean)) {
|
||||||
$upcData = fetchUPCitemdb($ean, $pdo);
|
$upcData = fetchUPCitemdb($ean, $pdo);
|
||||||
@@ -510,41 +520,68 @@ case 'import_batch':
|
|||||||
if (empty($publisher)) $publisher = $upcData['publisher'];
|
if (empty($publisher)) $publisher = $upcData['publisher'];
|
||||||
if (empty($format) || $format === 'Blu-ray') $format = $upcData['format'];
|
if (empty($format) || $format === 'Blu-ray') $format = $upcData['format'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1.5 DVDFr
|
// 1.5 DVDFr
|
||||||
$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']) && (empty($format) || $format === 'Blu-ray')) $format = $dvdfrData['format'];
|
if (!empty($dvdfrData['format']) && (empty($format) || $format === 'Blu-ray')) $format = $dvdfrData['format'];
|
||||||
if (!empty($dvdfrData['length']) && empty($length)) $length = $dvdfrData['length'];
|
if (!empty($dvdfrData['length']) && empty($length)) $length = $dvdfrData['length'];
|
||||||
if (!empty($dvdfrData['aspect']) && empty($aspect)) $aspect = $dvdfrData['aspect'];
|
if (!empty($dvdfrData['aspect']) && empty($aspect)) $aspect = $dvdfrData['aspect'];
|
||||||
if (!empty($dvdfrData['discs']) && $discs === 1) $discs = (int)$dvdfrData['discs'];
|
if (!empty($dvdfrData['discs']) && $discs === 1) $discs = (int)$dvdfrData['discs'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. TMDB
|
// 2. TMDB - 🔥 NETTOYAGE AGRESSIF DU TITRE
|
||||||
if ($tmdbApiKey && !empty($title)) {
|
if ($tmdbApiKey && !empty($title)) {
|
||||||
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
// 🔥 CRÉER UNE VERSION PROPRE DU TITRE POUR TMDB
|
||||||
if (!$tmdbData || empty($tmdbData['overview'])) {
|
$tmdbTitle = $title;
|
||||||
$cleanTitle = str_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $title);
|
|
||||||
$cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0];
|
// Supprimer les suffixes d'édition
|
||||||
$cleanTitle = explode(' - ', $cleanTitle)[0];
|
$tmdbTitle = preg_replace('/\s*[\[\(].*?[\]\)]\s*/', '', $tmdbTitle);
|
||||||
$cleanTitle = trim($cleanTitle);
|
$tmdbTitle = preg_replace('/\s*-\s*(Édition|Edition|Collector|Simple|Spéciale|Digibook|Ultimate|Intégrale|Combo|SteelBook|Boîtier|Coffret).*$/i', '', $tmdbTitle);
|
||||||
if (!empty($cleanTitle) && $cleanTitle !== $title) {
|
$tmdbTitle = preg_replace('/\s*(Blu-ray|Bluray|DVD|4K|Ultra HD|Combo|VHS|BDRip|\[.*\]).*$/i', '', $tmdbTitle);
|
||||||
$tmdbFallback = fetchTMDBFull($cleanTitle, '', $tmdbApiKey, $pdo);
|
$tmdbTitle = preg_replace('/\s*(Coffret|Trilogie|Quadrilogie|Collection|Anthologie).*$/i', '', $tmdbTitle);
|
||||||
if ($tmdbFallback && !empty($tmdbFallback['overview'])) $tmdbData = $tmdbFallback;
|
|
||||||
}
|
// Séparer les titres multiples (ex: "Alien / Aliens")
|
||||||
|
$tmdbTitle = preg_split('/\s*(\/|\+|:)\s*/', $tmdbTitle)[0];
|
||||||
|
|
||||||
|
// Séparer les sous-titres après tiret
|
||||||
|
$tmdbTitle = explode(' - ', $tmdbTitle)[0];
|
||||||
|
|
||||||
|
$tmdbTitle = trim($tmdbTitle);
|
||||||
|
|
||||||
|
// 🔥 LOG POUR DÉBOGAGE
|
||||||
|
if ($tmdbTitle !== $title) {
|
||||||
|
$debugLog[] = "Titre original: '$title' -> Titre TMDB: '$tmdbTitle'";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$tmdbData = fetchTMDBFull($tmdbTitle, $year, $tmdbApiKey, $pdo);
|
||||||
|
|
||||||
|
// Si TMDB ne trouve rien, essayer avec le titre original
|
||||||
|
if (!$tmdbData && $tmdbTitle !== $title) {
|
||||||
|
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
||||||
|
}
|
||||||
|
|
||||||
if ($tmdbData) {
|
if ($tmdbData) {
|
||||||
if (!empty($tmdbData['title'])) $title = $tmdbData['title'];
|
if (!empty($tmdbData['title'])) $title = $tmdbData['title'];
|
||||||
if (empty($director)) $director = $tmdbData['director'] ?? '';
|
if (empty($director)) $director = $tmdbData['director'] ?? '';
|
||||||
if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
|
if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
|
||||||
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']);
|
// 🔥 NE PAS ÉCRASER LES ACTEURS DU CSV SI TMDB N'A PAS D'ACTEURS
|
||||||
|
if (!empty($tmdbData['cast'])) {
|
||||||
|
$actors = implode(', ', $tmdbData['cast']);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$debugLog[] = "TMDB n'a pas trouvé: '$tmdbTitle' (année: $year)";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 🔥 LOG FINAL
|
||||||
|
$debugLog[] = "Film #$id: '$title' | Réal: '$director' | Acteurs: '$actors' | Durée: '$length'";
|
||||||
|
|
||||||
// INSERT SQL
|
// INSERT SQL
|
||||||
$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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
@@ -563,7 +600,7 @@ case 'import_batch':
|
|||||||
$stmt = $pdo->prepare($sql);
|
$stmt = $pdo->prepare($sql);
|
||||||
$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
|
// Pour les critiques (code inchangé)
|
||||||
$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'] ?? '';
|
||||||
@@ -594,14 +631,23 @@ case 'import_batch':
|
|||||||
$imported++;
|
$imported++;
|
||||||
}
|
}
|
||||||
$pdo->commit();
|
$pdo->commit();
|
||||||
echo json_encode(["success" => true, "imported" => $imported]);
|
|
||||||
|
// 🔥 RENVOYER LES LOGS DE DÉBOGAGE
|
||||||
|
echo json_encode([
|
||||||
|
"success" => true,
|
||||||
|
"imported" => $imported,
|
||||||
|
"debug" => $debugLog
|
||||||
|
]);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
// 🔥 FILET DE SÉCURITÉ : Annule tout et renvoie l'erreur en JSON (plus de HTML)
|
|
||||||
if ($pdo->inTransaction()) {
|
if ($pdo->inTransaction()) {
|
||||||
$pdo->rollBack();
|
$pdo->rollBack();
|
||||||
}
|
}
|
||||||
http_response_code(500);
|
http_response_code(500);
|
||||||
echo json_encode(["success" => false, "error" => "Erreur serveur : " . $e->getMessage()]);
|
echo json_encode([
|
||||||
|
"success" => false,
|
||||||
|
"error" => "Erreur serveur : " . $e->getMessage(),
|
||||||
|
"debug" => $debugLog
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user