From 1408fcde9300def772923c83fb6bd203344451c9 Mon Sep 17 00:00:00 2001 From: adrifcastr Date: Wed, 21 Jun 2023 13:07:33 +0200 Subject: [PATCH] export functions directly --- src/backend/metadata/getmeta.ts | 10 +- src/backend/metadata/search.ts | 4 +- src/backend/metadata/tmdb.ts | 168 ++++++++++++++--------------- src/components/media/MediaCard.tsx | 4 +- 4 files changed, 88 insertions(+), 98 deletions(-) diff --git a/src/backend/metadata/getmeta.ts b/src/backend/metadata/getmeta.ts index 4548a436..aa4267c5 100644 --- a/src/backend/metadata/getmeta.ts +++ b/src/backend/metadata/getmeta.ts @@ -3,8 +3,10 @@ import { FetchError } from "ofetch"; import { formatJWMeta, mediaTypeToJW } from "./justwatch"; import { TMDBMediaToMediaType, - Tmdb, formatTMDBMeta, + getEpisodes, + getExternalIds, + getMediaDetails, mediaTypeToTMDB, } from "./tmdb"; import { @@ -81,11 +83,11 @@ export async function getMetaFromId( id: string, seasonId?: string ): Promise { - const details = await Tmdb.getMediaDetails(id, mediaTypeToTMDB(type)); + const details = await getMediaDetails(id, mediaTypeToTMDB(type)); 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; let seasonData: TMDBSeasonMetaResult | undefined; @@ -95,7 +97,7 @@ export async function getMetaFromId( const season = seasons?.find((v) => v.id.toString() === seasonId) ?? seasons?.[0]; - const episodes = await Tmdb.getEpisodes( + const episodes = await getEpisodes( details.id.toString(), season.season_number === null || season.season_number === 0 ? 1 diff --git a/src/backend/metadata/search.ts b/src/backend/metadata/search.ts index 549d7ba4..9e2883d4 100644 --- a/src/backend/metadata/search.ts +++ b/src/backend/metadata/search.ts @@ -1,10 +1,10 @@ import { SimpleCache } from "@/utils/cache"; import { - Tmdb, formatTMDBMeta, formatTMDBSearchResult, mediaTypeToTMDB, + searchMedia, } from "./tmdb"; import { MWMediaMeta, MWQuery } from "./types"; @@ -18,7 +18,7 @@ export async function searchForMedia(query: MWQuery): Promise { if (cache.has(query)) return cache.get(query) as MWMediaMeta[]; const { searchQuery, type } = query; - const data = await Tmdb.searchMedia(searchQuery, mediaTypeToTMDB(type)); + const data = await searchMedia(searchQuery, mediaTypeToTMDB(type)); const results = await Promise.all( data.results.map(async (v) => { const formattedResult = await formatTMDBSearchResult( diff --git a/src/backend/metadata/tmdb.ts b/src/backend/metadata/tmdb.ts index 0df2df7e..cf327070 100644 --- a/src/backend/metadata/tmdb.ts +++ b/src/backend/metadata/tmdb.ts @@ -8,12 +8,10 @@ import { TMDBEpisodeShort, TMDBExternalIds, TMDBMediaResult, - TMDBMediaStatic, TMDBMovieData, TMDBMovieExternalIds, TMDBMovieResponse, TMDBMovieResult, - TMDBSearchResultStatic, TMDBSeason, TMDBSeasonMetaResult, TMDBShowData, @@ -98,103 +96,93 @@ export function decodeTMDBId( }; } -export abstract class Tmdb { - private static baseURL = "https://api.themoviedb.org/3"; +const baseURL = "https://api.themoviedb.org/3"; - private static headers = { - accept: "application/json", - Authorization: `Bearer ${conf().TMDB_API_KEY}`, - }; +const headers = { + accept: "application/json", + Authorization: `Bearer ${conf().TMDB_API_KEY}`, +}; - private static async get(url: string): Promise { - const res = await mwFetch(url, { - headers: Tmdb.headers, - baseURL: Tmdb.baseURL, - }); - return res; +async function get(url: string): Promise { + const res = await mwFetch(url, { + headers, + baseURL, + }); + return res; +} + +export async function searchMedia(query: string, type: TMDBContentTypes) { + let data; + + switch (type) { + case "movie": + data = await get( + `search/movie?query=${query}&include_adult=false&language=en-US&page=1` + ); + break; + case "show": + data = await get( + `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 ( - query: string, - type: TMDBContentTypes - ) => { - let data; + return data; +} - switch (type) { - case "movie": - data = await Tmdb.get( - `search/movie?query=${query}&include_adult=false&language=en-US&page=1` - ); - break; - case "show": - data = await Tmdb.get( - `search/tv?query=${query}&include_adult=false&language=en-US&page=1` - ); - break; - default: - throw new Error("Invalid media type"); - } +export async function getMediaDetails(id: string, type: TMDBContentTypes) { + let data; - return data; - }; - - public static getMediaDetails: TMDBMediaStatic["getMediaDetails"] = async ( - id: string, - type: TMDBContentTypes - ) => { - let data; - - switch (type) { - case "movie": - data = await Tmdb.get(`/movie/${id}`); - break; - case "show": - data = await Tmdb.get(`/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}`; + switch (type) { + case "movie": + data = await get(`/movie/${id}`); + break; + case "show": + data = await get(`/tv/${id}`); + break; + default: + throw new Error("Invalid media type"); } - public static async getEpisodes( - id: string, - season: number - ): Promise { - const data = await Tmdb.get(`/tv/${id}/season/${season}`); - return data.episodes.map((e) => ({ - id: e.id, - episode_number: e.episode_number, - title: e.name, - })); + return data; +} + +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 { + const data = await get(`/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 { + let data; + + switch (type) { + case "movie": + data = await get(`/movie/${id}/external_ids`); + break; + case "show": + data = await get(`/tv/${id}/external_ids`); + break; + default: + throw new Error("Invalid media type"); } - public static async getExternalIds( - id: string, - type: TMDBContentTypes - ): Promise { - let data; - - switch (type) { - case "movie": - data = await Tmdb.get( - `/movie/${id}/external_ids` - ); - break; - case "show": - data = await Tmdb.get(`/tv/${id}/external_ids`); - break; - default: - throw new Error("Invalid media type"); - } - - return data; - } + return data; } export async function formatTMDBSearchResult( @@ -208,7 +196,7 @@ export async function formatTMDBSearchResult( type === MWMediaType.SERIES ? (result as TMDBShowResult).name : (result as TMDBMovieResult).title, - poster: Tmdb.getMediaPoster(result.poster_path), + poster: getMediaPoster(result.poster_path), id: result.id, original_release_year: type === MWMediaType.SERIES diff --git a/src/components/media/MediaCard.tsx b/src/components/media/MediaCard.tsx index ece6d293..fd460bb7 100644 --- a/src/components/media/MediaCard.tsx +++ b/src/components/media/MediaCard.tsx @@ -2,7 +2,7 @@ import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; 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 { DotList } from "@/components/text/DotList"; @@ -57,7 +57,7 @@ function MediaCardContent({ ].join(" ")} style={{ backgroundImage: media.poster - ? `url(${Tmdb.getMediaPoster(media.poster)})` + ? `url(${getMediaPoster(media.poster)})` : undefined, }} >