From 1934471d463777510c88b3e45f33049d05ac684d Mon Sep 17 00:00:00 2001 From: Cedric Date: Wed, 1 Jul 2026 14:26:46 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 82 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/api.php b/api.php index e1cc61b..d6defac 100644 --- a/api.php +++ b/api.php @@ -890,42 +890,60 @@ case 'search_by_ean': break; case 'add_item_by_ean': - $data = json_decode(file_get_contents("php://input"), true); - $ean = $data['ean'] ?? ''; - - // Clé API TMDB (Remplacez par la vôtre) - $apiKey = 'VOTRE_CLE_API_TMDB'; - - // 1. Recherche TMDB via EAN - $url = "https://api.themoviedb.org/3/find/" . $ean . "?api_key=$apiKey&external_source=ean_isbn"; - $response = @file_get_contents($url); - $result = json_decode($response, true); - - if (empty($result['movie_results'])) { - echo json_encode(["success" => false, "error" => "EAN non trouvé sur TMDB"]); - exit; - } + $data = json_decode(file_get_contents("php://input"), true); + $ean = $data['ean'] ?? ''; + + // 1. Récupération des clés API depuis votre table 'config' + $stmt = $pdo->query("SELECT key_name, key_value FROM config"); + $keys = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); + $tmdbKey = $keys['tmdb_api_key'] ?? ''; + + if (!$tmdbKey) { + echo json_encode(["success" => false, "error" => "Clé API TMDB manquante en configuration."]); + exit; + } - $movie = $result['movie_results'][0]; - $title = $movie['title']; - $year = substr($movie['release_date'], 0, 4); + // 2. Recherche du titre via UPCItemDB (Utilise le mode Trial) + $upcUrl = "https://api.upcitemdb.com/prod/trial/lookup?upc=" . urlencode($ean); + $upcResponse = @file_get_contents($upcUrl); + $upcData = json_decode($upcResponse, true); - // 2. Tenter de récupérer la jaquette physique sur MovieCovers - // (Utilise la fonction que vous avez déjà ajoutée dans api.php) - $poster = fetchAndDownloadMovieCovers($title, $ean); - - // Si MovieCovers échoue, fallback sur TMDB - if (!$poster) { - $poster = "https://image.tmdb.org/t/p/w500" . $movie['poster_path']; - } + if (empty($upcData['items'])) { + echo json_encode(["success" => false, "error" => "EAN non trouvé sur UPCItemDB"]); + exit; + } - // 3. Insertion - $id = generateStableId('critique', $title, $year); - $stmt = $pdo->prepare("INSERT INTO critiques (id, title, year, poster, streaming) VALUES (?, ?, ?, ?, ?)"); - $stmt->execute([$id, $title, $year, $poster, 'Support physique']); + $rawTitle = $upcData['items'][0]['title']; - echo json_encode(["success" => true]); - break; + // 3. Recherche sur TMDB pour obtenir le titre FR et les infos correctes + $tmdbSearchUrl = "https://api.themoviedb.org/3/search/movie?api_key=$tmdbKey&query=" . urlencode($rawTitle) . "&language=fr-FR"; + $tmdbSearchResponse = @file_get_contents($tmdbSearchUrl); + $tmdbSearchData = json_decode($tmdbSearchResponse, true); + + if (empty($tmdbSearchData['results'])) { + echo json_encode(["success" => false, "error" => "Film introuvable sur TMDB"]); + exit; + } + + $movie = $tmdbSearchData['results'][0]; // Meilleur résultat + $title = $movie['title']; // Titre en français grâce à language=fr-FR + $year = !empty($movie['release_date']) ? substr($movie['release_date'], 0, 4) : ''; + + // 4. Récupération de la jaquette physique (votre scraper) + $poster = fetchAndDownloadMovieCovers($title, $ean); + + // Fallback affiche TMDB si MovieCovers échoue + if (!$poster && !empty($movie['poster_path'])) { + $poster = "https://image.tmdb.org/t/p/w500" . $movie['poster_path']; + } + + // 5. Insertion en base + $id = generateStableId('critique', $title, $year); + $stmt = $pdo->prepare("INSERT INTO critiques (id, title, year, poster, streaming) VALUES (?, ?, ?, ?, ?)"); + $stmt->execute([$id, $title, $year, $poster, 'Support physique']); + + echo json_encode(["success" => true, "title" => $title]); + break; case 'import_batch': checkAuth($pdo);