clean up remnants from details fetch

This commit is contained in:
adrifcastr 2023-06-21 13:54:34 +02:00
parent 436fb2707b
commit 09f6a3125b
2 changed files with 23 additions and 26 deletions

View File

@ -7,7 +7,6 @@ import {
searchMedia,
} from "./tmdb";
import { MWMediaMeta, MWQuery } from "./types/mw";
import { TMDBMovieResponse, TMDBShowResponse } from "./types/tmdb";
const cache = new SimpleCache<MWQuery, MWMediaMeta[]>();
cache.setCompare((a, b) => {
@ -19,18 +18,11 @@ export async function searchForMedia(query: MWQuery): Promise<MWMediaMeta[]> {
if (cache.has(query)) return cache.get(query) as MWMediaMeta[];
const { searchQuery, type } = query;
const data = (await searchMedia(searchQuery, mediaTypeToTMDB(type))) as
| TMDBMovieResponse
| TMDBShowResponse;
const results = await Promise.all(
data.results.map(async (v) => {
const formattedResult = await formatTMDBSearchResult(
v,
mediaTypeToTMDB(type)
);
const data = await searchMedia(searchQuery, mediaTypeToTMDB(type));
const results = data.results.map((v) => {
const formattedResult = formatTMDBSearchResult(v, mediaTypeToTMDB(type));
return formatTMDBMeta(formattedResult);
})
);
});
cache.set(query, results, 3600); // cache results for 1 hour
return results;

View File

@ -186,23 +186,28 @@ export async function getExternalIds(
return data;
}
export async function formatTMDBSearchResult(
export function formatTMDBSearchResult(
result: TMDBShowResult | TMDBMovieResult,
mediatype: TMDBContentTypes
): Promise<TMDBMediaResult> {
): TMDBMediaResult {
const type = TMDBMediaToMediaType(mediatype);
if (type === MWMediaType.SERIES) {
const show = result as TMDBShowResult;
return {
title: show.name,
poster: getMediaPoster(show.poster_path),
id: show.id,
original_release_year: new Date(show.first_air_date).getFullYear(),
object_type: mediatype,
};
}
const movie = result as TMDBMovieResult;
return {
title:
type === MWMediaType.SERIES
? (result as TMDBShowResult).name
: (result as TMDBMovieResult).title,
poster: getMediaPoster(result.poster_path),
id: result.id,
original_release_year:
type === MWMediaType.SERIES
? Number((result as TMDBShowResult).first_air_date?.split("-")[0])
: Number((result as TMDBMovieResult).release_date?.split("-")[0]),
object_type: mediaTypeToTMDB(type),
title: movie.title,
poster: getMediaPoster(movie.poster_path),
id: movie.id,
original_release_year: new Date(movie.release_date).getFullYear(),
object_type: mediatype,
};
}