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,
formatTMDBMeta,
getEpisodes,
getExternalIds,
getMediaDetails,
getMediaPoster,
getMovieFromExternalId,
@ -75,8 +74,7 @@ export async function getMetaFromId(
if (!details) return null;
const externalIds = await getExternalIds(id, mediaTypeToTMDB(type));
const imdbId = externalIds.imdb_id ?? undefined;
const imdbId = details.external_ids.imdb_id ?? undefined;
let seasonData: TMDBSeasonMetaResult | undefined;
@ -166,7 +164,13 @@ export async function getLegacyMetaFromId(
}
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;
}
@ -178,6 +182,15 @@ export async function convertLegacyUrl(
const urlParts = url.split("/").slice(2);
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 meta = await getLegacyMetaFromId(mediaType, id);

View file

@ -7,16 +7,13 @@ import {
ExternalIdMovieSearchResult,
TMDBContentTypes,
TMDBEpisodeShort,
TMDBExternalIds,
TMDBMediaResult,
TMDBMovieData,
TMDBMovieExternalIds,
TMDBMovieSearchResult,
TMDBSearchResult,
TMDBSeason,
TMDBSeasonMetaResult,
TMDBShowData,
TMDBShowExternalIds,
TMDBShowSearchResult,
} from "./types/tmdb";
import { mwFetch } from "../helpers/fetch";
@ -130,7 +127,7 @@ async function get<T>(url: string, params?: object): Promise<T> {
export async function multiSearch(
query: string
): Promise<(TMDBMovieSearchResult | TMDBShowSearchResult)[]> {
const data = await get<TMDBSearchResult>(`search/multi`, {
const data = await get<TMDBSearchResult>("search/multi", {
query,
include_adult: false,
language: "en-US",
@ -174,10 +171,10 @@ export function getMediaDetails<
TReturn = MediaDetailReturn<T>
>(id: string, type: T): Promise<TReturn> {
if (type === TMDBContentTypes.MOVIE) {
return get<TReturn>(`/movie/${id}`);
return get<TReturn>(`/movie/${id}`, { append_to_response: "external_ids" });
}
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");
}
@ -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(
imdbId: string
): Promise<string | undefined> {

View file

@ -124,6 +124,9 @@ export interface TMDBShowData {
type: string;
vote_average: number;
vote_count: number;
external_ids: {
imdb_id: string | null;
};
}
export interface TMDBMovieData {
@ -172,6 +175,9 @@ export interface TMDBMovieData {
video: boolean;
vote_average: number;
vote_count: number;
external_ids: {
imdb_id: string | null;
};
}
export interface TMDBEpisodeResult {
@ -214,30 +220,6 @@ export interface TMDBSeason {
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 {
movie_results: {
adult: boolean;