diff --git a/api.php b/api.php index 3ba26e4..be839eb 100644 --- a/api.php +++ b/api.php @@ -1,6 +1,7 @@ false, "error" => "Aucun élément sélectionné."]); } break; - case 'import_batch': +case 'import_batch': checkAuth($pdo); $data = json_decode(file_get_contents("php://input"), true); $type = $data['type'] ?? 'critique'; @@ -429,9 +430,6 @@ switch ($action) { $imported = 0; try { - // ── On récupère la clé d'API une seule fois pour tout le monde ── - $tmdbApiKey = getTmdbApiKey($pdo); - if ($type === 'videotheque') { $stmtVideo = $pdo->prepare("INSERT INTO videotheque (id, title, year, format, poster, ean_isbn13, description, length, number_of_discs, aspect_ratio, actors, publisher, director) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) @@ -447,55 +445,50 @@ switch ($action) { publisher = VALUES(publisher), director = VALUES(director), year = VALUES(year)"); - + foreach ($items as $item) { $title = $item['title'] ?? ''; if (empty($title)) continue; $year = $item['year'] ?? ''; $id = makeStableId('videotheque', $title, $year); - - // ── 1. RECUPERATION DE TOUTES LES INFOS PHYSIQUES DEPUIS LE CSV ── + + // ── SOURCE 1 : CSV STRICT (Sans Scraping) ── $ean = $item['ean'] ?? ''; $format = $item['format'] ?? 'Inconnu'; $publisher = $item['publisher'] ?? ''; $discs = $item['number_of_discs'] ?? 1; $aspect = $item['aspect_ratio'] ?? ''; $length = $item['length'] ?? ''; + $description = $item['description'] ?? ''; + $director = $item['director'] ?? ''; + $actors = $item['actors'] ?? ''; + $poster = 'assets/img/default_physical_media.jpg'; - // Champs cinématographiques de base (du CSV s'ils existent) - $desc = $item['description'] ?? ''; - $director = $item['director'] ?? ''; - $actors = $item['actors'] ?? ''; - $poster = 'assets/img/default_physical_media.jpg'; // Image par défaut - - // ── 2. APPEL API (TMDB ou OMDb) POUR COMBLER LES MANQUES ── - if ($tmdbApiKey && !empty($title)) { - $apiData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); - - if ($apiData) { - if (empty($director)) $director = $apiData['director'] ?? ''; - if (empty($actors)) $actors = $apiData['actors'] ?? ''; - if (empty($desc)) $desc = $apiData['synopsis'] ?? ''; - if (empty($length) && !empty($apiData['length'])) $length = $apiData['length']; - - if (!empty($apiData['poster'])) { - $poster = $apiData['poster']; - } + // ── SOURCE 2 : API TMDB POUR COMPLÉTER ── + $tmdbData = fetchTmdbPosterAndSynopsis($title, $year, $pdo); + + if ($tmdbData) { + if ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') { + $poster = $tmdbData['poster']; } + if (empty($description)) $description = $tmdbData['description'] ?? ''; + if (empty($director)) $director = $tmdbData['director'] ?? ''; + if (empty($actors)) $actors = $tmdbData['actors'] ?? ''; + if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length']; + if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year']; } - error_log("Import vidéotheque '{$title}' : poster=" . ($poster !== 'assets/img/default_physical_media.jpg' ? 'OK' : 'DEFAULT') . ", format={$format}, discs={$discs}, aspect={$aspect}"); + if (empty($format)) $format = detectFormat($title, $description); - // Exécution remise À L'INTÉRIEUR de la boucle, avec les bonnes variables ($year, $desc) $stmtVideo->execute([ - $id, $title, $year, $format, $poster, $ean, $desc, + $id, $title, $year, $format, $poster, $ean, $description, $length, $discs, $aspect, $actors, $publisher, $director ]); $imported++; } - } else { + } else { // ── IMPORTATION CRITIQUES ── $stmtCritiques = $pdo->prepare("INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE title=VALUES(title), year=VALUES(year), director=VALUES(director), @@ -511,32 +504,28 @@ switch ($action) { $ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? ''; $rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null; $review = $rowData['Review'] ?? $rowData['review'] ?? ''; - $director = ''; $poster = ''; $streaming = ''; + $director = ''; $poster = 'assets/img/default_physical_media.jpg'; $streaming = 'Support physique / Cinéma'; - 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($tmdbData['title'])) $title = $tmdbData['title']; - } + // ── CORRECTION ICI : Utilisation de la bonne fonction TMDB ── + $tmdbData = fetchTmdbPosterAndSynopsis($title, $year, $pdo); + 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']; } - if (empty($streaming)) $streaming = 'Support physique / Cinéma'; $stmtCritiques->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]); $imported++; } } - $pdo->commit(); echo json_encode(["success" => true, "imported" => $imported]); - } catch (Exception $e) { + + } catch (Throwable $e) { // ── CORRECTION ICI : Throwable capture les Crash Fatals PHP ── $pdo->rollBack(); error_log("import_batch error: " . $e->getMessage()); http_response_code(500); - echo json_encode(["success" => false, "error" => $e->getMessage()]); + echo json_encode(["success" => false, "error" => $e->getMessage(), "line" => $e->getLine()]); } break; }