Actualiser api.php
This commit is contained in:
@@ -893,69 +893,57 @@ 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 depuis votre table 'config'
|
||||
// 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 = $keys['tmdb_api_key'] ?? '';
|
||||
$tmdbKey = decryptData($keys['tmdb_api_key'] ?? ''); // Important : décrypter la clé
|
||||
|
||||
if (!$tmdbKey) {
|
||||
echo json_encode(["success" => false, "error" => "Clé API TMDB manquante en configuration."]);
|
||||
echo json_encode(["success" => false, "error" => "Clé API TMDB manquante."]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 2. Recherche du titre via UPCItemDB (Utilise le mode Trial)
|
||||
// 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'];
|
||||
|
||||
// 🚀 NOUVEAU : Nettoyage du titre avant d'appeler TMDB
|
||||
$cleanTitle = cleanUpcTitle($rawTitle);
|
||||
|
||||
// Log pour debug (vous pourrez le retirer plus tard)
|
||||
error_log("Nettoyage EAN : " . $rawTitle . " -> " . $cleanTitle);
|
||||
$cleanTitle = cleanUpcTitle($rawTitle); // Nettoyage
|
||||
error_log("Recherche TMDB avec titre nettoyé : " . $cleanTitle);
|
||||
|
||||
// 3. Recherche sur TMDB avec le titre nettoyé
|
||||
// 3. Recherche sur TMDB
|
||||
$tmdbSearchUrl = "https://api.themoviedb.org/3/search/movie?api_key=$tmdbKey&query=" . urlencode($cleanTitle) . "&language=fr-FR";
|
||||
|
||||
if (empty($upcData['items'])) {
|
||||
echo json_encode(["success" => false, "error" => "EAN non trouvé sur UPCItemDB"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$rawTitle = $upcData['items'][0]['title'];
|
||||
|
||||
// 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"]);
|
||||
echo json_encode(["success" => false, "error" => "Film introuvable sur TMDB avec le titre : " . $cleanTitle]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$movie = $tmdbSearchData['results'][0]; // Meilleur résultat
|
||||
$title = $movie['title']; // Titre en français grâce à language=fr-FR
|
||||
$movie = $tmdbSearchData['results'][0];
|
||||
$title = $movie['title'];
|
||||
$year = !empty($movie['release_date']) ? substr($movie['release_date'], 0, 4) : '';
|
||||
|
||||
// 4. Récupération de la jaquette physique (votre scraper)
|
||||
// 4. Récupération de la jaquette physique
|
||||
$poster = fetchAndDownloadMovieCovers($title, $ean);
|
||||
|
||||
// Fallback affiche TMDB si MovieCovers échoue
|
||||
// Fallback affiche TMDB
|
||||
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']);
|
||||
// 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]);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user