2023-06-23 10:23:46 +02:00
|
|
|
import slugify from "slugify";
|
|
|
|
|
2023-06-12 20:06:46 +02:00
|
|
|
import { conf } from "@/setup/config";
|
|
|
|
|
2023-06-21 13:23:39 +02:00
|
|
|
import { MWMediaMeta, MWMediaType, MWSeasonMeta } from "./types/mw";
|
2023-06-12 20:06:46 +02:00
|
|
|
import {
|
2023-06-21 18:16:41 +02:00
|
|
|
ExternalIdMovieSearchResult,
|
2023-06-13 21:23:47 +02:00
|
|
|
TMDBContentTypes,
|
|
|
|
TMDBEpisodeShort,
|
2023-06-16 11:18:32 +02:00
|
|
|
TMDBExternalIds,
|
2023-06-13 21:23:47 +02:00
|
|
|
TMDBMediaResult,
|
2023-06-12 20:06:46 +02:00
|
|
|
TMDBMovieData,
|
2023-06-16 11:18:32 +02:00
|
|
|
TMDBMovieExternalIds,
|
2023-06-23 11:05:01 +02:00
|
|
|
TMDBMovieSearchResult,
|
|
|
|
TMDBSearchResult,
|
2023-06-13 21:23:47 +02:00
|
|
|
TMDBSeason,
|
|
|
|
TMDBSeasonMetaResult,
|
2023-06-12 20:06:46 +02:00
|
|
|
TMDBShowData,
|
2023-06-16 11:18:32 +02:00
|
|
|
TMDBShowExternalIds,
|
2023-06-23 11:05:01 +02:00
|
|
|
TMDBShowSearchResult,
|
2023-06-21 13:23:39 +02:00
|
|
|
} from "./types/tmdb";
|
2023-06-12 20:06:46 +02:00
|
|
|
import { mwFetch } from "../helpers/fetch";
|
|
|
|
|
2023-06-13 21:23:47 +02:00
|
|
|
export function mediaTypeToTMDB(type: MWMediaType): TMDBContentTypes {
|
2023-06-30 12:20:01 +02:00
|
|
|
if (type === MWMediaType.MOVIE) return TMDBContentTypes.MOVIE;
|
|
|
|
if (type === MWMediaType.SERIES) return TMDBContentTypes.TV;
|
2023-06-13 21:23:47 +02:00
|
|
|
throw new Error("unsupported type");
|
|
|
|
}
|
|
|
|
|
2023-06-30 12:20:01 +02:00
|
|
|
export function TMDBMediaToMediaType(type: TMDBContentTypes): MWMediaType {
|
|
|
|
if (type === TMDBContentTypes.MOVIE) return MWMediaType.MOVIE;
|
|
|
|
if (type === TMDBContentTypes.TV) return MWMediaType.SERIES;
|
2023-06-13 21:23:47 +02:00
|
|
|
throw new Error("unsupported type");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatTMDBMeta(
|
|
|
|
media: TMDBMediaResult,
|
|
|
|
season?: TMDBSeasonMetaResult
|
|
|
|
): MWMediaMeta {
|
|
|
|
const type = TMDBMediaToMediaType(media.object_type);
|
|
|
|
let seasons: undefined | MWSeasonMeta[];
|
|
|
|
if (type === MWMediaType.SERIES) {
|
|
|
|
seasons = media.seasons
|
|
|
|
?.sort((a, b) => a.season_number - b.season_number)
|
|
|
|
.map(
|
|
|
|
(v): MWSeasonMeta => ({
|
|
|
|
title: v.title,
|
|
|
|
id: v.id.toString(),
|
|
|
|
number: v.season_number,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
title: media.title,
|
|
|
|
id: media.id.toString(),
|
|
|
|
year: media.original_release_year?.toString(),
|
|
|
|
poster: media.poster,
|
|
|
|
type,
|
|
|
|
seasons: seasons as any,
|
|
|
|
seasonData: season
|
|
|
|
? ({
|
|
|
|
id: season.id.toString(),
|
|
|
|
number: season.season_number,
|
|
|
|
title: season.title,
|
|
|
|
episodes: season.episodes
|
|
|
|
.sort((a, b) => a.episode_number - b.episode_number)
|
|
|
|
.map((v) => ({
|
|
|
|
id: v.id.toString(),
|
|
|
|
number: v.episode_number,
|
|
|
|
title: v.title,
|
|
|
|
})),
|
|
|
|
} as any)
|
|
|
|
: (undefined as any),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-29 21:21:24 +02:00
|
|
|
export function TMDBIdToUrlId(
|
|
|
|
type: MWMediaType,
|
|
|
|
tmdbId: string,
|
|
|
|
title: string
|
|
|
|
) {
|
2023-06-23 10:23:46 +02:00
|
|
|
return [
|
|
|
|
"tmdb",
|
2023-06-29 21:21:24 +02:00
|
|
|
mediaTypeToTMDB(type),
|
|
|
|
tmdbId,
|
|
|
|
slugify(title, { lower: true, strict: true }),
|
2023-06-23 10:23:46 +02:00
|
|
|
].join("-");
|
2023-06-13 21:23:47 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 21:21:24 +02:00
|
|
|
export function TMDBMediaToId(media: MWMediaMeta): string {
|
|
|
|
return TMDBIdToUrlId(media.type, media.id, media.title);
|
|
|
|
}
|
|
|
|
|
2023-06-13 21:23:47 +02:00
|
|
|
export function decodeTMDBId(
|
|
|
|
paramId: string
|
|
|
|
): { id: string; type: MWMediaType } | null {
|
|
|
|
const [prefix, type, id] = paramId.split("-", 3);
|
|
|
|
if (prefix !== "tmdb") return null;
|
|
|
|
let mediaType;
|
|
|
|
try {
|
2023-06-30 12:20:01 +02:00
|
|
|
mediaType = TMDBMediaToMediaType(type as TMDBContentTypes);
|
2023-06-13 21:23:47 +02:00
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: mediaType,
|
|
|
|
id,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-21 13:07:33 +02:00
|
|
|
const baseURL = "https://api.themoviedb.org/3";
|
2023-06-12 20:06:46 +02:00
|
|
|
|
2023-06-21 13:07:33 +02:00
|
|
|
const headers = {
|
|
|
|
accept: "application/json",
|
2023-06-23 21:58:33 +02:00
|
|
|
Authorization: `Bearer ${conf().TMDB_READ_API_KEY}`,
|
2023-06-21 13:07:33 +02:00
|
|
|
};
|
2023-06-12 20:06:46 +02:00
|
|
|
|
2023-06-21 14:04:37 +02:00
|
|
|
async function get<T>(url: string, params?: object): Promise<T> {
|
|
|
|
const res = await mwFetch<any>(encodeURI(url), {
|
2023-06-21 13:07:33 +02:00
|
|
|
headers,
|
|
|
|
baseURL,
|
2023-06-21 14:04:37 +02:00
|
|
|
params: {
|
|
|
|
...params,
|
|
|
|
},
|
2023-06-21 13:07:33 +02:00
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
2023-06-12 20:06:46 +02:00
|
|
|
|
2023-06-23 11:05:01 +02:00
|
|
|
export async function multiSearch(
|
|
|
|
query: string
|
|
|
|
): Promise<(TMDBMovieSearchResult | TMDBShowSearchResult)[]> {
|
|
|
|
const data = await get<TMDBSearchResult>(`search/multi`, {
|
|
|
|
query,
|
|
|
|
include_adult: false,
|
|
|
|
language: "en-US",
|
|
|
|
page: 1,
|
|
|
|
});
|
|
|
|
// filter out results that aren't movies or shows
|
|
|
|
const results = data.results.filter(
|
2023-06-30 12:20:01 +02:00
|
|
|
(r) =>
|
|
|
|
r.media_type === TMDBContentTypes.MOVIE ||
|
|
|
|
r.media_type === TMDBContentTypes.TV
|
2023-06-23 11:05:01 +02:00
|
|
|
);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function generateQuickSearchMediaUrl(
|
|
|
|
query: string
|
|
|
|
): Promise<string | undefined> {
|
|
|
|
const data = await multiSearch(query);
|
|
|
|
if (data.length === 0) return undefined;
|
|
|
|
const result = data[0];
|
2023-06-30 12:20:01 +02:00
|
|
|
const title =
|
|
|
|
result.media_type === TMDBContentTypes.MOVIE ? result.title : result.name;
|
2023-06-23 11:05:01 +02:00
|
|
|
|
2023-06-29 21:21:24 +02:00
|
|
|
return `/media/${TMDBIdToUrlId(
|
2023-06-30 12:20:01 +02:00
|
|
|
TMDBMediaToMediaType(result.media_type),
|
2023-06-29 21:21:24 +02:00
|
|
|
result.id.toString(),
|
|
|
|
title
|
|
|
|
)}`;
|
2023-06-23 11:05:01 +02:00
|
|
|
}
|
|
|
|
|
2023-06-22 22:37:16 +02:00
|
|
|
// Conditional type which for inferring the return type based on the content type
|
2023-06-30 12:20:01 +02:00
|
|
|
type MediaDetailReturn<T extends TMDBContentTypes> =
|
|
|
|
T extends TMDBContentTypes.MOVIE
|
|
|
|
? TMDBMovieData
|
|
|
|
: T extends TMDBContentTypes.TV
|
|
|
|
? TMDBShowData
|
|
|
|
: never;
|
2023-06-22 22:37:16 +02:00
|
|
|
|
|
|
|
export function getMediaDetails<
|
|
|
|
T extends TMDBContentTypes,
|
|
|
|
TReturn = MediaDetailReturn<T>
|
|
|
|
>(id: string, type: T): Promise<TReturn> {
|
2023-06-30 12:20:01 +02:00
|
|
|
if (type === TMDBContentTypes.MOVIE) {
|
2023-06-22 22:37:16 +02:00
|
|
|
return get<TReturn>(`/movie/${id}`);
|
2023-06-13 21:23:47 +02:00
|
|
|
}
|
2023-06-30 12:20:01 +02:00
|
|
|
if (type === TMDBContentTypes.TV) {
|
2023-06-22 22:37:16 +02:00
|
|
|
return get<TReturn>(`/tv/${id}`);
|
|
|
|
}
|
|
|
|
throw new Error("Invalid media type");
|
2023-06-21 13:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getMediaPoster(posterPath: string | null): string | undefined {
|
|
|
|
if (posterPath) return `https://image.tmdb.org/t/p/w185/${posterPath}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getEpisodes(
|
|
|
|
id: string,
|
|
|
|
season: number
|
|
|
|
): Promise<TMDBEpisodeShort[]> {
|
|
|
|
const data = await get<TMDBSeason>(`/tv/${id}/season/${season}`);
|
|
|
|
return data.episodes.map((e) => ({
|
|
|
|
id: e.id,
|
|
|
|
episode_number: e.episode_number,
|
|
|
|
title: e.name,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getExternalIds(
|
|
|
|
id: string,
|
|
|
|
type: TMDBContentTypes
|
|
|
|
): Promise<TMDBExternalIds> {
|
|
|
|
let data;
|
|
|
|
|
|
|
|
switch (type) {
|
2023-06-30 12:20:01 +02:00
|
|
|
case TMDBContentTypes.MOVIE:
|
2023-06-21 13:07:33 +02:00
|
|
|
data = await get<TMDBMovieExternalIds>(`/movie/${id}/external_ids`);
|
|
|
|
break;
|
2023-06-30 12:20:01 +02:00
|
|
|
case TMDBContentTypes.TV:
|
2023-06-21 13:07:33 +02:00
|
|
|
data = await get<TMDBShowExternalIds>(`/tv/${id}/external_ids`);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid media type");
|
2023-06-16 11:18:32 +02:00
|
|
|
}
|
2023-06-21 13:07:33 +02:00
|
|
|
|
|
|
|
return data;
|
2023-06-13 21:23:47 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 18:16:41 +02:00
|
|
|
export async function getMovieFromExternalId(
|
|
|
|
imdbId: string
|
|
|
|
): Promise<string | undefined> {
|
|
|
|
const data = await get<ExternalIdMovieSearchResult>(`/find/${imdbId}`, {
|
|
|
|
external_source: "imdb_id",
|
|
|
|
});
|
|
|
|
|
|
|
|
const movie = data.movie_results[0];
|
|
|
|
if (!movie) return undefined;
|
|
|
|
|
|
|
|
return movie.id.toString();
|
|
|
|
}
|
|
|
|
|
2023-06-21 13:54:34 +02:00
|
|
|
export function formatTMDBSearchResult(
|
2023-06-30 12:20:01 +02:00
|
|
|
result: TMDBMovieSearchResult | TMDBShowSearchResult,
|
2023-06-13 21:23:47 +02:00
|
|
|
mediatype: TMDBContentTypes
|
2023-06-21 13:54:34 +02:00
|
|
|
): TMDBMediaResult {
|
2023-06-13 21:23:47 +02:00
|
|
|
const type = TMDBMediaToMediaType(mediatype);
|
2023-06-21 13:54:34 +02:00
|
|
|
if (type === MWMediaType.SERIES) {
|
2023-06-30 12:20:01 +02:00
|
|
|
const show = result as TMDBShowSearchResult;
|
2023-06-21 13:54:34 +02:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
2023-06-30 12:20:01 +02:00
|
|
|
|
|
|
|
const movie = result as TMDBMovieSearchResult;
|
2023-06-13 21:23:47 +02:00
|
|
|
|
|
|
|
return {
|
2023-06-21 13:54:34 +02:00
|
|
|
title: movie.title,
|
|
|
|
poster: getMediaPoster(movie.poster_path),
|
|
|
|
id: movie.id,
|
|
|
|
original_release_year: new Date(movie.release_date).getFullYear(),
|
|
|
|
object_type: mediatype,
|
2023-06-13 21:23:47 +02:00
|
|
|
};
|
2023-06-12 20:06:46 +02:00
|
|
|
}
|