2023-09-08 13:33:29 +02:00
|
|
|
import * as cache from "@lib/cache/cache.ts";
|
|
|
|
import * as openai from "@lib/openai.ts";
|
|
|
|
import { GenericResource } from "@lib/types.ts";
|
|
|
|
import { parseRating } from "@lib/helpers.ts";
|
|
|
|
|
|
|
|
type RecommendationResource = {
|
|
|
|
id: string;
|
|
|
|
type: string;
|
|
|
|
rating: number;
|
|
|
|
tags?: string[];
|
|
|
|
keywords?: string[];
|
|
|
|
author?: string;
|
|
|
|
year?: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function createRecommendationResource(
|
|
|
|
res: GenericResource,
|
|
|
|
description?: string,
|
|
|
|
) {
|
|
|
|
const cacheId = `recommendations:${res.type}:${res.id}`;
|
|
|
|
const resource: RecommendationResource = await cache.get(cacheId) || {
|
|
|
|
id: res.id,
|
|
|
|
type: res.type,
|
|
|
|
rating: -1,
|
|
|
|
};
|
|
|
|
if (description && !resource.keywords) {
|
|
|
|
const keywords = await openai.createKeywords(res.type, description);
|
|
|
|
if (keywords?.length) {
|
|
|
|
resource.keywords = keywords;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const { author, date, rating } = res.meta || {};
|
|
|
|
|
|
|
|
if (res?.tags) {
|
|
|
|
resource.tags = res.tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof rating !== "undefined") {
|
|
|
|
resource.rating = parseRating(rating);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (author) {
|
|
|
|
resource.author = author;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (date) {
|
|
|
|
const d = typeof date === "string" ? new Date(date) : date;
|
|
|
|
resource.year = d.getFullYear();
|
|
|
|
}
|
|
|
|
|
|
|
|
cache.set(cacheId, JSON.stringify(resource));
|
|
|
|
}
|
2023-09-08 14:01:35 +02:00
|
|
|
|
|
|
|
export async function getAllRecommendations() {
|
|
|
|
const keys = await cache.keys("recommendations:movie:*");
|
|
|
|
return Promise.all(keys.map((k) => cache.get(k))).then((res) =>
|
|
|
|
res.map((r) => JSON.parse(r))
|
|
|
|
);
|
|
|
|
}
|