1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-29 16:07:40 +01:00

finish initial refactor

This commit is contained in:
castdrian 2023-06-13 10:41:54 +02:00 committed by Adrian Castro
parent c17f8a15e8
commit 3af98373fb
5 changed files with 101 additions and 39 deletions

View file

@ -1,12 +1,17 @@
import { FetchError } from "ofetch"; import { FetchError } from "ofetch";
import { formatJWMeta, mediaTypeToJW } from "./justwatch"; import { formatJWMeta, mediaTypeToJW } from "./justwatch";
import { Tmdb } from "./tmdb";
import { Trakt, formatTTVMeta } from "./trakttv";
import { import {
JWMediaResult, JWMediaResult,
JWSeasonMetaResult, JWSeasonMetaResult,
JW_API_BASE, JW_API_BASE,
MWMediaMeta, MWMediaMeta,
MWMediaType, MWMediaType,
TMDBMovieData,
TMDBShowData,
TTVSeasonMetaResult,
} from "./types"; } from "./types";
import { makeUrl, proxiedFetch } from "../helpers/fetch"; import { makeUrl, proxiedFetch } from "../helpers/fetch";
@ -37,6 +42,56 @@ export async function getMetaFromId(
type: MWMediaType, type: MWMediaType,
id: string, id: string,
seasonId?: 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> { ): Promise<DetailedMeta | null> {
const queryType = mediaTypeToJW(type); const queryType = mediaTypeToJW(type);

View file

@ -16,6 +16,7 @@ export async function searchForMedia(query: MWQuery): Promise<MWMediaMeta[]> {
const contentType = mediaTypeToTTV(type); const contentType = mediaTypeToTTV(type);
const results = await Trakt.search(searchQuery, contentType); const results = await Trakt.search(searchQuery, contentType);
console.log(results[0]);
cache.set(query, results, 3600); cache.set(query, results, 3600);
return results; return results;
} }

View file

@ -47,31 +47,4 @@ export abstract class Tmdb {
public static getMediaPoster(posterPath: string | null): string | undefined { public static getMediaPoster(posterPath: string | null): string | undefined {
if (posterPath) return `https://image.tmdb.org/t/p/w185/${posterPath}`; 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,
};
} */
} }

View file

@ -2,12 +2,13 @@ import { conf } from "@/setup/config";
import { Tmdb } from "./tmdb"; import { Tmdb } from "./tmdb";
import { import {
DetailedMeta,
MWMediaMeta, MWMediaMeta,
MWMediaType, MWMediaType,
MWSeasonMeta, MWSeasonMeta,
TMDBShowData, TMDBShowData,
TTVContentTypes, TTVContentTypes,
TTVEpisodeResult,
TTVEpisodeShort,
TTVMediaResult, TTVMediaResult,
TTVSearchResult, TTVSearchResult,
TTVSeasonMetaResult, TTVSeasonMetaResult,
@ -69,14 +70,14 @@ export function formatTTVMeta(
} }
export function TTVMediaToId(media: MWMediaMeta): string { 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( export function decodeTTVId(
paramId: string paramId: string
): { id: string; type: MWMediaType } | null { ): { id: string; type: MWMediaType } | null {
const [prefix, type, id] = paramId.split("-", 3); const [prefix, type, id] = paramId.split("-", 3);
if (prefix !== "TTV") return null; if (prefix !== "MW") return null;
let mediaType; let mediaType;
try { try {
mediaType = TTVMediaToMediaType(type); mediaType = TTVMediaToMediaType(type);
@ -101,7 +102,6 @@ export async function formatTTVSearchResult(
media.ids.tmdb.toString(), media.ids.tmdb.toString(),
TTVMediaToMediaType(result.type) TTVMediaToMediaType(result.type)
); );
console.log(details);
const seasons = const seasons =
type === MWMediaType.SERIES type === MWMediaType.SERIES
@ -115,7 +115,7 @@ export async function formatTTVSearchResult(
return { return {
title: media.title, title: media.title,
poster: Tmdb.getMediaPoster(details.poster_path), poster: Tmdb.getMediaPoster(details.poster_path),
id: media.ids.trakt, id: media.ids.tmdb,
original_release_year: media.year, original_release_year: media.year,
ttv_entity_id: media.ids.slug, ttv_entity_id: media.ids.slug,
object_type: mediaTypeToTTV(type), object_type: mediaTypeToTTV(type),
@ -155,12 +155,33 @@ export abstract class Trakt {
return formatted.map((v) => formatTTVMeta(v)); return formatted.map((v) => formatTTVMeta(v));
} }
public static async getMetaFromId( public static async searchById(
type: MWMediaType, tmdbId: string,
id: string, type: "movie" | "show"
seasonId?: string ): Promise<TTVMediaResult> {
): Promise<DetailedMeta | null> { const data = await Trakt.get<TTVSearchResult[]>(
console.log("getMetaFromId", type, id, seasonId); `/search/tmdb/${tmdbId}?type=${type}`
return null; );
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,
}));
} }
} }

View file

@ -311,3 +311,15 @@ export type JWSeasonMetaResult = {
season_number: number; season_number: number;
episodes: JWEpisodeShort[]; episodes: JWEpisodeShort[];
}; };
export interface TTVEpisodeResult {
season: number;
number: number;
title: string;
ids: {
trakt: number;
tvdb: number;
imdb: string;
tmdb: number;
};
}