feat: add loading of recommendations to movie page
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
import * as openai from "@lib/openai.ts";
|
||||
import * as tmdb from "@lib/tmdb.ts";
|
||||
import { GenericResource } from "@lib/types.ts";
|
||||
import { parseRating } from "@lib/helpers.ts";
|
||||
|
||||
@@ -57,11 +58,39 @@ export async function createRecommendationResource(
|
||||
cache.set(cacheId, JSON.stringify(resource));
|
||||
}
|
||||
|
||||
export function getRecommendation(id: string, type: string) {
|
||||
return cache.get(`recommendations:${type}:${id}`);
|
||||
export async function getRecommendation(
|
||||
id: string,
|
||||
type: string,
|
||||
): Promise<RecommendationResource | null> {
|
||||
const res = await cache.get(`recommendations:${type}:${id}`) as string;
|
||||
try {
|
||||
return JSON.parse(res);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllRecommendations() {
|
||||
export async function getSimilarMovies(id: string) {
|
||||
const recs = await getRecommendation(id, "movie");
|
||||
if (!recs?.keywords?.length) return;
|
||||
|
||||
const recommendations = await openai.getMovieRecommendations(
|
||||
recs.keywords.join(),
|
||||
[recs.id],
|
||||
);
|
||||
if (!recommendations) return;
|
||||
|
||||
const movies = await Promise.all(recommendations.map(async (rec) => {
|
||||
const m = await tmdb.searchMovie(rec.title, rec.year);
|
||||
return m?.results?.[0];
|
||||
}));
|
||||
|
||||
return movies.filter(Boolean);
|
||||
}
|
||||
|
||||
export async function getAllRecommendations(): Promise<
|
||||
RecommendationResource[]
|
||||
> {
|
||||
const keys = await cache.keys("recommendations:movie:*");
|
||||
return Promise.all(keys.map((k) => cache.get(k))).then((res) =>
|
||||
res.map((r) => JSON.parse(r))
|
||||
|
||||
Reference in New Issue
Block a user