From 3f97d129d7ad5e920ec8589352788e5d7114beb4 Mon Sep 17 00:00:00 2001 From: Cedric Date: Wed, 24 Jun 2026 09:36:16 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 121 insertions(+), 11 deletions(-) diff --git a/api.php b/api.php index b7cf250..b96d6f1 100644 --- a/api.php +++ b/api.php @@ -500,27 +500,137 @@ switch ($action) { } } - // ── PHASE 2 : INSERTs dans une transaction courte (pas d'HTTP ici) ── + $pdo->beginTransaction(); try { - $pdo->beginTransaction(); - foreach ($enriched as $row) { + foreach ($items as $rowData) { + $title = $rowData['title'] ?? $rowData['Name'] ?? $rowData['Title'] ?? 'Sans titre'; + $publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? ''; + $year = extractYear($publishDate); + $id = makeStableId($type, $title, $year); + if ($type === 'videotheque') { - $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)"; + $director = ''; + $ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? ''; + if (!empty($ean)) { + $eanFloat = floatval($ean); + if ($eanFloat > 0) $ean = (string) round($eanFloat); + $ean = preg_replace('/[^0-9]/', '', $ean); + } + $description = $rowData['description'] ?? $rowData['Description'] ?? ''; + $publisher = $rowData['publisher'] ?? ''; + + $lengthRaw = $rowData['length'] ?? ''; + $length = ''; + if ($lengthRaw !== '' && $lengthRaw !== null) { + $lengthVal = floatval($lengthRaw); + if ($lengthVal > 0) $length = (string) round($lengthVal); + } + + $discsRaw = $rowData['number_of_discs'] ?? ''; + $discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1; + + $aspect = $rowData['aspect_ratio'] ?? ''; + $format = $rowData['format'] ?? detectFormat($title, $description); + $poster = $rowData['poster'] ?? ''; + + $csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? ''; + $actors = ''; + if (!empty($csvActors)) { + $actorsArray = array_map('trim', explode(',', $csvActors)); + $actors = implode(', ', array_slice($actorsArray, 0, 4)); + } + + if (!empty($ean)) { + $upcData = fetchUPCitemdb($ean, $pdo); + if ($upcData) { + if (empty($poster) && !empty($upcData['poster'])) $poster = $upcData['poster']; + if ($title === 'Sans titre') $title = $upcData['title']; + if (empty($publisher)) $publisher = $upcData['publisher']; + if (empty($format) || $format === 'Blu-ray') $format = $upcData['format']; + } + $dvdfrCover = fetchDVDFr($ean, $pdo); + if (!empty($dvdfrCover)) $poster = $dvdfrCover; + } + + if ($tmdbApiKey && !empty($title)) { + $tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); + if (!$tmdbData || empty($tmdbData['overview'])) { + $cleanTitle = $title; + // 🔥 CORRECTION BUG FATAL : preg_ireplace n'existe pas, on utilise str_ireplace + $cleanTitle = str_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $cleanTitle); + $cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0]; + $cleanTitle = explode(' - ', $cleanTitle)[0]; + $cleanTitle = trim($cleanTitle); + 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'] ?? ''; + 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']); + } + } + + // 🔥 SQL MIS À JOUR (Ajout de title et year dans le UPDATE) + $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([$row['id'], $row['title'], $row['year'], $row['director'], $row['poster'], $row['format'], $row['length'], $row['publisher'], $row['ean'], $row['discs'], $row['aspect'], $row['description'], $row['actors']]); + $stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]); } else { - $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)"; + // Pour les critiques + $ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? ''; + $rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null; + $review = $rowData['Review'] ?? $rowData['review'] ?? ''; + $director = ''; $poster = ''; $streaming = ''; + if ($tmdbApiKey && !empty($title)) { + $tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); + if ($tmdbData) { + $director = $tmdbData['director']; + $poster = $tmdbData['poster']; + $streaming = $tmdbData['streaming']; + if(empty($year)) $year = $tmdbData['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([$row['id'], $row['title'], $row['year'], $row['director'], $row['poster'], $row['rating'], $row['review'], $row['streaming']]); + $stmt->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]); } $imported++; } $pdo->commit(); - } catch (\Exception $e) { - $pdo->rollback(); + } catch (\Throwable $e) { + // 🔥 CORRECTION : Si une erreur fatale ou SQL survient, on annule tout et on renvoie l'erreur au front + $pdo->rollBack(); http_response_code(500); - echo json_encode(["success" => false, "error" => $e->getMessage()]); - break; + echo json_encode(["success" => false, "error" => "Erreur BDD : " . $e->getMessage()]); + exit; } echo json_encode(["success" => true, "imported" => $imported]); break;