From 4bf47c8bdabd92179b9bb1502357c5c756ec4fa1 Mon Sep 17 00:00:00 2001 From: Cedric Date: Wed, 24 Jun 2026 10:25:04 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 226 +++++++++++++++----------------------------------------- 1 file changed, 58 insertions(+), 168 deletions(-) diff --git a/api.php b/api.php index a3f0a4e..df0c3fe 100644 --- a/api.php +++ b/api.php @@ -452,17 +452,18 @@ 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); - set_time_limit(0); // 🔥 EMPÊCHE PHP DE TUER LE SCRIPT APRÈS 30 SECONDES - $items = $data['items'] ?? []; - $type = $data['type'] ?? 'videotheque'; - $tmdbApiKey = getTmdbApiKey($pdo); - $imported = 0; + // ── IMPORT PAR LOTS CSV (CROISEMENT UPC + TMDB) ── +case 'import_batch': + checkAuth($pdo); + set_time_limit(0); // Empêche le timeout PHP + $items = $data['items'] ?? []; + $type = $data['type'] ?? 'videotheque'; + $tmdbApiKey = getTmdbApiKey($pdo); + $imported = 0; + + try { + $pdo->beginTransaction(); // 🔥 UNE SEULE transaction - // ── PHASE 1 : Enrichissement HTTP (HORS transaction pour éviter les timeouts) ── - $enriched = []; foreach ($items as $rowData) { $title = $rowData['title'] ?? $rowData['Name'] ?? $rowData['Title'] ?? 'Sans titre'; $publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? ''; @@ -470,32 +471,29 @@ switch ($action) { $id = makeStableId($type, $title, $year); if ($type === 'videotheque') { - // Normalisation EAN + // Normalisation des données $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); } - // Normalisation length $lengthRaw = $rowData['length'] ?? ''; $length = ''; if ($lengthRaw !== '' && $lengthRaw !== null) { $lengthVal = floatval($lengthRaw); if ($lengthVal > 0) $length = (string) round($lengthVal); } - // Normalisation number_of_discs $discsRaw = $rowData['number_of_discs'] ?? ''; $discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1; - + $description = $rowData['description'] ?? $rowData['Description'] ?? ''; $publisher = $rowData['publisher'] ?? ''; $aspect = $rowData['aspect_ratio'] ?? ''; $format = $rowData['format'] ?? detectFormat($title, $description); $poster = $rowData['poster'] ?? ''; $director = ''; - - // Acteurs depuis CSV + $csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? ''; $actors = ''; if (!empty($csvActors)) { @@ -512,7 +510,7 @@ switch ($action) { if (empty($publisher)) $publisher = $upcData['publisher']; if (empty($format) || $format === 'Blu-ray') $format = $upcData['format']; } - // 1.5 DVDFr : affiche FR + métadonnées (priorité sur UPCitemdb) + // 1.5 DVDFr $dvdfrData = fetchDVDFr($ean, $pdo); if (!empty($dvdfrData)) { if (!empty($dvdfrData['poster'])) $poster = $dvdfrData['poster']; @@ -528,8 +526,7 @@ switch ($action) { if ($tmdbApiKey && !empty($title)) { $tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); if (!$tmdbData || empty($tmdbData['overview'])) { - $cleanTitle = $title; - $cleanTitle = str_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $cleanTitle); + $cleanTitle = str_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $title); $cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0]; $cleanTitle = explode(' - ', $cleanTitle)[0]; $cleanTitle = trim($cleanTitle); @@ -541,7 +538,6 @@ switch ($action) { 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']; @@ -549,10 +545,25 @@ switch ($action) { } } - $enriched[] = compact('id','title','year','director','poster','format','length','publisher','ean','discs','aspect','description','actors'); - + // INSERT SQL + $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]); } else { - // Critiques + // Pour les critiques $ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? ''; $rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null; $review = $rowData['Review'] ?? $rowData['review'] ?? ''; @@ -563,155 +574,34 @@ switch ($action) { $director = $tmdbData['director']; $poster = $tmdbData['poster']; $streaming = $tmdbData['streaming']; - if (empty($year)) $year = $tmdbData['year']; + if(empty($year)) $year = $tmdbData['year']; } } if (empty($streaming)) $streaming = 'Support physique / Cinéma'; - $enriched[] = compact('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 + 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++; } - - $pdo->beginTransaction(); - $pdo->beginTransaction(); - try { - 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') { - $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']; - } - $dvdfrData = fetchDVDFr($ean, $pdo); - if (!empty($dvdfrData)) { - if (!empty($dvdfrData['poster'])) $poster = $dvdfrData['poster']; - if (!empty($dvdfrData['publisher'])) $publisher = $dvdfrData['publisher']; - if (!empty($dvdfrData['format']) && (empty($format) || $format === 'Blu-ray')) $format = $dvdfrData['format']; - if (!empty($dvdfrData['length']) && empty($length)) $length = $dvdfrData['length']; - if (!empty($dvdfrData['aspect']) && empty($aspect)) $aspect = $dvdfrData['aspect']; - if (!empty($dvdfrData['discs']) && $discs === 1) $discs = (int)$dvdfrData['discs']; - } - } - - 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([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]); - } else { - // 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([$id, $title, $year, $director, $poster, $rating, $review, $streaming]); - } - $imported++; - } - $pdo->commit(); - echo json_encode(["success" => true, "imported" => $imported]); - } catch (\Throwable $e) { - // 🔥 FILET DE SÉCURITÉ : Si une erreur fatale PHP survient, on annule tout proprement - $pdo->rollBack(); - http_response_code(500); - // On renvoie l'erreur en JSON pour que le JavaScript puisse l'afficher proprement - echo json_encode(["success" => false, "error" => "Erreur serveur : " . $e->getMessage()]); - } + $pdo->commit(); echo json_encode(["success" => true, "imported" => $imported]); - break; + } catch (\Throwable $e) { + // 🔥 FILET DE SÉCURITÉ : Annule tout et renvoie l'erreur en JSON (plus de HTML) + if ($pdo->inTransaction()) { + $pdo->rollBack(); + } + http_response_code(500); + echo json_encode(["success" => false, "error" => "Erreur serveur : " . $e->getMessage()]); + } + break; } \ No newline at end of file