mirror of
https://github.com/movie-web/movie-web.git
synced 2024-11-14 08:45:08 +01:00
export functions directly
This commit is contained in:
parent
89cdf74b2f
commit
1408fcde93
@ -3,8 +3,10 @@ import { FetchError } from "ofetch";
|
|||||||
import { formatJWMeta, mediaTypeToJW } from "./justwatch";
|
import { formatJWMeta, mediaTypeToJW } from "./justwatch";
|
||||||
import {
|
import {
|
||||||
TMDBMediaToMediaType,
|
TMDBMediaToMediaType,
|
||||||
Tmdb,
|
|
||||||
formatTMDBMeta,
|
formatTMDBMeta,
|
||||||
|
getEpisodes,
|
||||||
|
getExternalIds,
|
||||||
|
getMediaDetails,
|
||||||
mediaTypeToTMDB,
|
mediaTypeToTMDB,
|
||||||
} from "./tmdb";
|
} from "./tmdb";
|
||||||
import {
|
import {
|
||||||
@ -81,11 +83,11 @@ export async function getMetaFromId(
|
|||||||
id: string,
|
id: string,
|
||||||
seasonId?: string
|
seasonId?: string
|
||||||
): Promise<DetailedMeta | null> {
|
): Promise<DetailedMeta | null> {
|
||||||
const details = await Tmdb.getMediaDetails(id, mediaTypeToTMDB(type));
|
const details = await getMediaDetails(id, mediaTypeToTMDB(type));
|
||||||
|
|
||||||
if (!details) return null;
|
if (!details) return null;
|
||||||
|
|
||||||
const externalIds = await Tmdb.getExternalIds(id, mediaTypeToTMDB(type));
|
const externalIds = await getExternalIds(id, mediaTypeToTMDB(type));
|
||||||
const imdbId = externalIds.imdb_id ?? undefined;
|
const imdbId = externalIds.imdb_id ?? undefined;
|
||||||
|
|
||||||
let seasonData: TMDBSeasonMetaResult | undefined;
|
let seasonData: TMDBSeasonMetaResult | undefined;
|
||||||
@ -95,7 +97,7 @@ export async function getMetaFromId(
|
|||||||
const season =
|
const season =
|
||||||
seasons?.find((v) => v.id.toString() === seasonId) ?? seasons?.[0];
|
seasons?.find((v) => v.id.toString() === seasonId) ?? seasons?.[0];
|
||||||
|
|
||||||
const episodes = await Tmdb.getEpisodes(
|
const episodes = await getEpisodes(
|
||||||
details.id.toString(),
|
details.id.toString(),
|
||||||
season.season_number === null || season.season_number === 0
|
season.season_number === null || season.season_number === 0
|
||||||
? 1
|
? 1
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import { SimpleCache } from "@/utils/cache";
|
import { SimpleCache } from "@/utils/cache";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Tmdb,
|
|
||||||
formatTMDBMeta,
|
formatTMDBMeta,
|
||||||
formatTMDBSearchResult,
|
formatTMDBSearchResult,
|
||||||
mediaTypeToTMDB,
|
mediaTypeToTMDB,
|
||||||
|
searchMedia,
|
||||||
} from "./tmdb";
|
} from "./tmdb";
|
||||||
import { MWMediaMeta, MWQuery } from "./types";
|
import { MWMediaMeta, MWQuery } from "./types";
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ export async function searchForMedia(query: MWQuery): Promise<MWMediaMeta[]> {
|
|||||||
if (cache.has(query)) return cache.get(query) as MWMediaMeta[];
|
if (cache.has(query)) return cache.get(query) as MWMediaMeta[];
|
||||||
const { searchQuery, type } = query;
|
const { searchQuery, type } = query;
|
||||||
|
|
||||||
const data = await Tmdb.searchMedia(searchQuery, mediaTypeToTMDB(type));
|
const data = await searchMedia(searchQuery, mediaTypeToTMDB(type));
|
||||||
const results = await Promise.all(
|
const results = await Promise.all(
|
||||||
data.results.map(async (v) => {
|
data.results.map(async (v) => {
|
||||||
const formattedResult = await formatTMDBSearchResult(
|
const formattedResult = await formatTMDBSearchResult(
|
||||||
|
@ -8,12 +8,10 @@ import {
|
|||||||
TMDBEpisodeShort,
|
TMDBEpisodeShort,
|
||||||
TMDBExternalIds,
|
TMDBExternalIds,
|
||||||
TMDBMediaResult,
|
TMDBMediaResult,
|
||||||
TMDBMediaStatic,
|
|
||||||
TMDBMovieData,
|
TMDBMovieData,
|
||||||
TMDBMovieExternalIds,
|
TMDBMovieExternalIds,
|
||||||
TMDBMovieResponse,
|
TMDBMovieResponse,
|
||||||
TMDBMovieResult,
|
TMDBMovieResult,
|
||||||
TMDBSearchResultStatic,
|
|
||||||
TMDBSeason,
|
TMDBSeason,
|
||||||
TMDBSeasonMetaResult,
|
TMDBSeasonMetaResult,
|
||||||
TMDBShowData,
|
TMDBShowData,
|
||||||
@ -98,103 +96,93 @@ export function decodeTMDBId(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class Tmdb {
|
const baseURL = "https://api.themoviedb.org/3";
|
||||||
private static baseURL = "https://api.themoviedb.org/3";
|
|
||||||
|
|
||||||
private static headers = {
|
const headers = {
|
||||||
accept: "application/json",
|
accept: "application/json",
|
||||||
Authorization: `Bearer ${conf().TMDB_API_KEY}`,
|
Authorization: `Bearer ${conf().TMDB_API_KEY}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
private static async get<T>(url: string): Promise<T> {
|
async function get<T>(url: string): Promise<T> {
|
||||||
const res = await mwFetch<any>(url, {
|
const res = await mwFetch<any>(url, {
|
||||||
headers: Tmdb.headers,
|
headers,
|
||||||
baseURL: Tmdb.baseURL,
|
baseURL,
|
||||||
});
|
});
|
||||||
return res;
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function searchMedia(query: string, type: TMDBContentTypes) {
|
||||||
|
let data;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case "movie":
|
||||||
|
data = await get<TMDBMovieResponse>(
|
||||||
|
`search/movie?query=${query}&include_adult=false&language=en-US&page=1`
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "show":
|
||||||
|
data = await get<TMDBShowResponse>(
|
||||||
|
`search/tv?query=${query}&include_adult=false&language=en-US&page=1`
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error("Invalid media type");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static searchMedia: TMDBSearchResultStatic["searchMedia"] = async (
|
return data;
|
||||||
query: string,
|
}
|
||||||
type: TMDBContentTypes
|
|
||||||
) => {
|
|
||||||
let data;
|
|
||||||
|
|
||||||
switch (type) {
|
export async function getMediaDetails(id: string, type: TMDBContentTypes) {
|
||||||
case "movie":
|
let data;
|
||||||
data = await Tmdb.get<TMDBMovieResponse>(
|
|
||||||
`search/movie?query=${query}&include_adult=false&language=en-US&page=1`
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case "show":
|
|
||||||
data = await Tmdb.get<TMDBShowResponse>(
|
|
||||||
`search/tv?query=${query}&include_adult=false&language=en-US&page=1`
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Error("Invalid media type");
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
switch (type) {
|
||||||
};
|
case "movie":
|
||||||
|
data = await get<TMDBMovieData>(`/movie/${id}`);
|
||||||
public static getMediaDetails: TMDBMediaStatic["getMediaDetails"] = async (
|
break;
|
||||||
id: string,
|
case "show":
|
||||||
type: TMDBContentTypes
|
data = await get<TMDBShowData>(`/tv/${id}`);
|
||||||
) => {
|
break;
|
||||||
let data;
|
default:
|
||||||
|
throw new Error("Invalid media type");
|
||||||
switch (type) {
|
|
||||||
case "movie":
|
|
||||||
data = await Tmdb.get<TMDBMovieData>(`/movie/${id}`);
|
|
||||||
break;
|
|
||||||
case "show":
|
|
||||||
data = await Tmdb.get<TMDBShowData>(`/tv/${id}`);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Error("Invalid media type");
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
};
|
|
||||||
|
|
||||||
public static getMediaPoster(posterPath: string | null): string | undefined {
|
|
||||||
if (posterPath) return `https://image.tmdb.org/t/p/w185/${posterPath}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async getEpisodes(
|
return data;
|
||||||
id: string,
|
}
|
||||||
season: number
|
|
||||||
): Promise<TMDBEpisodeShort[]> {
|
export function getMediaPoster(posterPath: string | null): string | undefined {
|
||||||
const data = await Tmdb.get<TMDBSeason>(`/tv/${id}/season/${season}`);
|
if (posterPath) return `https://image.tmdb.org/t/p/w185/${posterPath}`;
|
||||||
return data.episodes.map((e) => ({
|
}
|
||||||
id: e.id,
|
|
||||||
episode_number: e.episode_number,
|
export async function getEpisodes(
|
||||||
title: e.name,
|
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) {
|
||||||
|
case "movie":
|
||||||
|
data = await get<TMDBMovieExternalIds>(`/movie/${id}/external_ids`);
|
||||||
|
break;
|
||||||
|
case "show":
|
||||||
|
data = await get<TMDBShowExternalIds>(`/tv/${id}/external_ids`);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error("Invalid media type");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async getExternalIds(
|
return data;
|
||||||
id: string,
|
|
||||||
type: TMDBContentTypes
|
|
||||||
): Promise<TMDBExternalIds> {
|
|
||||||
let data;
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case "movie":
|
|
||||||
data = await Tmdb.get<TMDBMovieExternalIds>(
|
|
||||||
`/movie/${id}/external_ids`
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case "show":
|
|
||||||
data = await Tmdb.get<TMDBShowExternalIds>(`/tv/${id}/external_ids`);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Error("Invalid media type");
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function formatTMDBSearchResult(
|
export async function formatTMDBSearchResult(
|
||||||
@ -208,7 +196,7 @@ export async function formatTMDBSearchResult(
|
|||||||
type === MWMediaType.SERIES
|
type === MWMediaType.SERIES
|
||||||
? (result as TMDBShowResult).name
|
? (result as TMDBShowResult).name
|
||||||
: (result as TMDBMovieResult).title,
|
: (result as TMDBMovieResult).title,
|
||||||
poster: Tmdb.getMediaPoster(result.poster_path),
|
poster: getMediaPoster(result.poster_path),
|
||||||
id: result.id,
|
id: result.id,
|
||||||
original_release_year:
|
original_release_year:
|
||||||
type === MWMediaType.SERIES
|
type === MWMediaType.SERIES
|
||||||
|
@ -2,7 +2,7 @@ import { useTranslation } from "react-i18next";
|
|||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
|
|
||||||
import { TMDBMediaToId } from "@/backend/metadata/getmeta";
|
import { TMDBMediaToId } from "@/backend/metadata/getmeta";
|
||||||
import { Tmdb } from "@/backend/metadata/tmdb";
|
import { getMediaPoster } from "@/backend/metadata/tmdb";
|
||||||
import { MWMediaMeta } from "@/backend/metadata/types";
|
import { MWMediaMeta } from "@/backend/metadata/types";
|
||||||
import { DotList } from "@/components/text/DotList";
|
import { DotList } from "@/components/text/DotList";
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ function MediaCardContent({
|
|||||||
].join(" ")}
|
].join(" ")}
|
||||||
style={{
|
style={{
|
||||||
backgroundImage: media.poster
|
backgroundImage: media.poster
|
||||||
? `url(${Tmdb.getMediaPoster(media.poster)})`
|
? `url(${getMediaPoster(media.poster)})`
|
||||||
: undefined,
|
: undefined,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
Loading…
Reference in New Issue
Block a user