From 5d0e63107143abbf9d7f2c4249c7150d50c287bb Mon Sep 17 00:00:00 2001 From: Cedric Date: Wed, 24 Jun 2026 08:45:16 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 154 +++++++++++++++++++++++--------------------------------- 1 file changed, 62 insertions(+), 92 deletions(-) diff --git a/api.php b/api.php index 206ba62..69a4802 100644 --- a/api.php +++ b/api.php @@ -389,7 +389,6 @@ switch ($action) { else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); } break; - // ── IMPORT PAR LOTS CSV (CROISEMENT UPC + TMDB) ── case 'import_batch': checkAuth($pdo); $items = $data['items'] ?? []; @@ -403,9 +402,6 @@ switch ($action) { $publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? ''; $year = extractYear($publishDate); - // 🔥 ID isolé par type pour éviter les collisions - $id = makeStableId($type, $title, $year); - if ($type === 'videotheque') { $firstName = $rowData['first_name'] ?? ''; $lastName = $rowData['last_name'] ?? ''; @@ -417,16 +413,17 @@ switch ($action) { $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'] ?? ''; - $publisher = $rowData['publisher'] ?? ''; + $publisher = $rowData['publisher'] ?? $rowData['Publisher'] ?? ''; $length = $rowData['length'] ?? ''; $discs = $rowData['number_of_discs'] ?? 1; $aspect = $rowData['aspect_ratio'] ?? ''; $format = $rowData['format'] ?? detectFormat($title, $description); $poster = $rowData['poster'] ?? ''; - // 🔥 Extraction des acteurs depuis le CSV + // Extraction des acteurs depuis le CSV $csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? ''; $actors = ''; if (!empty($csvActors)) { @@ -434,7 +431,7 @@ switch ($action) { $actors = implode(', ', array_slice($actorsArray, 0, 4)); } - // 1. UPCitemDB + // 1. UPCitemDB & DVDFr (Si un EAN est fourni) if (!empty($ean)) { $upcData = fetchUPCitemdb($ean, $pdo); if ($upcData) { @@ -444,89 +441,68 @@ switch ($action) { if (empty($format) || $format === 'Blu-ray') $format = $upcData['format']; } - // 🔥 1.5 DVDFr (Récupère uniquement la vraie jaquette FR) $dvdfrCover = fetchDVDFr($ean, $pdo); if (!empty($dvdfrCover)) { $poster = $dvdfrCover; } + } - // 2. TMDB (Données Officielles & Synopsis) - if ($tmdbApiKey && !empty($title)) { - // Premier essai classique - $tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); + // 2. TMDB (Sorti du bloc EAN pour fonctionner même avec juste un Titre) + if ($tmdbApiKey && !empty($title) && $title !== 'Sans titre') { + $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 (!$tmdbData || empty($tmdbData['overview'])) { - $cleanTitle = $title; - - // 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 (!empty($cleanTitle) && $cleanTitle !== $title) { + $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']; + + 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) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - ON DUPLICATE KEY UPDATE - title=VALUES(title), year=VALUES(year), - 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)"; - $stmt = $pdo->prepare($sql); - $stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]); - + + // 3. GÉNÉRATION DE L'ID (Calculé APRIORI avec les données fiables de l'API) + if ($title === 'Sans titre' && !empty($ean)) { + $id = makeStableId($type, $ean, '0000'); // Sécurité extrême si on n'a vraiment aucun titre + } else { + $id = makeStableId($type, $title, $year); + } + + // 4. INSERTION EN BDD + $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)"; + $stmt = $pdo->prepare($sql); + $stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]); + } else { - // Pour les critiques + // Logique pour les Critiques $ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? ''; $rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null; $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)) { $streaming = 'Support physique / Cinéma'; } - $sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) - VALUES (?, ?, ?, ?, ?, ?, ?, ?) - ON DUPLICATE KEY UPDATE - 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]); + $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)"; + $stmt = $pdo->prepare($sql); + $stmt->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]); } $imported++; }