Actualiser api.php

This commit is contained in:
2026-06-24 08:45:16 +02:00
parent e3aea0f50c
commit 5d0e631071
+62 -92
View File
@@ -389,7 +389,6 @@ switch ($action) {
else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); } else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); }
break; break;
// ── IMPORT PAR LOTS CSV (CROISEMENT UPC + TMDB) ──
case 'import_batch': case 'import_batch':
checkAuth($pdo); checkAuth($pdo);
$items = $data['items'] ?? []; $items = $data['items'] ?? [];
@@ -403,9 +402,6 @@ switch ($action) {
$publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? ''; $publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? '';
$year = extractYear($publishDate); $year = extractYear($publishDate);
// 🔥 ID isolé par type pour éviter les collisions
$id = makeStableId($type, $title, $year);
if ($type === 'videotheque') { if ($type === 'videotheque') {
$firstName = $rowData['first_name'] ?? ''; $firstName = $rowData['first_name'] ?? '';
$lastName = $rowData['last_name'] ?? ''; $lastName = $rowData['last_name'] ?? '';
@@ -417,16 +413,17 @@ switch ($action) {
$director = $creators; // Fallback CSV (utile pour les coffrets) $director = $creators; // Fallback CSV (utile pour les coffrets)
} }
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? ''; // Support de plusieurs nomenclatures de colonnes EAN
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? $rowData['ean'] ?? '';
$description = $rowData['description'] ?? $rowData['Description'] ?? ''; $description = $rowData['description'] ?? $rowData['Description'] ?? '';
$publisher = $rowData['publisher'] ?? ''; $publisher = $rowData['publisher'] ?? $rowData['Publisher'] ?? '';
$length = $rowData['length'] ?? ''; $length = $rowData['length'] ?? '';
$discs = $rowData['number_of_discs'] ?? 1; $discs = $rowData['number_of_discs'] ?? 1;
$aspect = $rowData['aspect_ratio'] ?? ''; $aspect = $rowData['aspect_ratio'] ?? '';
$format = $rowData['format'] ?? detectFormat($title, $description); $format = $rowData['format'] ?? detectFormat($title, $description);
$poster = $rowData['poster'] ?? ''; $poster = $rowData['poster'] ?? '';
// 🔥 Extraction des acteurs depuis le CSV // Extraction des acteurs depuis le CSV
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? ''; $csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
$actors = ''; $actors = '';
if (!empty($csvActors)) { if (!empty($csvActors)) {
@@ -434,7 +431,7 @@ switch ($action) {
$actors = implode(', ', array_slice($actorsArray, 0, 4)); $actors = implode(', ', array_slice($actorsArray, 0, 4));
} }
// 1. UPCitemDB // 1. UPCitemDB & DVDFr (Si un EAN est fourni)
if (!empty($ean)) { if (!empty($ean)) {
$upcData = fetchUPCitemdb($ean, $pdo); $upcData = fetchUPCitemdb($ean, $pdo);
if ($upcData) { if ($upcData) {
@@ -444,89 +441,68 @@ switch ($action) {
if (empty($format) || $format === 'Blu-ray') $format = $upcData['format']; if (empty($format) || $format === 'Blu-ray') $format = $upcData['format'];
} }
// 🔥 1.5 DVDFr (Récupère uniquement la vraie jaquette FR)
$dvdfrCover = fetchDVDFr($ean, $pdo); $dvdfrCover = fetchDVDFr($ean, $pdo);
if (!empty($dvdfrCover)) { if (!empty($dvdfrCover)) {
$poster = $dvdfrCover; $poster = $dvdfrCover;
} }
}
// 2. TMDB (Données Officielles & Synopsis) // 2. TMDB (Sorti du bloc EAN pour fonctionner même avec juste un Titre)
if ($tmdbApiKey && !empty($title)) { if ($tmdbApiKey && !empty($title) && $title !== 'Sans titre') {
// Premier essai classique $tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
// Fallback intelligent pour les coffrets si rien n'est trouvé
if (!$tmdbData || empty($tmdbData['overview'])) {
$cleanTitle = $title;
$cleanTitle = preg_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $cleanTitle);
$cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0];
$cleanTitle = explode(' - ', $cleanTitle)[0];
$cleanTitle = trim($cleanTitle);
// 🔥 NOUVEAU : Si TMDB ne trouve rien (cas des Coffrets ou titres multiples) if (!empty($cleanTitle) && $cleanTitle !== $title) {
if (!$tmdbData || empty($tmdbData['overview'])) { $tmdbFallback = fetchTMDBFull($cleanTitle, '', $tmdbApiKey, $pdo);
$cleanTitle = $title; if ($tmdbFallback && !empty($tmdbFallback['overview'])) {
$tmdbData = $tmdbFallback;
// A. Supprime les mots parasites
$cleanTitle = preg_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $cleanTitle);
// B. Sépare les listes de films (ex: "Alien / Aliens" ou "Batman + Batman Le Défi")
$cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0];
// C. Sépare les tirets longs (ex: "Le Parrain - L'intégrale")
$cleanTitle = explode(' - ', $cleanTitle)[0];
$cleanTitle = trim($cleanTitle);
// Si le titre a été nettoyé, on relance TMDB avec le titre du 1er film
if (!empty($cleanTitle) && $cleanTitle !== $title) {
// On omet volontairement l'année, car l'année du coffret n'est pas celle du 1er film
$tmdbFallback = fetchTMDBFull($cleanTitle, '', $tmdbApiKey, $pdo);
if ($tmdbFallback && !empty($tmdbFallback['overview'])) {
$tmdbData = $tmdbFallback;
}
}
}
if ($tmdbData) {
if (!empty($tmdbData['title'])) $title = $tmdbData['title'];
if (empty($director)) {
$director = $tmdbData['director'] ?? '';
} elseif (!empty($tmdbData['director']) && strpos($director, ',') === false && strpos($tmdbData['director'], ',') !== false) {
$director = $tmdbData['director'];
}
if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
// LIGNE DÉSACTIVÉE : TMDB ne touche jamais à l'affiche de la vidéothèque
// if (empty($poster) && !empty($tmdbData['poster'])) $poster = $tmdbData['poster'];
// 🔥 Récupération du Synopsis via TMDB (Même pour le 1er film d'un coffret)
if (!empty($tmdbData['overview'])) {
$description = $tmdbData['overview'];
}
// Récupération des Acteurs officiels (Top 4)
if (!empty($tmdbData['cast'])) {
$actors = implode(', ', $tmdbData['cast']);
} }
} }
} }
if ($tmdbData) {
if (!empty($tmdbData['title'])) $title = $tmdbData['title'];
if (empty($director)) {
$director = $tmdbData['director'] ?? '';
} elseif (!empty($tmdbData['director']) && strpos($director, ',') === false && strpos($tmdbData['director'], ',') !== false) {
$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']);
}
}
} }
// 🔥 L'INSERT SQL DOIT ÊTRE ICI (EN DEHORS DU IF TMDB)
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors) // 3. GÉNÉRATION DE L'ID (Calculé APRIORI avec les données fiables de l'API)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) if ($title === 'Sans titre' && !empty($ean)) {
ON DUPLICATE KEY UPDATE $id = makeStableId($type, $ean, '0000'); // Sécurité extrême si on n'a vraiment aucun titre
title=VALUES(title), year=VALUES(year), } else {
director=IF(VALUES(director)!='',VALUES(director),director), $id = makeStableId($type, $title, $year);
poster=IF(VALUES(poster)!='',VALUES(poster),poster), }
format=IF(VALUES(format)!='',VALUES(format),format),
length=IF(VALUES(length)!='',VALUES(length),length), // 4. INSERTION EN BDD
publisher=IF(VALUES(publisher)!='',VALUES(publisher),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 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)";
ean_isbn13=IF(VALUES(ean_isbn13)!='',VALUES(ean_isbn13),ean_isbn13), $stmt = $pdo->prepare($sql);
number_of_discs=IF(VALUES(number_of_discs)!=1,VALUES(number_of_discs),number_of_discs), $stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]);
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 { } else {
// Pour les critiques // Logique pour les Critiques
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? ''; $ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null; $rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
$review = $rowData['Review'] ?? $rowData['review'] ?? ''; $review = $rowData['Review'] ?? $rowData['review'] ?? '';
@@ -542,22 +518,16 @@ switch ($action) {
} }
} }
// 🔥 Valeur par défaut pour le streaming // 3. GÉNÉRATION DE L'ID (Déplacé ici pour les critiques aussi)
$id = makeStableId($type, $title, $year);
if (empty($streaming)) { if (empty($streaming)) {
$streaming = 'Support physique / Cinéma'; $streaming = 'Support physique / Cinéma';
} }
$sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) $sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE 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)";
VALUES (?, ?, ?, ?, ?, ?, ?, ?) $stmt = $pdo->prepare($sql);
ON DUPLICATE KEY UPDATE $stmt->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]);
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]);
} }
$imported++; $imported++;
} }