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);
|
$data = json_decode(file_get_contents("php://input"), true);
|
||||||
$ean = $data['ean'] ?? '';
|
$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");
|
$stmt = $pdo->query("SELECT key_name, key_value FROM config");
|
||||||
$keys = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
|
$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) {
|
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;
|
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);
|
$upcUrl = "https://api.upcitemdb.com/prod/trial/lookup?upc=" . urlencode($ean);
|
||||||
$upcResponse = @file_get_contents($upcUrl);
|
$upcResponse = @file_get_contents($upcUrl);
|
||||||
$upcData = json_decode($upcResponse, true);
|
$upcData = json_decode($upcResponse, true);
|
||||||
|
|
||||||
if (empty($upcData['items'])) {
|
if (empty($upcData['items'])) {
|
||||||
echo json_encode(["success" => false, "error" => "EAN non trouvé sur UPCItemDB"]);
|
echo json_encode(["success" => false, "error" => "EAN non trouvé sur UPCItemDB"]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$rawTitle = $upcData['items'][0]['title'];
|
$rawTitle = $upcData['items'][0]['title'];
|
||||||
|
$cleanTitle = cleanUpcTitle($rawTitle); // Nettoyage
|
||||||
// 🚀 NOUVEAU : Nettoyage du titre avant d'appeler TMDB
|
error_log("Recherche TMDB avec titre nettoyé : " . $cleanTitle);
|
||||||
$cleanTitle = cleanUpcTitle($rawTitle);
|
|
||||||
|
|
||||||
// Log pour debug (vous pourrez le retirer plus tard)
|
|
||||||
error_log("Nettoyage EAN : " . $rawTitle . " -> " . $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";
|
$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);
|
$tmdbSearchResponse = @file_get_contents($tmdbSearchUrl);
|
||||||
$tmdbSearchData = json_decode($tmdbSearchResponse, true);
|
$tmdbSearchData = json_decode($tmdbSearchResponse, true);
|
||||||
|
|
||||||
if (empty($tmdbSearchData['results'])) {
|
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;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$movie = $tmdbSearchData['results'][0]; // Meilleur résultat
|
$movie = $tmdbSearchData['results'][0];
|
||||||
$title = $movie['title']; // Titre en français grâce à language=fr-FR
|
$title = $movie['title'];
|
||||||
$year = !empty($movie['release_date']) ? substr($movie['release_date'], 0, 4) : '';
|
$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);
|
$poster = fetchAndDownloadMovieCovers($title, $ean);
|
||||||
|
|
||||||
// Fallback affiche TMDB si MovieCovers échoue
|
// Fallback affiche TMDB
|
||||||
if (!$poster && !empty($movie['poster_path'])) {
|
if (!$poster && !empty($movie['poster_path'])) {
|
||||||
$poster = "https://image.tmdb.org/t/p/w500" . $movie['poster_path'];
|
$poster = "https://image.tmdb.org/t/p/w500" . $movie['poster_path'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Insertion en base
|
// 5. Insertion
|
||||||
$id = generateStableId('critique', $title, $year);
|
$id = makeStableId('videotheque', $title, $year);
|
||||||
$stmt = $pdo->prepare("INSERT INTO critiques (id, title, year, poster, streaming) VALUES (?, ?, ?, ?, ?)");
|
// On insère dans la table 'videotheque' comme demandé pour l'ajout d'œuvre
|
||||||
$stmt->execute([$id, $title, $year, $poster, 'Support physique']);
|
$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" => $title]);
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user