feat: refactor whole bunch of stuff
This commit is contained in:
@@ -1,25 +1,25 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { createStreamResponse } from "@lib/helpers.ts";
|
||||
import { Movie } from "@lib/resource/movies.ts";
|
||||
import * as tmdb from "@lib/tmdb.ts";
|
||||
import {
|
||||
createRecommendationResource,
|
||||
getRecommendation,
|
||||
} from "@lib/recommendation.ts";
|
||||
import { AccessDeniedError } from "@lib/errors.ts";
|
||||
import { fetchResource } from "@lib/marka.ts";
|
||||
import { listResources } from "@lib/marka/index.ts";
|
||||
import { ReviewResource } from "@lib/marka/schema.ts";
|
||||
|
||||
async function processUpdateRecommendations(
|
||||
streamResponse: ReturnType<typeof createStreamResponse>,
|
||||
) {
|
||||
const allMovies = await fetchResource("movies");
|
||||
const allMovies = await listResources<ReviewResource>("movies");
|
||||
|
||||
const movies = allMovies?.content.filter((m) => {
|
||||
const movies = allMovies?.filter((m: ReviewResource) => {
|
||||
if (!m?.content) return false;
|
||||
if (!m.content.reviewRating) return false;
|
||||
if (!m.content.tmdbId) return false;
|
||||
return true;
|
||||
}) as Movie[];
|
||||
}) as ReviewResource[];
|
||||
|
||||
streamResponse.enqueue("Fetched all movies");
|
||||
|
||||
@@ -27,22 +27,23 @@ async function processUpdateRecommendations(
|
||||
const total = movies.length;
|
||||
|
||||
await Promise.all(movies.map(async (movie) => {
|
||||
if (!movie.meta.tmdbId) return;
|
||||
if (!movie.meta.rating) return;
|
||||
const recommendation = getRecommendation(movie.id, movie.type);
|
||||
if (!movie.content.tmdbId) return;
|
||||
if (!movie.content.reviewRating) return;
|
||||
const recommendation = getRecommendation(movie.name, movie.type);
|
||||
if (recommendation) {
|
||||
done++;
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const movieDetails = await tmdb.getMovie(movie.meta.tmdbId);
|
||||
const movieDetails = await tmdb.getMovie(movie.content.tmdbId);
|
||||
await createRecommendationResource(movie, movieDetails.overview);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
done++;
|
||||
streamResponse.enqueue(
|
||||
`${Math.floor((done / total) * 100)}% [${done + 1}/${total}] ${movie.id}`,
|
||||
`${Math.floor((done / total) * 100)}% [${done + 1
|
||||
}/${total}] ${movie.name}`,
|
||||
);
|
||||
})).catch((err) => {
|
||||
console.log(err);
|
||||
|
||||
Reference in New Issue
Block a user