Actualiser api.php
This commit is contained in:
@@ -455,35 +455,52 @@ switch ($action) {
|
||||
// ── IMPORT PAR LOTS CSV (CROISEMENT UPC + TMDB) ──
|
||||
case 'import_batch':
|
||||
checkAuth($pdo);
|
||||
set_time_limit(0); // Empêche le timeout PHP
|
||||
set_time_limit(0);
|
||||
$items = $data['items'] ?? [];
|
||||
$type = $data['type'] ?? 'videotheque';
|
||||
$tmdbApiKey = getTmdbApiKey($pdo);
|
||||
$imported = 0;
|
||||
$debugLog = []; // 🔥 LOG DE DÉBOGAGE
|
||||
|
||||
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';
|
||||
$publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? '';
|
||||
$year = extractYear($publishDate);
|
||||
$id = makeStableId($type, $title, $year);
|
||||
|
||||
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'] ?? '';
|
||||
if (!empty($ean)) {
|
||||
$eanFloat = floatval($ean);
|
||||
if ($eanFloat > 0) $ean = (string) round($eanFloat);
|
||||
$ean = preg_replace('/[^0-9]/', '', $ean);
|
||||
}
|
||||
|
||||
$lengthRaw = $rowData['length'] ?? '';
|
||||
$length = '';
|
||||
if ($lengthRaw !== '' && $lengthRaw !== null) {
|
||||
$lengthVal = floatval($lengthRaw);
|
||||
if ($lengthVal > 0) $length = (string) round($lengthVal);
|
||||
}
|
||||
|
||||
$discsRaw = $rowData['number_of_discs'] ?? '';
|
||||
$discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1;
|
||||
|
||||
@@ -493,13 +510,6 @@ case 'import_batch':
|
||||
$format = $rowData['format'] ?? detectFormat($title, $description);
|
||||
$poster = $rowData['poster'] ?? '';
|
||||
$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
|
||||
if (!empty($ean)) {
|
||||
@@ -510,41 +520,68 @@ case 'import_batch':
|
||||
if (empty($publisher)) $publisher = $upcData['publisher'];
|
||||
if (empty($format) || $format === 'Blu-ray') $format = $upcData['format'];
|
||||
}
|
||||
|
||||
// 1.5 DVDFr
|
||||
$dvdfrData = fetchDVDFr($ean, $pdo);
|
||||
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['format']) && (empty($format) || $format === 'Blu-ray')) $format = $dvdfrData['format'];
|
||||
if (!empty($dvdfrData['length']) && empty($length)) $length = $dvdfrData['length'];
|
||||
if (!empty($dvdfrData['aspect']) && empty($aspect)) $aspect = $dvdfrData['aspect'];
|
||||
if (!empty($dvdfrData['discs']) && $discs === 1) $discs = (int)$dvdfrData['discs'];
|
||||
if (!empty($dvdfrData['length']) && empty($length)) $length = $dvdfrData['length'];
|
||||
if (!empty($dvdfrData['aspect']) && empty($aspect)) $aspect = $dvdfrData['aspect'];
|
||||
if (!empty($dvdfrData['discs']) && $discs === 1) $discs = (int)$dvdfrData['discs'];
|
||||
}
|
||||
}
|
||||
|
||||
// 2. TMDB
|
||||
// 2. TMDB - 🔥 NETTOYAGE AGRESSIF DU TITRE
|
||||
if ($tmdbApiKey && !empty($title)) {
|
||||
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
||||
if (!$tmdbData || empty($tmdbData['overview'])) {
|
||||
$cleanTitle = str_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $title);
|
||||
$cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0];
|
||||
$cleanTitle = explode(' - ', $cleanTitle)[0];
|
||||
$cleanTitle = trim($cleanTitle);
|
||||
if (!empty($cleanTitle) && $cleanTitle !== $title) {
|
||||
$tmdbFallback = fetchTMDBFull($cleanTitle, '', $tmdbApiKey, $pdo);
|
||||
if ($tmdbFallback && !empty($tmdbFallback['overview'])) $tmdbData = $tmdbFallback;
|
||||
}
|
||||
// 🔥 CRÉER UNE VERSION PROPRE DU TITRE POUR TMDB
|
||||
$tmdbTitle = $title;
|
||||
|
||||
// Supprimer les suffixes d'édition
|
||||
$tmdbTitle = preg_replace('/\s*[\[\(].*?[\]\)]\s*/', '', $tmdbTitle);
|
||||
$tmdbTitle = preg_replace('/\s*-\s*(Édition|Edition|Collector|Simple|Spéciale|Digibook|Ultimate|Intégrale|Combo|SteelBook|Boîtier|Coffret).*$/i', '', $tmdbTitle);
|
||||
$tmdbTitle = preg_replace('/\s*(Blu-ray|Bluray|DVD|4K|Ultra HD|Combo|VHS|BDRip|\[.*\]).*$/i', '', $tmdbTitle);
|
||||
$tmdbTitle = preg_replace('/\s*(Coffret|Trilogie|Quadrilogie|Collection|Anthologie).*$/i', '', $tmdbTitle);
|
||||
|
||||
// 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 (!empty($tmdbData['title'])) $title = $tmdbData['title'];
|
||||
if (empty($director)) $director = $tmdbData['director'] ?? '';
|
||||
if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
|
||||
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
|
||||
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
|
||||
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
@@ -563,7 +600,7 @@ case 'import_batch':
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]);
|
||||
} else {
|
||||
// Pour les critiques
|
||||
// Pour les critiques (code inchangé)
|
||||
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
|
||||
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
|
||||
$review = $rowData['Review'] ?? $rowData['review'] ?? '';
|
||||
@@ -594,14 +631,23 @@ case 'import_batch':
|
||||
$imported++;
|
||||
}
|
||||
$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) {
|
||||
// 🔥 FILET DE SÉCURITÉ : Annule tout et renvoie l'erreur en JSON (plus de HTML)
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user