Actualiser api.php

This commit is contained in:
2026-06-30 12:31:37 +02:00
parent 174eccdb60
commit bfb5342b4a
+53 -29
View File
@@ -480,40 +480,64 @@ case 'import_batch':
$pdo->beginTransaction(); $pdo->beginTransaction();
$imported = 0; $skipped = 0; $imported = 0; $skipped = 0;
try { try {
if ($type === 'videotheque') { if ($type === 'videotheque') {
$stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director) // L'UPDATE est optimisé pour ne pas écraser les bonnes données avec des valeurs vides ou l'affiche par défaut
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE $stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director)
title=VALUES(title), year=VALUES(year), format=VALUES(format), poster=VALUES(poster), ean_isbn13=VALUES(ean_isbn13), description=VALUES(description), length=VALUES(length), number_of_discs=VALUES(number_of_discs), aspect_ratio=VALUES(aspect_ratio), actors=VALUES(actors), publisher=VALUES(publisher), director=VALUES(director)"); VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE
title=VALUES(title), year=VALUES(year), format=VALUES(format),
poster=IF(VALUES(poster)!='assets/img/default_physical_media.jpg', VALUES(poster), poster),
ean_isbn13=IF(VALUES(ean_isbn13)!='', VALUES(ean_isbn13), ean_isbn13),
description=IF(VALUES(description)!='', VALUES(description), description),
length=IF(VALUES(length)!='', VALUES(length), length),
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),
actors=IF(VALUES(actors)!='', VALUES(actors), actors),
publisher=IF(VALUES(publisher)!='', VALUES(publisher), publisher),
director=IF(VALUES(director)!='', VALUES(director), director)");
foreach ($items as $item) {
$ean = preg_replace('/[^0-9]/', '', (string)($item['ean'] ?? ''));
foreach ($items as $item) { // 1. Données de base (CSV ou fallback UPC si le titre est absent)
$ean = preg_replace('/[^0-9]/', '', (string)($item['ean'] ?? '')); $title = trim($item['title'] ?? '');
if (strlen($ean) < 8) { $skipped++; continue; } $publisher = trim($item['publisher'] ?? '');
$length = trim($item['length'] ?? '');
// 1. Données physiques via UPC (UPCitemdb → UPCMDB) $discs = (int)($item['number_of_discs'] ?? 1) ?: 1;
$aspect = trim($item['aspect_ratio'] ?? '');
$desc = trim($item['description'] ?? '');
$actors = trim($item['actors'] ?? '');
$year = '';
// Fallback API UPC uniquement si le titre manque dans le CSV
if (empty($title) && strlen($ean) >= 8) {
$phys = fetchPhysicalByEan($ean, $pdo); $phys = fetchPhysicalByEan($ean, $pdo);
$title = $phys['title'] ?? ''; $title = $phys['title'] ?? '';
if (empty($title)) { $skipped++; continue; } if (empty($publisher)) $publisher = $phys['publisher'] ?? '';
if (empty($length)) $length = $phys['length'] ?? '';
if ($discs == 1) $discs = $phys['number_of_discs'] ?? 1;
if (empty($aspect)) $aspect = $phys['aspect_ratio'] ?? '';
$year = $phys['year'] ?? ''; $year = $phys['year'] ?? '';
$format = $phys['format'] ?: detectFormat($title);
$publisher = $phys['publisher'] ?? '';
$discs = $phys['number_of_discs'] ?? 1;
$aspect = $phys['aspect_ratio'] ?? '';
$length = $phys['length'] ?? '';
// 2. Données cinéma via TMDB (Affiche, Réalisateur, Acteurs, Synopsis, Durée)
$tmdb = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
$poster = $tmdb['poster'];
$director = $tmdb['director'] ?? '';
$actors = $tmdb['actors'] ?? '';
$desc = $tmdb['description'] ?? '';
if (!empty($tmdb['length'])) $length = $tmdb['length'];
if (empty($year) && !empty($tmdb['year'])) $year = $tmdb['year'];
$id = makeStableId('videotheque', $title, $year);
$stmt->execute([$id, $title, $year, $format, $poster, $ean, $desc, $length, $discs, $aspect, $actors, $publisher, $director]);
$imported++;
} }
if (empty($title)) { $skipped++; continue; }
$format = detectFormat($title);
// 2. Données cinéma via TMDB (Affiche, Réalisateur, Acteurs, Synopsis, Année)
$tmdb = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
$poster = $tmdb['poster'];
$director = $tmdb['director'] ?? '';
// On complète avec TMDB si les données du CSV sont vides
if (empty($desc)) $desc = $tmdb['description'] ?? '';
if (empty($actors)) $actors = $tmdb['actors'] ?? '';
if (empty($length) && !empty($tmdb['length'])) $length = $tmdb['length'];
if (empty($year) && !empty($tmdb['year'])) $year = $tmdb['year'];
$id = makeStableId('videotheque', $title, $year);
$stmt->execute([$id, $title, $year, $format, $poster, $ean, $desc, $length, $discs, $aspect, $actors, $publisher, $director]);
$imported++;
}
} else { // ── IMPORTATION CRITIQUES ── } 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 (?, ?, ?, ?, ?, ?, ?, ?)