Actualiser api.php

This commit is contained in:
2026-06-27 15:28:02 +02:00
parent 69c276035f
commit d993260574
+60 -62
View File
@@ -395,74 +395,72 @@ switch ($action) {
break; break;
case 'import_batch': case 'import_batch':
checkAuth($pdo); checkAuth($pdo);
$data = json_decode(file_get_contents("php://input"), true); $data = json_decode(file_get_contents("php://input"), true);
$type = $data['type'] ?? 'critique'; $type = $data['type'] ?? 'critique';
$items = $data['items'] ?? []; $items = $data['items'] ?? [];
$pdo->beginTransaction();
$imported = 0;
try {
if ($type === 'videotheque') {
$stmtVideo = $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
ean_isbn13 = VALUES(ean_isbn13),
poster = VALUES(poster),
description = VALUES(description),
format = VALUES(format),
length = VALUES(length),
number_of_discs = VALUES(number_of_discs),
aspect_ratio = VALUES(aspect_ratio),
actors = VALUES(actors),
publisher = VALUES(publisher),
director = VALUES(director),
year = VALUES(year)");
$pdo->beginTransaction(); foreach ($items as $item) {
$imported = 0; $title = $item['title'] ?? '';
if (empty($title)) continue;
$year = $item['year'] ?? '';
$id = makeStableId('videotheque', $title, $year);
try { // ── SOURCE 1 : CSV (Informations physiques uniquement) ──
if ($type === 'videotheque') { $ean = $item['ean'] ?? '';
$stmtVideo = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director) $format = $item['format'] ?? '';
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) $publisher = $item['publisher'] ?? '';
ON DUPLICATE KEY UPDATE $discs = $item['number_of_discs'] ?? 1;
ean_isbn13 = VALUES(ean_isbn13), $aspect = $item['aspect_ratio'] ?? '';
poster = VALUES(poster), $length = $item['length'] ?? '';
description = VALUES(description),
format = VALUES(format),
length = VALUES(length),
number_of_discs = VALUES(number_of_discs),
aspect_ratio = VALUES(aspect_ratio),
actors = VALUES(actors),
publisher = VALUES(publisher),
director = VALUES(director),
year = VALUES(year)");
foreach ($items as $item) { // On initialise avec le CSV, mais l'API TMDB va écraser ces valeurs si elle trouve mieux
$title = $item['title'] ?? ''; $description = $item['description'] ?? '';
if (empty($title)) continue; $director = $item['director'] ?? '';
$actors = $item['actors'] ?? '';
$poster = 'assets/img/default_physical_media.jpg';
$year = $item['year'] ?? ''; // ── SOURCE 2 : API TMDB (Priorité absolue pour les métadonnées) ──
$id = makeStableId('videotheque', $title, $year); $tmdbData = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
if ($tmdbData) {
// ── SOURCE 1 : CSV STRICT (Sans Scraping) ── if ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') {
$ean = $item['ean'] ?? ''; $poster = $tmdbData['poster'];
$format = $item['format'] ?? 'Inconnu';
$publisher = $item['publisher'] ?? '';
$discs = $item['number_of_discs'] ?? 1;
$aspect = $item['aspect_ratio'] ?? '';
$length = $item['length'] ?? '';
$description = $item['description'] ?? '';
$director = $item['director'] ?? '';
$actors = $item['actors'] ?? '';
$poster = 'assets/img/default_physical_media.jpg';
// ── SOURCE 2 : API TMDB POUR COMPLÉTER ──
$tmdbData = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
if ($tmdbData) {
if ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') {
$poster = $tmdbData['poster'];
}
if (empty($description)) $description = $tmdbData['description'] ?? '';
if (empty($director)) $director = $tmdbData['director'] ?? '';
if (empty($actors)) $actors = $tmdbData['actors'] ?? '';
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
}
if (empty($format)) $format = detectFormat($title, $description);
$stmtVideo->execute([
$id, $title, $year, $format, $poster, $ean, $description,
$length, $discs, $aspect, $actors, $publisher, $director
]);
$imported++;
} }
// Écrasement des données CSV par l'API TMDB si disponibles
if (!empty($tmdbData['description'])) $description = $tmdbData['description'];
if (!empty($tmdbData['director'])) $director = $tmdbData['director'];
if (!empty($tmdbData['actors'])) $actors = $tmdbData['actors'];
if (!empty($tmdbData['length'])) $length = $tmdbData['length'];
if (!empty($tmdbData['year'])) $year = $tmdbData['year'];
}
} else { // ── IMPORTATION CRITIQUES ── if (empty($format)) $format = detectFormat($title, $description);
$stmtVideo->execute([
$id, $title, $year, $format, $poster, $ean, $description,
$length, $discs, $aspect, $actors, $publisher, $director
]);
$imported++;
}
} else { // ── IMPORTATION CRITIQUES ──
$stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) $stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
VALUES (?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director), ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director),