cleanup and overlooked refactoring

This commit is contained in:
castdrian 2023-06-30 12:57:29 +02:00
parent 95f03db5b2
commit 5a4a9f01f3
3 changed files with 26 additions and 54 deletions

View File

@ -6,7 +6,6 @@ import {
TMDBMediaToMediaType, TMDBMediaToMediaType,
formatTMDBMeta, formatTMDBMeta,
getEpisodes, getEpisodes,
getExternalIds,
getMediaDetails, getMediaDetails,
getMediaPoster, getMediaPoster,
getMovieFromExternalId, getMovieFromExternalId,
@ -75,8 +74,7 @@ export async function getMetaFromId(
if (!details) return null; if (!details) return null;
const externalIds = await getExternalIds(id, mediaTypeToTMDB(type)); const imdbId = details.external_ids.imdb_id ?? undefined;
const imdbId = externalIds.imdb_id ?? undefined;
let seasonData: TMDBSeasonMetaResult | undefined; let seasonData: TMDBSeasonMetaResult | undefined;
@ -166,7 +164,13 @@ export async function getLegacyMetaFromId(
} }
export function isLegacyUrl(url: string): boolean { export function isLegacyUrl(url: string): boolean {
if (url.startsWith("/media/JW")) return true; if (url.startsWith("/media/JW") || url.startsWith("/media/tmdb-show"))
return true;
return false;
}
export function isLegacyMediaType(url: string): boolean {
if (url.startsWith("/media/tmdb-show")) return true;
return false; return false;
} }
@ -178,6 +182,15 @@ export async function convertLegacyUrl(
const urlParts = url.split("/").slice(2); const urlParts = url.split("/").slice(2);
const [, type, id] = urlParts[0].split("-", 3); const [, type, id] = urlParts[0].split("-", 3);
if (isLegacyMediaType(url)) {
const details = await getMediaDetails(id, TMDBContentTypes.TV);
return `/media/${TMDBIdToUrlId(
MWMediaType.SERIES,
details.id.toString(),
details.name
)}`;
}
const mediaType = TMDBMediaToMediaType(type as TMDBContentTypes); const mediaType = TMDBMediaToMediaType(type as TMDBContentTypes);
const meta = await getLegacyMetaFromId(mediaType, id); const meta = await getLegacyMetaFromId(mediaType, id);

View File

@ -7,16 +7,13 @@ import {
ExternalIdMovieSearchResult, ExternalIdMovieSearchResult,
TMDBContentTypes, TMDBContentTypes,
TMDBEpisodeShort, TMDBEpisodeShort,
TMDBExternalIds,
TMDBMediaResult, TMDBMediaResult,
TMDBMovieData, TMDBMovieData,
TMDBMovieExternalIds,
TMDBMovieSearchResult, TMDBMovieSearchResult,
TMDBSearchResult, TMDBSearchResult,
TMDBSeason, TMDBSeason,
TMDBSeasonMetaResult, TMDBSeasonMetaResult,
TMDBShowData, TMDBShowData,
TMDBShowExternalIds,
TMDBShowSearchResult, TMDBShowSearchResult,
} from "./types/tmdb"; } from "./types/tmdb";
import { mwFetch } from "../helpers/fetch"; import { mwFetch } from "../helpers/fetch";
@ -130,7 +127,7 @@ async function get<T>(url: string, params?: object): Promise<T> {
export async function multiSearch( export async function multiSearch(
query: string query: string
): Promise<(TMDBMovieSearchResult | TMDBShowSearchResult)[]> { ): Promise<(TMDBMovieSearchResult | TMDBShowSearchResult)[]> {
const data = await get<TMDBSearchResult>(`search/multi`, { const data = await get<TMDBSearchResult>("search/multi", {
query, query,
include_adult: false, include_adult: false,
language: "en-US", language: "en-US",
@ -174,10 +171,10 @@ export function getMediaDetails<
TReturn = MediaDetailReturn<T> TReturn = MediaDetailReturn<T>
>(id: string, type: T): Promise<TReturn> { >(id: string, type: T): Promise<TReturn> {
if (type === TMDBContentTypes.MOVIE) { if (type === TMDBContentTypes.MOVIE) {
return get<TReturn>(`/movie/${id}`); return get<TReturn>(`/movie/${id}`, { append_to_response: "external_ids" });
} }
if (type === TMDBContentTypes.TV) { if (type === TMDBContentTypes.TV) {
return get<TReturn>(`/tv/${id}`); return get<TReturn>(`/tv/${id}`, { append_to_response: "external_ids" });
} }
throw new Error("Invalid media type"); throw new Error("Invalid media type");
} }
@ -198,26 +195,6 @@ export async function getEpisodes(
})); }));
} }
export async function getExternalIds(
id: string,
type: TMDBContentTypes
): Promise<TMDBExternalIds> {
let data;
switch (type) {
case TMDBContentTypes.MOVIE:
data = await get<TMDBMovieExternalIds>(`/movie/${id}/external_ids`);
break;
case TMDBContentTypes.TV:
data = await get<TMDBShowExternalIds>(`/tv/${id}/external_ids`);
break;
default:
throw new Error("Invalid media type");
}
return data;
}
export async function getMovieFromExternalId( export async function getMovieFromExternalId(
imdbId: string imdbId: string
): Promise<string | undefined> { ): Promise<string | undefined> {

View File

@ -124,6 +124,9 @@ export interface TMDBShowData {
type: string; type: string;
vote_average: number; vote_average: number;
vote_count: number; vote_count: number;
external_ids: {
imdb_id: string | null;
};
} }
export interface TMDBMovieData { export interface TMDBMovieData {
@ -172,6 +175,9 @@ export interface TMDBMovieData {
video: boolean; video: boolean;
vote_average: number; vote_average: number;
vote_count: number; vote_count: number;
external_ids: {
imdb_id: string | null;
};
} }
export interface TMDBEpisodeResult { export interface TMDBEpisodeResult {
@ -214,30 +220,6 @@ export interface TMDBSeason {
season_number: number; season_number: number;
} }
export interface TMDBShowExternalIds {
id: number;
imdb_id: null | string;
freebase_mid: null | string;
freebase_id: null | string;
tvdb_id: number;
tvrage_id: null | string;
wikidata_id: null | string;
facebook_id: null | string;
instagram_id: null | string;
twitter_id: null | string;
}
export interface TMDBMovieExternalIds {
id: number;
imdb_id: null | string;
wikidata_id: null | string;
facebook_id: null | string;
instagram_id: null | string;
twitter_id: null | string;
}
export type TMDBExternalIds = TMDBShowExternalIds | TMDBMovieExternalIds;
export interface ExternalIdMovieSearchResult { export interface ExternalIdMovieSearchResult {
movie_results: { movie_results: {
adult: boolean; adult: boolean;