feat: refactor whole bunch of stuff
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
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";
|
||||
import { createCache } from "@lib/cache.ts";
|
||||
import { GenericResource, ReviewResource } from "./marka/schema.ts";
|
||||
|
||||
type RecommendationResource = {
|
||||
id: string;
|
||||
@@ -18,42 +18,48 @@ type RecommendationResource = {
|
||||
const cache = createCache<RecommendationResource>("recommendations");
|
||||
|
||||
export async function createRecommendationResource(
|
||||
res: GenericResource,
|
||||
res: ReviewResource,
|
||||
description?: string,
|
||||
) {
|
||||
const cacheId = `${res.type}:${res.id.replaceAll(":", "")}`;
|
||||
const cacheId = `${res.type}:${res.name.replaceAll(":", "")}`;
|
||||
const resource = cache.get(cacheId) || {
|
||||
id: res.id,
|
||||
id: res.name,
|
||||
type: res.type,
|
||||
rating: -1,
|
||||
};
|
||||
if (description && !resource.keywords) {
|
||||
const keywords = await openai.createKeywords(res.type, description, res.id);
|
||||
const keywords = await openai.createKeywords(
|
||||
res.type,
|
||||
description,
|
||||
res.name,
|
||||
);
|
||||
if (keywords?.length) {
|
||||
resource.keywords = keywords;
|
||||
}
|
||||
}
|
||||
|
||||
const { author, date, rating } = res.meta || {};
|
||||
const { author, datePublished, reviewRating } = res.content;
|
||||
|
||||
if (res?.tags) {
|
||||
resource.tags = res.tags;
|
||||
if (res?.content?.keywords) {
|
||||
resource.keywords = res.content.keywords;
|
||||
}
|
||||
|
||||
if (typeof rating !== "undefined") {
|
||||
resource.rating = parseRating(rating);
|
||||
if (typeof reviewRating?.ratingValue !== "undefined") {
|
||||
resource.rating = parseRating(reviewRating?.ratingValue);
|
||||
}
|
||||
|
||||
if (author) {
|
||||
resource.author = author;
|
||||
if (author?.name) {
|
||||
resource.author = author.name;
|
||||
}
|
||||
|
||||
if (description) {
|
||||
resource.description = description;
|
||||
}
|
||||
|
||||
if (date) {
|
||||
const d = typeof date === "string" ? new Date(date) : date;
|
||||
if (datePublished) {
|
||||
const d = typeof datePublished === "string"
|
||||
? new Date(datePublished)
|
||||
: datePublished;
|
||||
resource.year = d.getFullYear();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user