73 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			73 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import {
 | |
|   CreditsResponse,
 | |
|   MovieDb,
 | |
|   MovieResponse,
 | |
|   MovieResultsResponse,
 | |
|   ShowResponse,
 | |
|   TvResultsResponse,
 | |
| } from "https://esm.sh/moviedb-promise@3.4.1";
 | |
| import { createCache } from "@lib/cache.ts";
 | |
| const moviedb = new MovieDb(Deno.env.get("TMDB_API_KEY") || "");
 | |
| 
 | |
| const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
 | |
| const cache = createCache("the-movie-db", { expires: CACHE_INTERVAL });
 | |
| 
 | |
| 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;
 | |
| };
 | |
| 
 | |
| 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;
 | |
| };
 | |
| 
 | |
| 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;
 | |
| };
 | |
| 
 | |
| 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;
 | |
| };
 | |
| 
 | |
| 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;
 | |
| };
 | |
| 
 | |
| 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;
 | |
| };
 | |
| 
 | |
| export function getMovieGenre() {
 | |
|   return moviedb.genreTvList();
 | |
| }
 | |
| 
 | |
| 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;
 | |
| }
 |