Actualiser api.php
This commit is contained in:
@@ -420,165 +420,166 @@ switch ($action) {
|
||||
else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); }
|
||||
break;
|
||||
|
||||
// ── IMPORT PAR LOTS CSV (DVDfr + TMDB, SANS CACHE) ──
|
||||
case 'import_batch':
|
||||
checkAuth($pdo);
|
||||
set_time_limit(0);
|
||||
$items = $data['items'] ?? [];
|
||||
$type = $data['type'] ?? 'videotheque';
|
||||
$tmdbApiKey = getTmdbApiKey($pdo);
|
||||
$imported = 0;
|
||||
$debugLog = [];
|
||||
case 'import_batch':
|
||||
checkAuth($pdo);
|
||||
set_time_limit(0);
|
||||
$items = $data['items'] ?? [];
|
||||
$type = $data['type'] ?? 'videotheque';
|
||||
$tmdbApiKey = getTmdbApiKey($pdo);
|
||||
$imported = 0;
|
||||
$debugLog = [];
|
||||
|
||||
try {
|
||||
$pdo->beginTransaction(); // 🔥 UNE SEULE TRANSACTION
|
||||
$pdo->beginTransaction(); // 🔥 UNE SEULE transaction
|
||||
try {
|
||||
foreach ($items as $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);
|
||||
|
||||
foreach ($items as $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') {
|
||||
// Acteurs depuis CSV
|
||||
$csvActors = $rowData['ensemble'] ?? $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;
|
||||
|
||||
$description = $rowData['description'] ?? $rowData['Description'] ?? '';
|
||||
$publisher = $rowData['publisher'] ?? '';
|
||||
$aspect = $rowData['aspect_ratio'] ?? '';
|
||||
$format = $rowData['format'] ?? detectFormat($title, $description);
|
||||
$poster = $rowData['poster'] ?? '';
|
||||
$director = '';
|
||||
|
||||
// 🔥 1. DVDFr (priorité pour les métadonnées physiques)
|
||||
if (!empty($ean)) {
|
||||
$dvdfrData = fetchDVDFr($ean, $pdo);
|
||||
if (!empty($dvdfrData)) {
|
||||
if (!empty($dvdfrData['poster'])) $poster = $dvdfrData['poster'];
|
||||
if (!empty($dvdfrData['publisher'])) $publisher = $dvdfrData['publisher'];
|
||||
if (!empty($dvdfrData['format'])) $format = $dvdfrData['format'];
|
||||
if (!empty($dvdfrData['length'])) $length = $dvdfrData['length'];
|
||||
if (!empty($dvdfrData['aspect'])) $aspect = $dvdfrData['aspect'];
|
||||
if (!empty($dvdfrData['discs'])) $discs = (int)$dvdfrData['discs'];
|
||||
}
|
||||
}
|
||||
|
||||
// 🔥 2. TMDB (réalisateur, acteurs, synopsis)
|
||||
if ($tmdbApiKey && !empty($title)) {
|
||||
$tmdbTitle = $title;
|
||||
$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);
|
||||
$tmdbTitle = preg_split('/\s*(\/|\+|:)\s*/', $tmdbTitle)[0];
|
||||
$tmdbTitle = explode(' - ', $tmdbTitle)[0];
|
||||
$tmdbTitle = trim($tmdbTitle);
|
||||
|
||||
if ($tmdbTitle !== $title) {
|
||||
$debugLog[] = "Titre nettoyé: '$title' -> '$tmdbTitle'";
|
||||
}
|
||||
|
||||
$tmdbData = fetchTMDBFull($tmdbTitle, $year, $tmdbApiKey, $pdo);
|
||||
|
||||
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']);
|
||||
} 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)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
title=VALUES(title), year=VALUES(year),
|
||||
director=IF(VALUES(director)!='',VALUES(director),director),
|
||||
poster=IF(VALUES(poster)!='',VALUES(poster),poster),
|
||||
format=IF(VALUES(format)!='',VALUES(format),format),
|
||||
length=IF(VALUES(length)!='',VALUES(length),length),
|
||||
publisher=IF(VALUES(publisher)!='',VALUES(publisher),publisher),
|
||||
ean_isbn13=IF(VALUES(ean_isbn13)!='',VALUES(ean_isbn13),ean_isbn13),
|
||||
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),
|
||||
description=IF(VALUES(description)!='',VALUES(description),description),
|
||||
actors=IF(VALUES(actors)!='',VALUES(actors),actors)";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]);
|
||||
|
||||
} else {
|
||||
// Pour les critiques
|
||||
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
|
||||
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
|
||||
$review = $rowData['Review'] ?? $rowData['review'] ?? '';
|
||||
$director = ''; $poster = ''; $streaming = '';
|
||||
if ($tmdbApiKey && !empty($title)) {
|
||||
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
|
||||
if ($tmdbData) {
|
||||
$director = $tmdbData['director'];
|
||||
$poster = $tmdbData['poster'];
|
||||
$streaming = $tmdbData['streaming'];
|
||||
if(empty($year)) $year = $tmdbData['year'];
|
||||
}
|
||||
}
|
||||
if (empty($streaming)) $streaming = 'Support physique / Cinéma';
|
||||
|
||||
$sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
title=VALUES(title), year=VALUES(year),
|
||||
rating=VALUES(rating),
|
||||
review=IF(VALUES(review)!='',VALUES(review),review),
|
||||
director=IF(VALUES(director)!='',VALUES(director),director),
|
||||
poster=IF(VALUES(poster)!='',VALUES(poster),poster),
|
||||
streaming=IF(VALUES(streaming)!='',VALUES(streaming),streaming)";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]);
|
||||
if ($type === 'videotheque') {
|
||||
// Acteurs depuis CSV
|
||||
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
|
||||
$actors = '';
|
||||
if (!empty($csvActors)) {
|
||||
$actorsArray = array_map('trim', explode(',', $csvActors));
|
||||
$actors = implode(', ', array_slice($actorsArray, 0, 4));
|
||||
}
|
||||
$imported++;
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
echo json_encode(["success" => true, "imported" => $imported, "debug" => $debugLog]);
|
||||
// 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);
|
||||
}
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
$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;
|
||||
|
||||
$description = $rowData['description'] ?? $rowData['Description'] ?? '';
|
||||
$publisher = $rowData['publisher'] ?? '';
|
||||
$aspect = $rowData['aspect_ratio'] ?? '';
|
||||
$format = $rowData['format'] ?? detectFormat($title, $description);
|
||||
$poster = $rowData['poster'] ?? '';
|
||||
$director = '';
|
||||
|
||||
// 1. DVDFr (priorité pour les supports physiques FR)
|
||||
if (!empty($ean)) {
|
||||
$dvdfrData = fetchDVDFr($ean, $pdo);
|
||||
if (!empty($dvdfrData)) {
|
||||
if (!empty($dvdfrData['poster'])) $poster = $dvdfrData['poster'];
|
||||
if (!empty($dvdfrData['publisher'])) $publisher = $dvdfrData['publisher'];
|
||||
if (!empty($dvdfrData['format'])) $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'];
|
||||
}
|
||||
}
|
||||
|
||||
// 2. TMDB (réalisateur, synopsis, acteurs)
|
||||
if ($tmdbApiKey && !empty($title)) {
|
||||
$tmdbTitle = $title;
|
||||
$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);
|
||||
$tmdbTitle = preg_split('/\s*(\/|\+|:)\s*/', $tmdbTitle)[0];
|
||||
$tmdbTitle = explode(' - ', $tmdbTitle)[0];
|
||||
$tmdbTitle = trim($tmdbTitle);
|
||||
|
||||
$tmdbData = fetchTMDBFull($tmdbTitle, $year, $tmdbApiKey, $pdo);
|
||||
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']);
|
||||
}
|
||||
}
|
||||
|
||||
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
title=VALUES(title), year=VALUES(year),
|
||||
director=IF(VALUES(director)!='',VALUES(director),director),
|
||||
poster=IF(VALUES(poster)!='',VALUES(poster),poster),
|
||||
format=IF(VALUES(format)!='',VALUES(format),format),
|
||||
length=IF(VALUES(length)!='',VALUES(length),length),
|
||||
publisher=IF(VALUES(publisher)!='',VALUES(publisher),publisher),
|
||||
ean_isbn13=IF(VALUES(ean_isbn13)!='',VALUES(ean_isbn13),ean_isbn13),
|
||||
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),
|
||||
description=IF(VALUES(description)!='',VALUES(description),description),
|
||||
actors=IF(VALUES(actors)!='',VALUES(actors),actors)";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]);
|
||||
|
||||
} else {
|
||||
// ── IMPORT DES CRITIQUES ──
|
||||
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
|
||||
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
|
||||
$review = $rowData['Review'] ?? $rowData['review'] ?? '';
|
||||
$director = ''; $poster = ''; $streaming = '';
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if ($tmdbData) {
|
||||
$director = $tmdbData['director'] ?? '';
|
||||
$poster = $tmdbData['poster'] ?? '';
|
||||
$streaming = $tmdbData['streaming'] ?? '';
|
||||
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';
|
||||
|
||||
$sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
title=VALUES(title), year=VALUES(year),
|
||||
rating=VALUES(rating),
|
||||
review=IF(VALUES(review)!='',VALUES(review),review),
|
||||
director=IF(VALUES(director)!='',VALUES(director),director),
|
||||
poster=IF(VALUES(poster)!='',VALUES(poster),poster),
|
||||
streaming=IF(VALUES(streaming)!='',VALUES(streaming),streaming)";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]);
|
||||
}
|
||||
http_response_code(500);
|
||||
echo json_encode(["success" => false, "error" => "Erreur serveur : " . $e->getMessage(), "debug" => $debugLog]);
|
||||
$imported++;
|
||||
}
|
||||
break;
|
||||
|
||||
$pdo->commit();
|
||||
echo json_encode(["success" => true, "imported" => $imported, "debug" => $debugLog]);
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
http_response_code(500);
|
||||
echo json_encode(["success" => false, "error" => "Erreur serveur : " . $e->getMessage(), "debug" => $debugLog]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
Reference in New Issue
Block a user