2025-01-06 16:14:29 +01:00
|
|
|
import {
|
|
|
|
CreditsResponse,
|
|
|
|
MovieDb,
|
|
|
|
MovieResponse,
|
|
|
|
MovieResultsResponse,
|
|
|
|
ShowResponse,
|
|
|
|
TvResultsResponse,
|
|
|
|
} from "https://esm.sh/moviedb-promise@3.4.1";
|
|
|
|
import { createCache } from "@lib/cache.ts";
|
2023-07-31 04:19:04 +02:00
|
|
|
const moviedb = new MovieDb(Deno.env.get("TMDB_API_KEY") || "");
|
|
|
|
|
2023-08-09 23:51:40 +02:00
|
|
|
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
|
2025-01-19 16:43:00 +01:00
|
|
|
const cache = createCache("the-movie-db", { expires: CACHE_INTERVAL });
|
2023-07-31 04:19:04 +02:00
|
|
|
|
2025-01-06 16:14:29 +01:00
|
|
|
export const searchMovie = async (query: string, year?: number) => {
|
|
|
|
const id = `query:moviesearch:${query}${year ? `-${year}` : ""}`;
|
|
|
|
if (cache.has(id)) return cache.get(id) as MovieResultsResponse;
|
|
|
|
const res = await moviedb.searchMovie({ query, year });
|
|
|
|
cache.set(id, res);
|
|
|
|
return res;
|
|
|
|
};
|
2023-08-07 14:44:04 +02:00
|
|
|
|
2025-01-06 16:14:29 +01:00
|
|
|
export const searchTVShow = async (query: string) => {
|
|
|
|
const id = `query:tvshowsearch:${query}`;
|
|
|
|
if (cache.has(id)) return cache.get(id) as TvResultsResponse;
|
|
|
|
const res = await moviedb.searchTv({ query });
|
|
|
|
cache.set(id, res);
|
|
|
|
return res;
|
|
|
|
};
|
2023-07-31 04:19:04 +02:00
|
|
|
|
2025-01-06 16:14:29 +01:00
|
|
|
export const getMovie = async (id: number) => {
|
|
|
|
const cacheId = `query:movie:${id}`;
|
|
|
|
if (cache.has(cacheId)) return cache.get(cacheId) as MovieResponse;
|
|
|
|
const res = await moviedb.movieInfo({ id });
|
|
|
|
cache.set(cacheId, res);
|
|
|
|
return res;
|
|
|
|
};
|
2023-08-08 21:50:23 +02:00
|
|
|
|
2025-01-06 16:14:29 +01:00
|
|
|
export const getSeries = async (id: number) => {
|
|
|
|
const cacheId = `query:tvshow:${id}`;
|
|
|
|
if (cache.has(cacheId)) return cache.get(cacheId) as ShowResponse;
|
|
|
|
const res = await moviedb.tvInfo({ id });
|
|
|
|
cache.set(cacheId, res);
|
|
|
|
return res;
|
|
|
|
};
|
2023-07-31 17:21:17 +02:00
|
|
|
|
2025-01-06 16:14:29 +01:00
|
|
|
export const getMovieCredits = async (id: number) => {
|
|
|
|
const cacheId = `query:moviecredits:${id}`;
|
|
|
|
if (cache.has(cacheId)) return cache.get(cacheId) as CreditsResponse;
|
|
|
|
const res = await moviedb.movieCredits(id);
|
|
|
|
cache.set(cacheId, res);
|
|
|
|
return res;
|
|
|
|
};
|
2023-08-09 23:51:40 +02:00
|
|
|
|
2025-01-06 16:14:29 +01:00
|
|
|
export const getSeriesCredits = async (id: number) => {
|
|
|
|
const cacheId = `query:tvshowcredits:${id}`;
|
|
|
|
if (cache.has(cacheId)) return cache.get(cacheId) as CreditsResponse;
|
|
|
|
const res = await moviedb.tvCredits(id);
|
|
|
|
cache.set(cacheId, res);
|
|
|
|
return res;
|
|
|
|
};
|
2023-08-08 21:50:23 +02:00
|
|
|
|
2025-01-06 16:14:29 +01:00
|
|
|
export function getMovieGenre() {
|
2023-08-08 21:50:23 +02:00
|
|
|
return moviedb.genreTvList();
|
|
|
|
}
|
|
|
|
|
2023-07-31 17:21:17 +02:00
|
|
|
export async function getMoviePoster(id: string): Promise<ArrayBuffer> {
|
|
|
|
const posterUrl = `https://image.tmdb.org/t/p/original/${id}`;
|
|
|
|
const response = await fetch(posterUrl);
|
|
|
|
const poster = await response.arrayBuffer();
|
|
|
|
return poster;
|
|
|
|
}
|