Actualiser api.php
This commit is contained in:
@@ -881,139 +881,100 @@ case 'import_batch':
|
||||
$data = json_decode(file_get_contents("php://input"), true);
|
||||
$type = $data['type'] ?? '';
|
||||
$items = $data['items'] ?? [];
|
||||
|
||||
if ($type !== 'videotheque') {
|
||||
// Conserver l'ancien comportement pour les critiques (inchangé)
|
||||
// ...
|
||||
break;
|
||||
}
|
||||
set_time_limit(0);
|
||||
|
||||
// Caches en mémoire pour cette session d'import
|
||||
static $blurayCache = [];
|
||||
static $movieCoversCache = [];
|
||||
static $tmdbCache = [];
|
||||
$data = json_decode(file_get_contents("php://input"), true);
|
||||
$rows = $data['rows'] ?? [];
|
||||
|
||||
$imported = 0;
|
||||
$skipped = 0;
|
||||
|
||||
$pdo->beginTransaction();
|
||||
$imported = 0;
|
||||
$skipped = 0;
|
||||
|
||||
try {
|
||||
$stmt = $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
|
||||
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) {
|
||||
// Nettoyage de l'EAN et récupération du titre CSV
|
||||
$ean = preg_replace('/[^0-9]/', '', (string)($item['ean'] ?? ''));
|
||||
$csvTitle = trim($item['title'] ?? '');
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
|
||||
// Si aucun titre et aucun EAN, on ignore
|
||||
if (empty($csvTitle) && strlen($ean) < 8) {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
// Préparation de la requête d'insertion (Adaptée avec ON DUPLICATE KEY pour mettre à jour si réimport)
|
||||
$stmtCritiques = $pdo->prepare("
|
||||
INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
director = VALUES(director),
|
||||
poster = VALUES(poster),
|
||||
rating = VALUES(rating),
|
||||
review = VALUES(review)
|
||||
");
|
||||
|
||||
// Étape 1 : Essayer de récupérer les infos depuis Blu-ray.com (si EAN valide)
|
||||
$blurayData = [];
|
||||
if (strlen($ean) >= 8) {
|
||||
$cacheKey = 'bluray_' . $ean;
|
||||
if (!isset($blurayCache[$cacheKey])) {
|
||||
$blurayCache[$cacheKey] = fetchFromBlurayCom($ean);
|
||||
foreach ($rows as $rowData) {
|
||||
// Extraction des données du CSV
|
||||
$title = $rowData['title'] ?? $rowData['Title'] ?? '';
|
||||
$yearRaw = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? '';
|
||||
$year = substr($yearRaw, 0, 4);
|
||||
|
||||
// Récupération de l'EAN (Code-barres Blu-ray.com)
|
||||
$ean = $rowData['ean_isbn13'] ?? $rowData['upc_isbn10'] ?? '';
|
||||
|
||||
if (empty($title)) {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
$blurayData = $blurayCache[$cacheKey];
|
||||
}
|
||||
|
||||
// Titre final : on prend celui de Blu-ray.com s'il existe, sinon celui du CSV
|
||||
$title = !empty($blurayData['title']) ? $blurayData['title'] : $csvTitle;
|
||||
if (empty($title)) {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
$id = generateStableId('critique', $title, $year);
|
||||
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
|
||||
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
|
||||
$review = $rowData['Review'] ?? $rowData['review'] ?? '';
|
||||
|
||||
// Remplir les champs avec les données de Blu-ray.com (ou valeurs par défaut)
|
||||
$year = $blurayData['year'] ?? '';
|
||||
$format = $blurayData['format'] ?: detectFormat($title);
|
||||
$publisher = $blurayData['publisher'] ?? '';
|
||||
$discs = $blurayData['number_of_discs'] ?: 1;
|
||||
$aspect = $blurayData['aspect_ratio'] ?? '';
|
||||
$length = $blurayData['length'] ?? '';
|
||||
$poster = $blurayData['poster'] ?? '';
|
||||
$desc = $blurayData['description'] ?? '';
|
||||
$director = $blurayData['director'] ?? '';
|
||||
$actors = $blurayData['actors'] ?? '';
|
||||
$director = '';
|
||||
$poster = 'assets/img/default_physical_media.jpg';
|
||||
$streaming = 'Support physique / Cinéma';
|
||||
|
||||
// Étape 2 : Compléter avec MovieCovers.com (toujours, car il fonctionne bien avec les titres)
|
||||
$cacheKey = 'moviecovers_' . strtolower(trim($title));
|
||||
if (!isset($movieCoversCache[$cacheKey])) {
|
||||
$movieCoversCache[$cacheKey] = fetchFromMovieCovers($title, $year);
|
||||
}
|
||||
$mcData = $movieCoversCache[$cacheKey];
|
||||
if (!empty($mcData)) {
|
||||
if (empty($poster) || $poster === 'assets/img/default_physical_media.jpg') {
|
||||
$poster = $mcData['poster'] ?? $poster;
|
||||
// =========================================================
|
||||
// 1. TENTATIVE : TÉLÉCHARGEMENT DE LA JAQUETTE MOVIECOVERS
|
||||
// =========================================================
|
||||
$localCover = fetchAndDownloadMovieCovers($title, $ean);
|
||||
|
||||
// =========================================================
|
||||
// 2. RÉCUPÉRATION : METADONNÉES TMDB (Casting, Synopsis...)
|
||||
// =========================================================
|
||||
$tmdbData = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
|
||||
|
||||
// =========================================================
|
||||
// 3. LOGIQUE DE PRIORITÉ DES AFFICHES
|
||||
// =========================================================
|
||||
if ($localCover) {
|
||||
// ➔ SUCCÈS MOVIECOVERS : On utilise l'image physique HD téléchargée
|
||||
$poster = $localCover;
|
||||
|
||||
// On récupère quand même le réalisateur depuis TMDB
|
||||
if ($tmdbData && !empty($tmdbData['director'])) {
|
||||
$director = $tmdbData['director'];
|
||||
}
|
||||
} else {
|
||||
// ➔ ÉCHEC MOVIECOVERS : Repli sur l'affiche TMDB
|
||||
if ($tmdbData) {
|
||||
if (!empty($tmdbData['director'])) {
|
||||
$director = $tmdbData['director'];
|
||||
}
|
||||
if ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') {
|
||||
$poster = $tmdbData['poster'];
|
||||
}
|
||||
if (empty($year) && !empty($tmdbData['year'])) {
|
||||
$year = $tmdbData['year']; // Corrige l'année si absente du CSV
|
||||
}
|
||||
}
|
||||
}
|
||||
if (empty($director)) $director = $mcData['director'] ?? '';
|
||||
if (empty($actors)) $actors = $mcData['actors'] ?? '';
|
||||
if (empty($desc)) $desc = $mcData['description'] ?? '';
|
||||
if (empty($length) && !empty($mcData['length'])) $length = $mcData['length'];
|
||||
if (empty($year) && !empty($mcData['year'])) $year = $mcData['year'];
|
||||
// Option : on peut aussi améliorer le titre si MovieCovers en donne un plus propre
|
||||
if (!empty($mcData['title'])) {
|
||||
$title = cleanTitle($mcData['title']);
|
||||
}
|
||||
}
|
||||
|
||||
// Étape 3 : Fallback TMDB pour ce qui manque encore
|
||||
$cacheKey = 'tmdb_' . strtolower(trim($title));
|
||||
if (!isset($tmdbCache[$cacheKey])) {
|
||||
$tmdbCache[$cacheKey] = fetchTmdbPosterAndSynopsis($title, $year, $pdo);
|
||||
// Exécution de l'insertion ou de la mise à jour
|
||||
$stmtCritiques->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]);
|
||||
$imported++;
|
||||
}
|
||||
$tmdbData = $tmdbCache[$cacheKey];
|
||||
if (!empty($tmdbData)) {
|
||||
if (empty($poster) || $poster === 'assets/img/default_physical_media.jpg') {
|
||||
$poster = $tmdbData['poster'] ?? $poster;
|
||||
}
|
||||
if (empty($director)) $director = $tmdbData['director'] ?? '';
|
||||
if (empty($actors)) $actors = $tmdbData['actors'] ?? '';
|
||||
if (empty($desc)) $desc = $tmdbData['description'] ?? '';
|
||||
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
|
||||
if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
|
||||
}
|
||||
|
||||
// Génération d'un ID stable
|
||||
$id = makeStableId('videotheque', $title, $year);
|
||||
|
||||
// Insertion
|
||||
$stmt->execute([
|
||||
$id, $title, $year, $format, $poster, $ean, $desc, $length,
|
||||
$discs, $aspect, $actors, $publisher, $director
|
||||
]);
|
||||
$imported++;
|
||||
|
||||
$pdo->commit();
|
||||
echo json_encode(["success" => true, "imported" => $imported, "skipped" => $skipped]);
|
||||
|
||||
} catch (Throwable $e) {
|
||||
$pdo->rollBack();
|
||||
error_log("import_batch error: " . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode(["success" => false, "error" => "Erreur lors de l'import : " . $e->getMessage()]);
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
echo json_encode(["success" => true, "imported" => $imported, "skipped" => $skipped]);
|
||||
|
||||
} catch (Throwable $e) {
|
||||
$pdo->rollBack();
|
||||
error_log("import_batch error: " . $e->getMessage() . " ligne " . $e->getLine());
|
||||
http_response_code(500);
|
||||
echo json_encode(["success" => false, "error" => $e->getMessage(), "line" => $e->getLine()]);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
}
|
||||
Reference in New Issue
Block a user