movie-web/src/backend/metadata/getmeta.ts

215 lines
5.3 KiB
TypeScript
Raw Normal View History

2023-01-15 15:01:07 +00:00
import { FetchError } from "ofetch";
2023-06-12 19:25:24 +00:00
import { formatJWMeta, mediaTypeToJW } from "./justwatch";
2023-06-13 09:01:07 +00:00
import {
TMDBMediaToMediaType,
Tmdb,
formatTMDBMeta,
mediaTypeToTMDB,
} from "./tmdb";
2023-01-15 15:01:07 +00:00
import {
JWMediaResult,
JWSeasonMetaResult,
2023-01-15 15:01:07 +00:00
JW_API_BASE,
2023-06-12 19:25:24 +00:00
MWMediaMeta,
MWMediaType,
TMDBMediaResult,
2023-06-13 08:41:54 +00:00
TMDBMovieData,
TMDBSeasonMetaResult,
2023-06-13 08:41:54 +00:00
TMDBShowData,
2023-06-12 19:25:24 +00:00
} from "./types";
import { makeUrl, proxiedFetch } from "../helpers/fetch";
type JWExternalIdType =
| "eidr"
| "imdb_latest"
| "imdb"
| "tmdb_latest"
| "tmdb"
| "tms";
interface JWExternalId {
provider: JWExternalIdType;
external_id: string;
}
interface JWDetailedMeta extends JWMediaResult {
external_ids: JWExternalId[];
}
export interface DetailedMeta {
meta: MWMediaMeta;
2023-05-21 16:12:45 +00:00
imdbId?: string;
2023-05-21 19:00:35 +00:00
tmdbId?: string;
}
2023-06-15 20:13:19 +00:00
export function formatTMDBMetaResult(
2023-06-14 05:48:31 +00:00
details: TMDBShowData | TMDBMovieData,
type: MWMediaType
2023-06-21 10:43:36 +00:00
): TMDBMediaResult {
2023-06-14 05:48:31 +00:00
if (type === MWMediaType.MOVIE) {
2023-06-21 10:47:09 +00:00
const movie = details as TMDBMovieData;
2023-06-21 10:48:33 +00:00
return {
2023-06-14 05:48:31 +00:00
id: details.id,
2023-06-21 10:47:09 +00:00
title: movie.title,
2023-06-14 05:48:31 +00:00
object_type: mediaTypeToTMDB(type),
2023-06-21 10:47:09 +00:00
poster: movie.poster_path ?? undefined,
original_release_year: new Date(movie.release_date).getFullYear(),
2023-06-14 05:48:31 +00:00
};
}
if (type === MWMediaType.SERIES) {
2023-06-21 10:47:09 +00:00
const show = details as TMDBShowData;
2023-06-21 10:48:33 +00:00
return {
2023-06-14 05:48:31 +00:00
id: details.id,
2023-06-21 10:47:09 +00:00
title: show.name,
2023-06-14 05:48:31 +00:00
object_type: mediaTypeToTMDB(type),
2023-06-21 10:47:09 +00:00
seasons: show.seasons.map((v) => ({
2023-06-14 05:48:31 +00:00
id: v.id,
season_number: v.season_number,
title: v.name,
})),
2023-06-15 09:06:24 +00:00
poster: (details as TMDBMovieData).poster_path ?? undefined,
original_release_year: new Date(show.first_air_date).getFullYear(),
2023-06-14 05:48:31 +00:00
};
}
2023-06-21 10:43:36 +00:00
2023-06-21 10:48:33 +00:00
throw new Error("unsupported type");
2023-06-14 05:48:31 +00:00
}
export async function getMetaFromId(
type: MWMediaType,
id: string,
seasonId?: string
2023-06-13 08:41:54 +00:00
): Promise<DetailedMeta | null> {
const details = await Tmdb.getMediaDetails(id, mediaTypeToTMDB(type));
2023-06-13 08:41:54 +00:00
if (!details) return null;
2023-06-16 09:18:32 +00:00
const externalIds = await Tmdb.getExternalIds(id, mediaTypeToTMDB(type));
const imdbId = externalIds.imdb_id ?? undefined;
2023-06-13 08:41:54 +00:00
let seasonData: TMDBSeasonMetaResult | undefined;
2023-06-13 08:41:54 +00:00
if (type === MWMediaType.SERIES) {
const seasons = (details as TMDBShowData).seasons;
const season =
seasons?.find((v) => v.id.toString() === seasonId) ?? seasons?.[0];
const episodes = await Tmdb.getEpisodes(
details.id.toString(),
2023-06-20 17:39:16 +00:00
season.season_number === null || season.season_number === 0
? 1
: season.season_number
2023-06-13 08:41:54 +00:00
);
if (season && episodes) {
seasonData = {
id: season.id.toString(),
2023-06-20 17:39:16 +00:00
season_number:
season.season_number === null || season.season_number === 0
? 1
: season.season_number,
2023-06-13 08:41:54 +00:00
title: season.name,
episodes,
};
}
}
2023-06-15 20:13:19 +00:00
const tmdbmeta = formatTMDBMetaResult(details, type);
2023-06-14 05:48:31 +00:00
if (!tmdbmeta) return null;
const meta = formatTMDBMeta(tmdbmeta, seasonData);
if (!meta) return null;
2023-06-13 08:41:54 +00:00
return {
meta,
imdbId,
tmdbId: id,
};
}
export async function getLegacyMetaFromId(
type: MWMediaType,
id: string,
seasonId?: string
2023-01-15 15:01:07 +00:00
): Promise<DetailedMeta | null> {
const queryType = mediaTypeToJW(type);
let data: JWDetailedMeta;
try {
const url = makeUrl("/content/titles/{type}/{id}/locale/en_US", {
type: queryType,
id,
});
data = await proxiedFetch<JWDetailedMeta>(url, { baseURL: JW_API_BASE });
2023-01-15 15:01:07 +00:00
} catch (err) {
if (err instanceof FetchError) {
// 400 and 404 are treated as not found
if (err.statusCode === 400 || err.statusCode === 404) return null;
}
throw err;
}
2023-03-10 19:54:56 +00:00
let imdbId = data.external_ids.find(
(v) => v.provider === "imdb_latest"
2023-03-10 19:59:10 +00:00
)?.external_id;
2023-03-10 19:54:56 +00:00
if (!imdbId)
2023-03-10 19:59:10 +00:00
imdbId = data.external_ids.find((v) => v.provider === "imdb")?.external_id;
2023-03-10 19:54:56 +00:00
2023-05-21 19:00:35 +00:00
let tmdbId = data.external_ids.find(
(v) => v.provider === "tmdb_latest"
)?.external_id;
if (!tmdbId)
tmdbId = data.external_ids.find((v) => v.provider === "tmdb")?.external_id;
let seasonData: JWSeasonMetaResult | undefined;
if (data.object_type === "show") {
const seasonToScrape = seasonId ?? data.seasons?.[0].id.toString() ?? "";
const url = makeUrl("/content/titles/show_season/{id}/locale/en_US", {
id: seasonToScrape,
});
seasonData = await proxiedFetch<any>(url, { baseURL: JW_API_BASE });
}
return {
meta: formatJWMeta(data, seasonData),
imdbId,
2023-05-21 19:00:35 +00:00
tmdbId,
};
}
2023-06-13 09:01:07 +00:00
export function TMDBMediaToId(media: MWMediaMeta): string {
return ["tmdb", mediaTypeToTMDB(media.type), media.id].join("-");
2023-06-13 09:01:07 +00:00
}
export function decodeTMDBId(
2023-06-13 09:01:07 +00:00
paramId: string
): { id: string; type: MWMediaType } | null {
const [prefix, type, id] = paramId.split("-", 3);
if (prefix !== "tmdb") return null;
2023-06-13 09:01:07 +00:00
let mediaType;
try {
mediaType = TMDBMediaToMediaType(type);
2023-06-13 09:01:07 +00:00
} catch {
return null;
}
return {
type: mediaType,
id,
};
}
2023-06-13 12:06:37 +00:00
export async function convertLegacyUrl(
url: string
): Promise<string | undefined> {
if (url.startsWith("/media/JW")) {
const urlParts = url.split("/").slice(2);
const [, type, id] = urlParts[0].split("-", 3);
const meta = await getLegacyMetaFromId(TMDBMediaToMediaType(type), id);
2023-06-13 12:06:37 +00:00
if (!meta) return undefined;
const tmdbId = meta.tmdbId;
if (!tmdbId) return undefined;
return `/media/tmdb-${type}-${tmdbId}`;
2023-06-13 12:06:37 +00:00
}
return undefined;
}