2023-09-08 15:15:36 +02:00
|
|
|
import { Handlers } from "$fresh/server.ts";
|
|
|
|
import { createStreamResponse } from "@lib/helpers.ts";
|
2025-01-06 16:14:29 +01:00
|
|
|
import { getAllMovies, Movie } from "@lib/resource/movies.ts";
|
2023-09-08 15:15:36 +02:00
|
|
|
import * as tmdb from "@lib/tmdb.ts";
|
|
|
|
import {
|
|
|
|
createRecommendationResource,
|
|
|
|
getRecommendation,
|
|
|
|
} from "@lib/recommendation.ts";
|
|
|
|
import { AccessDeniedError } from "@lib/errors.ts";
|
|
|
|
|
|
|
|
async function processUpdateRecommendations(
|
|
|
|
streamResponse: ReturnType<typeof createStreamResponse>,
|
|
|
|
) {
|
|
|
|
const allMovies = await getAllMovies();
|
|
|
|
|
|
|
|
const movies = allMovies.filter((m) => {
|
2025-01-06 16:14:29 +01:00
|
|
|
if (!m?.meta) return false;
|
2023-09-08 15:15:36 +02:00
|
|
|
if (!m.meta.rating) return false;
|
|
|
|
if (!m.meta.tmdbId) return false;
|
|
|
|
return true;
|
2025-01-06 16:14:29 +01:00
|
|
|
}) as Movie[];
|
2023-09-08 15:15:36 +02:00
|
|
|
|
|
|
|
streamResponse.enqueue("Fetched all movies");
|
|
|
|
|
|
|
|
let done = 0;
|
|
|
|
const total = movies.length;
|
|
|
|
|
|
|
|
await Promise.all(movies.map(async (movie) => {
|
|
|
|
if (!movie.meta.tmdbId) return;
|
|
|
|
if (!movie.meta.rating) return;
|
2025-01-06 16:14:29 +01:00
|
|
|
const recommendation = getRecommendation(movie.id, movie.type);
|
2023-09-08 15:15:36 +02:00
|
|
|
if (recommendation) {
|
|
|
|
done++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const movieDetails = await tmdb.getMovie(movie.meta.tmdbId);
|
|
|
|
await createRecommendationResource(movie, movieDetails.overview);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
done++;
|
|
|
|
streamResponse.enqueue(
|
|
|
|
`${Math.floor((done / total) * 100)}% [${done + 1}/${total}] ${movie.id}`,
|
|
|
|
);
|
|
|
|
})).catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
streamResponse.enqueue("100% Finished");
|
|
|
|
}
|
|
|
|
|
|
|
|
export const handler: Handlers = {
|
|
|
|
GET(_, ctx) {
|
|
|
|
const session = ctx.state.session;
|
|
|
|
if (!session) {
|
|
|
|
throw new AccessDeniedError();
|
|
|
|
}
|
|
|
|
|
|
|
|
const streamResponse = createStreamResponse();
|
|
|
|
processUpdateRecommendations(streamResponse);
|
|
|
|
|
|
|
|
return streamResponse.response;
|
|
|
|
},
|
|
|
|
};
|