mirror of
https://github.com/movie-web/movie-web.git
synced 2024-12-26 17:51:57 +01:00
finish initial refactor
This commit is contained in:
parent
1eac9f886e
commit
e5ddb98162
@ -1,12 +1,17 @@
|
||||
import { FetchError } from "ofetch";
|
||||
|
||||
import { formatJWMeta, mediaTypeToJW } from "./justwatch";
|
||||
import { Tmdb } from "./tmdb";
|
||||
import { Trakt, formatTTVMeta } from "./trakttv";
|
||||
import {
|
||||
JWMediaResult,
|
||||
JWSeasonMetaResult,
|
||||
JW_API_BASE,
|
||||
MWMediaMeta,
|
||||
MWMediaType,
|
||||
TMDBMovieData,
|
||||
TMDBShowData,
|
||||
TTVSeasonMetaResult,
|
||||
} from "./types";
|
||||
import { makeUrl, proxiedFetch } from "../helpers/fetch";
|
||||
|
||||
@ -37,6 +42,56 @@ export async function getMetaFromId(
|
||||
type: MWMediaType,
|
||||
id: string,
|
||||
seasonId?: string
|
||||
): Promise<DetailedMeta | null> {
|
||||
const result = await Trakt.searchById(id, mediaTypeToJW(type));
|
||||
if (!result) return null;
|
||||
const details = await Tmdb.getMediaDetails(id, type);
|
||||
|
||||
if (!details) return null;
|
||||
|
||||
let imdbId;
|
||||
if (type === MWMediaType.MOVIE) {
|
||||
imdbId = (details as TMDBMovieData).imdb_id ?? undefined;
|
||||
}
|
||||
|
||||
let seasonData: TTVSeasonMetaResult | undefined;
|
||||
|
||||
if (type === MWMediaType.SERIES) {
|
||||
const seasons = (details as TMDBShowData).seasons;
|
||||
const season =
|
||||
seasons?.find((v) => v.id.toString() === seasonId) ?? seasons?.[0];
|
||||
|
||||
const episodes = await Trakt.getEpisodes(
|
||||
result.ttv_entity_id,
|
||||
season?.season_number ?? 1
|
||||
);
|
||||
|
||||
if (season && episodes) {
|
||||
seasonData = {
|
||||
id: season.id.toString(),
|
||||
season_number: season.season_number,
|
||||
title: season.name,
|
||||
episodes,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const meta = formatTTVMeta(result, seasonData);
|
||||
if (!meta) return null;
|
||||
|
||||
console.log(meta);
|
||||
|
||||
return {
|
||||
meta,
|
||||
imdbId,
|
||||
tmdbId: id,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getLegacyMetaFromId(
|
||||
type: MWMediaType,
|
||||
id: string,
|
||||
seasonId?: string
|
||||
): Promise<DetailedMeta | null> {
|
||||
const queryType = mediaTypeToJW(type);
|
||||
|
||||
|
@ -16,6 +16,7 @@ export async function searchForMedia(query: MWQuery): Promise<MWMediaMeta[]> {
|
||||
const contentType = mediaTypeToTTV(type);
|
||||
|
||||
const results = await Trakt.search(searchQuery, contentType);
|
||||
console.log(results[0]);
|
||||
cache.set(query, results, 3600);
|
||||
return results;
|
||||
}
|
||||
|
@ -47,31 +47,4 @@ export abstract class Tmdb {
|
||||
public static getMediaPoster(posterPath: string | null): string | undefined {
|
||||
if (posterPath) return `https://image.tmdb.org/t/p/w185/${posterPath}`;
|
||||
}
|
||||
|
||||
/* public static async getMetaFromId(
|
||||
type: MWMediaType,
|
||||
id: string,
|
||||
seasonId?: string
|
||||
): Promise<DetailedMeta | null> {
|
||||
console.log("getMetaFromId", type, id, seasonId);
|
||||
|
||||
const details = await Tmdb.getMediaDetails(id, type);
|
||||
|
||||
if (!details) return null;
|
||||
|
||||
let imdbId;
|
||||
if (type === MWMediaType.MOVIE) {
|
||||
imdbId = (details as TMDBMovieData).imdb_id ?? undefined;
|
||||
}
|
||||
|
||||
if (!meta.length) return null;
|
||||
|
||||
console.log(meta);
|
||||
|
||||
return {
|
||||
meta,
|
||||
imdbId,
|
||||
tmdbId: id,
|
||||
};
|
||||
} */
|
||||
}
|
||||
|
@ -2,12 +2,13 @@ import { conf } from "@/setup/config";
|
||||
|
||||
import { Tmdb } from "./tmdb";
|
||||
import {
|
||||
DetailedMeta,
|
||||
MWMediaMeta,
|
||||
MWMediaType,
|
||||
MWSeasonMeta,
|
||||
TMDBShowData,
|
||||
TTVContentTypes,
|
||||
TTVEpisodeResult,
|
||||
TTVEpisodeShort,
|
||||
TTVMediaResult,
|
||||
TTVSearchResult,
|
||||
TTVSeasonMetaResult,
|
||||
@ -69,14 +70,14 @@ export function formatTTVMeta(
|
||||
}
|
||||
|
||||
export function TTVMediaToId(media: MWMediaMeta): string {
|
||||
return ["TTV", mediaTypeToTTV(media.type), media.id].join("-");
|
||||
return ["MW", mediaTypeToTTV(media.type), media.id].join("-");
|
||||
}
|
||||
|
||||
export function decodeTTVId(
|
||||
paramId: string
|
||||
): { id: string; type: MWMediaType } | null {
|
||||
const [prefix, type, id] = paramId.split("-", 3);
|
||||
if (prefix !== "TTV") return null;
|
||||
if (prefix !== "MW") return null;
|
||||
let mediaType;
|
||||
try {
|
||||
mediaType = TTVMediaToMediaType(type);
|
||||
@ -101,7 +102,6 @@ export async function formatTTVSearchResult(
|
||||
media.ids.tmdb.toString(),
|
||||
TTVMediaToMediaType(result.type)
|
||||
);
|
||||
console.log(details);
|
||||
|
||||
const seasons =
|
||||
type === MWMediaType.SERIES
|
||||
@ -115,7 +115,7 @@ export async function formatTTVSearchResult(
|
||||
return {
|
||||
title: media.title,
|
||||
poster: Tmdb.getMediaPoster(details.poster_path),
|
||||
id: media.ids.trakt,
|
||||
id: media.ids.tmdb,
|
||||
original_release_year: media.year,
|
||||
ttv_entity_id: media.ids.slug,
|
||||
object_type: mediaTypeToTTV(type),
|
||||
@ -155,12 +155,33 @@ export abstract class Trakt {
|
||||
return formatted.map((v) => formatTTVMeta(v));
|
||||
}
|
||||
|
||||
public static async getMetaFromId(
|
||||
type: MWMediaType,
|
||||
id: string,
|
||||
seasonId?: string
|
||||
): Promise<DetailedMeta | null> {
|
||||
console.log("getMetaFromId", type, id, seasonId);
|
||||
return null;
|
||||
public static async searchById(
|
||||
tmdbId: string,
|
||||
type: "movie" | "show"
|
||||
): Promise<TTVMediaResult> {
|
||||
const data = await Trakt.get<TTVSearchResult[]>(
|
||||
`/search/tmdb/${tmdbId}?type=${type}`
|
||||
);
|
||||
|
||||
const formatted = await Promise.all(
|
||||
// eslint-disable-next-line no-return-await
|
||||
data.map(async (v) => await formatTTVSearchResult(v))
|
||||
);
|
||||
return formatted[0];
|
||||
}
|
||||
|
||||
public static async getEpisodes(
|
||||
slug: string,
|
||||
season: number
|
||||
): Promise<TTVEpisodeShort[]> {
|
||||
const data = await Trakt.get<TTVEpisodeResult[]>(
|
||||
`/shows/${slug}/seasons/${season}`
|
||||
);
|
||||
|
||||
return data.map((e) => ({
|
||||
id: e.ids.tmdb,
|
||||
episode_number: e.number,
|
||||
title: e.title,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
@ -311,3 +311,15 @@ export type JWSeasonMetaResult = {
|
||||
season_number: number;
|
||||
episodes: JWEpisodeShort[];
|
||||
};
|
||||
|
||||
export interface TTVEpisodeResult {
|
||||
season: number;
|
||||
number: number;
|
||||
title: string;
|
||||
ids: {
|
||||
trakt: number;
|
||||
tvdb: number;
|
||||
imdb: string;
|
||||
tmdb: number;
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user