reduce code duplication

This commit is contained in:
castdrian 2023-06-29 21:21:24 +02:00
parent e5be04f5ae
commit 545ac8bb7b
2 changed files with 24 additions and 20 deletions

View File

@ -1,8 +1,8 @@
import { FetchError } from "ofetch"; import { FetchError } from "ofetch";
import slugify from "slugify";
import { formatJWMeta, mediaTypeToJW } from "./justwatch"; import { formatJWMeta, mediaTypeToJW } from "./justwatch";
import { import {
TMDBIdToUrlId,
TMDBMediaToMediaType, TMDBMediaToMediaType,
formatTMDBMeta, formatTMDBMeta,
getEpisodes, getEpisodes,
@ -187,17 +187,12 @@ export async function convertLegacyUrl(
// movies always have an imdb id on tmdb // movies always have an imdb id on tmdb
if (imdbId && mediaType === MWMediaType.MOVIE) { if (imdbId && mediaType === MWMediaType.MOVIE) {
const movieId = await getMovieFromExternalId(imdbId); const movieId = await getMovieFromExternalId(imdbId);
if (movieId) if (movieId) {
return `/media/tmdb-movie-${movieId}-${slugify(meta.meta.title, { return `/media/${TMDBIdToUrlId(mediaType, movieId, meta.meta.title)}`;
lower: true, }
strict: true,
})}`;
}
if (tmdbId) { if (tmdbId) {
return `/media/tmdb-${type}-${tmdbId}-${slugify(meta.meta.title, { return `/media/${TMDBIdToUrlId(mediaType, tmdbId, meta.meta.title)}`;
lower: true, }
strict: true,
})}`;
} }
} }

View File

@ -79,15 +79,23 @@ export function formatTMDBMeta(
}; };
} }
export function TMDBMediaToId(media: MWMediaMeta): string { export function TMDBIdToUrlId(
type: MWMediaType,
tmdbId: string,
title: string
) {
return [ return [
"tmdb", "tmdb",
mediaTypeToTMDB(media.type), mediaTypeToTMDB(type),
media.id, tmdbId,
slugify(media.title, { lower: true, strict: true }), slugify(title, { lower: true, strict: true }),
].join("-"); ].join("-");
} }
export function TMDBMediaToId(media: MWMediaMeta): string {
return TMDBIdToUrlId(media.type, media.id, media.title);
}
export function decodeTMDBId( export function decodeTMDBId(
paramId: string paramId: string
): { id: string; type: MWMediaType } | null { ): { id: string; type: MWMediaType } | null {
@ -178,10 +186,11 @@ export async function generateQuickSearchMediaUrl(
const type = result.media_type === "movie" ? "movie" : "show"; const type = result.media_type === "movie" ? "movie" : "show";
const title = result.media_type === "movie" ? result.title : result.name; const title = result.media_type === "movie" ? result.title : result.name;
return `/media/tmdb-${type}-${result.id}-${slugify(title, { return `/media/${TMDBIdToUrlId(
lower: true, TMDBMediaToMediaType(type),
strict: true, result.id.toString(),
})}`; title
)}`;
} }
// Conditional type which for inferring the return type based on the content type // Conditional type which for inferring the return type based on the content type