feat: completely remove redis
This commit is contained in:
111
lib/tmdb.ts
111
lib/tmdb.ts
@@ -1,74 +1,69 @@
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
import { MovieDb } from "https://esm.sh/moviedb-promise@3.4.1";
|
||||
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({ expires: CACHE_INTERVAL });
|
||||
|
||||
export const searchMovie = (query: string, year?: number) =>
|
||||
cache.cacheFunction({
|
||||
fn: () => moviedb.searchMovie({ query, year }),
|
||||
id: `query:moviesearch:${query}${year ? `-${year}` : ""}`,
|
||||
options: {
|
||||
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 = (query: string) =>
|
||||
cache.cacheFunction(
|
||||
{
|
||||
fn: () => moviedb.searchTv({ query }),
|
||||
id: `query:tvshowsearch:${query}`,
|
||||
options: {
|
||||
expires: CACHE_INTERVAL,
|
||||
},
|
||||
},
|
||||
);
|
||||
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 = (id: number) =>
|
||||
cache.cacheFunction({
|
||||
fn: () => moviedb.movieInfo({ id }),
|
||||
id: `query:movie:${id}`,
|
||||
options: {
|
||||
expires: CACHE_INTERVAL,
|
||||
},
|
||||
});
|
||||
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 = (id: number) =>
|
||||
cache.cacheFunction({
|
||||
fn: () => moviedb.tvInfo({ id }),
|
||||
id: `query:tvshow:${id}`,
|
||||
options: {
|
||||
expires: CACHE_INTERVAL,
|
||||
},
|
||||
});
|
||||
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 = (id: number) =>
|
||||
cache.cacheFunction({
|
||||
fn: () => moviedb.movieCredits(id),
|
||||
id: `query:moviecredits:${id}`,
|
||||
options: {
|
||||
expires: CACHE_INTERVAL,
|
||||
},
|
||||
});
|
||||
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 = (id: number) =>
|
||||
cache.cacheFunction({
|
||||
fn: () => moviedb.tvCredits(id),
|
||||
id: `query:tvshowcredits:${id}`,
|
||||
options: {
|
||||
expires: CACHE_INTERVAL,
|
||||
},
|
||||
});
|
||||
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 async function getMovieGenre(id: number) {
|
||||
const genres = await cache.get("/genres/movies");
|
||||
export function getMovieGenre() {
|
||||
return moviedb.genreTvList();
|
||||
}
|
||||
|
||||
export async function getSeriesGenre(id: number) {
|
||||
const genres = await cache.get("/genres/series");
|
||||
}
|
||||
|
||||
export async function getMoviePoster(id: string): Promise<ArrayBuffer> {
|
||||
const posterUrl = `https://image.tmdb.org/t/p/original/${id}`;
|
||||
const response = await fetch(posterUrl);
|
||||
|
||||
Reference in New Issue
Block a user