Actualiser api.php

This commit is contained in:
2026-06-27 15:28:02 +02:00
parent 69c276035f
commit d993260574
+64 -66
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;
try { $year = $item['year'] ?? '';
if ($type === 'videotheque') { $id = makeStableId('videotheque', $title, $year);
$stmtVideo = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) // ── SOURCE 1 : CSV (Informations physiques uniquement) ──
ON DUPLICATE KEY UPDATE $ean = $item['ean'] ?? '';
ean_isbn13 = VALUES(ean_isbn13), $format = $item['format'] ?? '';
poster = VALUES(poster), $publisher = $item['publisher'] ?? '';
description = VALUES(description), $discs = $item['number_of_discs'] ?? 1;
format = VALUES(format), $aspect = $item['aspect_ratio'] ?? '';
length = VALUES(length), $length = $item['length'] ?? '';
number_of_discs = VALUES(number_of_discs),
aspect_ratio = VALUES(aspect_ratio), // On initialise avec le CSV, mais l'API TMDB va écraser ces valeurs si elle trouve mieux
actors = VALUES(actors), $description = $item['description'] ?? '';
publisher = VALUES(publisher), $director = $item['director'] ?? '';
director = VALUES(director), $actors = $item['actors'] ?? '';
year = VALUES(year)"); $poster = 'assets/img/default_physical_media.jpg';
foreach ($items as $item) { // ── SOURCE 2 : API TMDB (Priorité absolue pour les métadonnées) ──
$title = $item['title'] ?? ''; $tmdbData = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
if (empty($title)) continue; if ($tmdbData) {
if ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') {
$year = $item['year'] ?? ''; $poster = $tmdbData['poster'];
$id = makeStableId('videotheque', $title, $year);
// ── SOURCE 1 : CSV STRICT (Sans Scraping) ──
$ean = $item['ean'] ?? '';
$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
} else { // ── IMPORTATION CRITIQUES ── 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'];
}
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),