diff --git a/api.php b/api.php index 2e4433f..303d173 100644 --- a/api.php +++ b/api.php @@ -223,6 +223,19 @@ function fetchPhysicalFromUpcitemdb($ean) { ]; } +function fetchFromUpcIndex($ean) { + $url = "https://www.upcindex.com/" . urlencode($ean); + $html = httpGet($url, 15); // Utilise votre fonction curl existante + + if ($html && preg_match('/]*>(.*?)<\/h1>/si', $html, $m)) { + $title = strip_tags($m[1]); + // Nettoyage spécifique : retire le suffixe "- UPC [chiffres]" souvent présent + $title = preg_replace('/ - UPC \d+$/', '', $title); + return trim($title); + } + return null; +} + function fetchPhysicalFromUpcmdb($ean, $pdo) { $empty = ['title'=>'','publisher'=>'','format'=>'','length'=>'','number_of_discs'=>1,'aspect_ratio'=>'','year'=>'']; $apiKey = getUpcmdbApiKey($pdo); @@ -814,7 +827,6 @@ case 'save_config': $stmt = $pdo->prepare($sql); $stmt->execute([$id, $data['title'] ?? '', $data['year'] ?? '', $data['director'] ?? '', $data['poster'] ?? '', $data['rating'] ?? 3.0, $data['review'] ?? '', $streaming]); } else { - // ── CORRECTION : Suppression de fetchBluRayPhysicalInfo qui n'existe pas ── if (empty($data['poster']) && !empty($data['title'])) { $tmdbData = fetchTmdbPosterAndSynopsis($data['title'], $data['year'] ?? '', $pdo); @@ -901,59 +913,50 @@ case 'add_item_by_ean': $data = json_decode(file_get_contents("php://input"), true); $ean = $data['ean'] ?? ''; - // 1. Récupération des clés API - $stmt = $pdo->query("SELECT key_name, key_value FROM config"); - $keys = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); - $tmdbKey = decryptData($keys['tmdb_api_key'] ?? ''); // Important : décrypter la clé + // 1. Récupération via UpcIndex (Priorité 1) + $title = fetchFromUpcIndex($ean); - if (!$tmdbKey) { - echo json_encode(["success" => false, "error" => "Clé API TMDB manquante."]); + // Fallback si UpcIndex échoue + if (!$title) { + $blurayData = fetchFromBlurayCom($ean); + $title = $blurayData['title'] ?? ''; + } + + if (empty($title)) { + echo json_encode(["success" => false, "error" => "EAN non trouvé sur UpcIndex ni Blu-ray.com"]); exit; } - // 2. Recherche du titre via UPCItemDB - $upcUrl = "https://api.upcitemdb.com/prod/trial/lookup?upc=" . urlencode($ean); - $upcResponse = @file_get_contents($upcUrl); - $upcData = json_decode($upcResponse, true); - - if (empty($upcData['items'])) { - echo json_encode(["success" => false, "error" => "EAN non trouvé sur UPCItemDB"]); - exit; - } - - $rawTitle = $upcData['items'][0]['title']; - $cleanTitle = cleanUpcTitle($rawTitle); // Nettoyage - error_log("Recherche TMDB avec titre nettoyé : " . $cleanTitle); - - // 3. Recherche sur TMDB - $tmdbSearchUrl = "https://api.themoviedb.org/3/search/movie?api_key=$tmdbKey&query=" . urlencode($cleanTitle) . "&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 avec le titre : " . $cleanTitle]); - exit; - } - - $movie = $tmdbSearchData['results'][0]; - $title = $movie['title']; - $year = !empty($movie['release_date']) ? substr($movie['release_date'], 0, 4) : ''; + // 2. Recherche sur TMDB (Source de vérité pour le titre FR et infos) + // On utilise fetchTmdbPosterAndSynopsis pour récupérer le titre français, poster, etc. + $tmdbData = fetchTmdbPosterAndSynopsis($title, '', $pdo); - // 4. Récupération de la jaquette physique - $poster = fetchAndDownloadMovieCovers($title, $ean); + // Si TMDB ne trouve rien avec le titre trouvé, on utilise le titre original + $finalTitle = (!empty($tmdbData['title']) && $tmdbData['title'] !== 'assets/img/default_physical_media.jpg') + ? $tmdbData['title'] + : $title; + + $poster = ($tmdbData['poster'] !== 'assets/img/default_physical_media.jpg') ? $tmdbData['poster'] : ''; - // Fallback affiche TMDB - if (!$poster && !empty($movie['poster_path'])) { - $poster = "https://image.tmdb.org/t/p/w500" . $movie['poster_path']; - } + // 3. Insertion + $id = makeStableId('videotheque', $finalTitle, date('Y')); + + $stmt = $pdo->prepare("INSERT INTO videotheque + (id, title, poster, description, director, actors, ean_isbn13) + VALUES (?, ?, ?, ?, ?, ?, ?) + ON DUPLICATE KEY UPDATE title=VALUES(title)"); + + $stmt->execute([ + $id, + $finalTitle, + $poster, + $tmdbData['description'] ?? '', + $tmdbData['director'] ?? '', + $tmdbData['actors'] ?? '', + $ean + ]); - // 5. Insertion - $id = makeStableId('videotheque', $title, $year); - // On insère dans la table 'videotheque' comme demandé pour l'ajout d'œuvre - $stmt = $pdo->prepare("INSERT INTO videotheque (id, title, year, poster) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE title=VALUES(title)"); - $stmt->execute([$id, $title, $year, $poster]); - - echo json_encode(["success" => true, "title" => $title]); + echo json_encode(["success" => true, "title" => $finalTitle]); break; case 'import_batch':