From 753fe7aba8ef9cf1db44e5d6b2cf14c30908dfc9 Mon Sep 17 00:00:00 2001 From: Cedric Date: Mon, 15 Jun 2026 22:37:19 +0200 Subject: [PATCH] Actualiser js/admin.js --- js/admin.js | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/js/admin.js b/js/admin.js index 1d070ec..8a632e8 100644 --- a/js/admin.js +++ b/js/admin.js @@ -824,39 +824,47 @@ async function fetchTmdbDetails(title, year) { try { const yearParam = year ? `&year=${year}` : ''; - const res = await fetch(`https://api.themoviedb.org/3/search/movie?api_key=${key}&query=${encodeURIComponent(title)}&language=fr-FR${yearParam}`); + // Step 1 : Recherche du film pour obtenir son ID TMDB + const res = await fetch( + `https://api.themoviedb.org/3/search/movie?api_key=${key}&query=${encodeURIComponent(title)}&language=fr-FR${yearParam}` + ); const data = await res.json(); - let movie = data.results && data.results.length > 0 ? data.results[0] : null; - - if (!movie && year) { - const res2 = await fetch(`https://api.themoviedb.org/3/search/movie?api_key=${key}&query=${encodeURIComponent(title)}&language=fr-FR`); - const data2 = await res2.json(); - if (data2.results && data2.results.length > 0) movie = data2.results[0]; - } - - if (movie) { - const poster = movie.poster_path ? `https://image.tmdb.org/t/p/w500${movie.poster_path}` : ''; - let streaming = streamingDefault; + if (data.results && data.results.length > 0) { + const movie = data.results[0]; + const movieId = movie.id; + const posterPath = movie.poster_path ? `https://image.tmdb.org/t/p/w500${movie.poster_path}` : ''; + let streamingInfo = streamingDefault; + // Step 2 : Requête sur l'endpoint "watch/providers" pour avoir le streaming en France (FR) try { - const providerRes = await fetch(`https://api.themoviedb.org/3/movie/${movie.id}/watch/providers?api_key=${key}`); - const providerData = await providerRes.json(); + const watchRes = await fetch( + `https://api.themoviedb.org/3/movie/${movieId}/watch/providers?api_key=${key}` + ); + const watchData = await watchRes.json(); - // On cherche les plateformes d'abonnement (flatrate) disponibles en France (FR) - if (providerData.results && providerData.results.FR && providerData.results.FR.flatrate) { - const providers = providerData.results.FR.flatrate.map(p => p.provider_name); + if (watchData.results && watchData.results.FR) { + const frProviders = watchData.results.FR; + // On privilégie les plateformes incluses dans un abonnement (flatrate), sinon à la location/achat + const providers = frProviders.flatrate || frProviders.buy || frProviders.rent || []; + if (providers.length > 0) { - streaming = providers.join(', '); + // Extrait les noms des plateformes trouvées (ex: Netflix, Canal+, Disney Plus) + const names = providers.map(p => p.provider_name); + // On supprime les doublons potentiels et on limite aux 3 premières pour l'affichage + const uniqueNames = [...new Set(names)].slice(0, 3); + streamingInfo = `Disponible sur : ${uniqueNames.join(', ')}`; } } - } catch (e) { - console.error("Erreur fournisseurs streaming:", e); + } catch (watchErr) { + console.error("Erreur watch providers TMDB:", watchErr); } - return { poster, streaming }; + + return { poster: posterPath, streaming: streamingInfo }; } } catch (e) { - console.error("Erreur recherche TMDB:", e); + console.error("Erreur globale fetchTmdbDetails:", e); } + return { poster: '', streaming: streamingDefault }; } \ No newline at end of file