Fix TMDB code

This commit is contained in:
William Oldham 2024-04-14 21:29:45 +01:00
parent 44694c6c5a
commit 926018310e

View File

@ -143,40 +143,37 @@ export function decodeTMDBId(
}; };
} }
const baseURL = "https://api.themoviedb.org/3"; const tmdbBaseUrl1 = "https://api.themoviedb.org/3";
const otherUrl = "https://api.tmdb.org/3"; const tmdbBaseUrl2 = "https://api.tmdb.org/3";
let useFallback = false;
const apiKey = conf().TMDB_READ_API_KEY; const apiKey = conf().TMDB_READ_API_KEY;
const headers = { const tmdbHeaders = {
accept: "application/json", accept: "application/json",
Authorization: `Bearer ${apiKey}`, Authorization: `Bearer ${apiKey}`,
}; };
async function get<T>(url: string, params?: object): Promise<T> { async function get<T>(url: string, params?: object): Promise<T> {
if (!apiKey) throw new Error("TMDB API key not set"); if (!apiKey) throw new Error("TMDB API key not set");
let res: T;
try { try {
res = await mwFetch<T>(encodeURI(url), { return await mwFetch<T>(encodeURI(url), {
headers, headers: tmdbHeaders,
baseURL: !useFallback ? baseURL : otherUrl, baseURL: tmdbBaseUrl1,
params: { params: {
...params, ...params,
}, },
signal: AbortSignal.timeout(!useFallback ? 5000 : 30000), signal: AbortSignal.timeout(5000),
}); });
} catch (err) { } catch (err) {
useFallback = true; return mwFetch<T>(encodeURI(url), {
res = await mwFetch<T>(encodeURI(url), { headers: tmdbHeaders,
headers, baseURL: tmdbBaseUrl2,
baseURL: otherUrl,
params: { params: {
...params, ...params,
}, },
signal: AbortSignal.timeout(30000),
}); });
} }
return res;
} }
export async function multiSearch( export async function multiSearch(