Actualiser js/admin.js
This commit is contained in:
+32
-24
@@ -824,39 +824,47 @@ async function fetchTmdbDetails(title, year) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const yearParam = year ? `&year=${year}` : '';
|
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();
|
const data = await res.json();
|
||||||
|
|
||||||
let movie = data.results && data.results.length > 0 ? data.results[0] : null;
|
if (data.results && data.results.length > 0) {
|
||||||
|
const movie = data.results[0];
|
||||||
if (!movie && year) {
|
const movieId = movie.id;
|
||||||
const res2 = await fetch(`https://api.themoviedb.org/3/search/movie?api_key=${key}&query=${encodeURIComponent(title)}&language=fr-FR`);
|
const posterPath = movie.poster_path ? `https://image.tmdb.org/t/p/w500${movie.poster_path}` : '';
|
||||||
const data2 = await res2.json();
|
let streamingInfo = streamingDefault;
|
||||||
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;
|
|
||||||
|
|
||||||
|
// Step 2 : Requête sur l'endpoint "watch/providers" pour avoir le streaming en France (FR)
|
||||||
try {
|
try {
|
||||||
const providerRes = await fetch(`https://api.themoviedb.org/3/movie/${movie.id}/watch/providers?api_key=${key}`);
|
const watchRes = await fetch(
|
||||||
const providerData = await providerRes.json();
|
`https://api.themoviedb.org/3/movie/${movieId}/watch/providers?api_key=${key}`
|
||||||
|
);
|
||||||
|
const watchData = await watchRes.json();
|
||||||
|
|
||||||
|
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 || [];
|
||||||
|
|
||||||
// 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 (providers.length > 0) {
|
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 (watchErr) {
|
||||||
|
console.error("Erreur watch providers TMDB:", watchErr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { poster: posterPath, streaming: streamingInfo };
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Erreur fournisseurs streaming:", e);
|
console.error("Erreur globale fetchTmdbDetails:", e);
|
||||||
}
|
|
||||||
return { poster, streaming };
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error("Erreur recherche TMDB:", e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { poster: '', streaming: streamingDefault };
|
return { poster: '', streaming: streamingDefault };
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user