mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 17:03:26 +00:00
support all API key types
This commit is contained in:
@@ -63,13 +63,31 @@ function getAllSources() {
|
|||||||
return [...map.values()]
|
return [...map.values()]
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getMovieMediaDetails(id: string): Promise<MovieMedia> {
|
async function makeTMDBRequest(url: string): Promise<Response> {
|
||||||
const response = await fetch(`https://api.themoviedb.org/3/movie/${id}?api_key=${TMDB_API_KEY}`, {
|
const headers: {
|
||||||
|
accept: 'application/json';
|
||||||
|
authorization?: string;
|
||||||
|
} = {
|
||||||
|
accept: 'application/json'
|
||||||
|
};
|
||||||
|
|
||||||
|
// * JWT keys always start with ey and are ONLY valid as a header.
|
||||||
|
// * All other keys are ONLY valid as a query param.
|
||||||
|
// * Thanks TMDB.
|
||||||
|
if (TMDB_API_KEY!.startsWith('ey')) {
|
||||||
|
headers.authorization = `Bearer ${TMDB_API_KEY}`;
|
||||||
|
} else {
|
||||||
|
url += `?api_key=${TMDB_API_KEY}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fetch(url, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: headers
|
||||||
accept: 'application/json'
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getMovieMediaDetails(id: string): Promise<MovieMedia> {
|
||||||
|
const response = await makeTMDBRequest(`https://api.themoviedb.org/3/movie/${id}`);
|
||||||
const movie = await response.json();
|
const movie = await response.json();
|
||||||
|
|
||||||
if (movie.success === false) {
|
if (movie.success === false) {
|
||||||
@@ -91,12 +109,7 @@ async function getMovieMediaDetails(id: string): Promise<MovieMedia> {
|
|||||||
async function getShowMediaDetails(id: string, seasonNumber: string, episodeNumber: string): Promise<ShowMedia> {
|
async function getShowMediaDetails(id: string, seasonNumber: string, episodeNumber: string): Promise<ShowMedia> {
|
||||||
// * TV shows require the TMDB ID for the series, season, and episode
|
// * TV shows require the TMDB ID for the series, season, and episode
|
||||||
// * and the name of the series. Needs multiple requests
|
// * and the name of the series. Needs multiple requests
|
||||||
let response = await fetch(`https://api.themoviedb.org/3/tv/${id}?api_key=${TMDB_API_KEY}`, {
|
let response = await makeTMDBRequest(`https://api.themoviedb.org/3/tv/${id}`);
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
accept: 'application/json'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const series = await response.json();
|
const series = await response.json();
|
||||||
|
|
||||||
if (series.success === false) {
|
if (series.success === false) {
|
||||||
@@ -107,24 +120,14 @@ async function getShowMediaDetails(id: string, seasonNumber: string, episodeNumb
|
|||||||
throw new Error(`${series.name} has no first_air_date. Assuming unaired`);
|
throw new Error(`${series.name} has no first_air_date. Assuming unaired`);
|
||||||
}
|
}
|
||||||
|
|
||||||
response = await fetch(`https://api.themoviedb.org/3/tv/${id}/season/${seasonNumber}?api_key=${TMDB_API_KEY}`, {
|
response = await makeTMDBRequest(`https://api.themoviedb.org/3/tv/${id}/season/${seasonNumber}`);
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
accept: 'application/json'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const season = await response.json();
|
const season = await response.json();
|
||||||
|
|
||||||
if (season.success === false) {
|
if (season.success === false) {
|
||||||
throw new Error(season.status_message);
|
throw new Error(season.status_message);
|
||||||
}
|
}
|
||||||
|
|
||||||
response = await fetch(`https://api.themoviedb.org/3/tv/${id}/season/${seasonNumber}/episode/${episodeNumber}?api_key=${TMDB_API_KEY}`, {
|
response = await makeTMDBRequest(`https://api.themoviedb.org/3/tv/${id}/season/${seasonNumber}/episode/${episodeNumber}`);
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
accept: 'application/json'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const episode = await response.json();
|
const episode = await response.json();
|
||||||
|
|
||||||
if (episode.success === false) {
|
if (episode.success === false) {
|
||||||
|
Reference in New Issue
Block a user