feat: implement adding movie details from tmdb
This commit is contained in:
@ -1,8 +1,14 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { createDocument, getDocument } from "@lib/documents.ts";
|
||||
import { fileExtension } from "https://deno.land/x/file_extension/mod.ts";
|
||||
import { parseMovie } from "@lib/movies.ts";
|
||||
import {
|
||||
createDocument,
|
||||
getDocument,
|
||||
transformDocument,
|
||||
} from "@lib/documents.ts";
|
||||
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
|
||||
import { type Movie, parseMovie } from "@lib/movies.ts";
|
||||
import * as tmdb from "@lib/tmdb.ts";
|
||||
import { parse, stringify } from "https://deno.land/std@0.194.0/yaml/mod.ts";
|
||||
import { formatDate } from "@lib/string.ts";
|
||||
|
||||
function safeFileName(inputString: string): string {
|
||||
// Convert the string to lowercase
|
||||
@ -25,6 +31,45 @@ export async function getMovie(name: string) {
|
||||
return movie;
|
||||
}
|
||||
|
||||
async function updateMovieMetadata(
|
||||
name: string,
|
||||
metadata: Partial<Movie["meta"]>,
|
||||
) {
|
||||
const docId = `Media/movies/${name}.md`;
|
||||
|
||||
const currentDoc = await getDocument(docId);
|
||||
if (!currentDoc) return;
|
||||
|
||||
const newDoc = transformDocument(currentDoc, (root) => {
|
||||
const frontmatterNode = root.children.find((c) => c.type === "yaml");
|
||||
|
||||
const frontmatter = frontmatterNode?.value as string;
|
||||
|
||||
if (frontmatter) {
|
||||
const value = parse(frontmatter) as Movie["meta"];
|
||||
|
||||
if (metadata.author && !value.author) {
|
||||
value.author = metadata.author;
|
||||
}
|
||||
|
||||
if (metadata.image && !value.image) {
|
||||
value.image = metadata.image;
|
||||
}
|
||||
|
||||
if (metadata.date && !value.date) {
|
||||
value.date = formatDate(metadata.date);
|
||||
}
|
||||
frontmatterNode.value = stringify(value);
|
||||
}
|
||||
|
||||
return root;
|
||||
});
|
||||
|
||||
const response = await createDocument(docId, newDoc);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
export const handler = async (
|
||||
_req: Request,
|
||||
_ctx: HandlerContext,
|
||||
@ -32,8 +77,9 @@ export const handler = async (
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
|
||||
const movie = await getMovie(_ctx.params.name);
|
||||
|
||||
if (_req.method === "GET") {
|
||||
const movie = await getMovie(_ctx.params.name);
|
||||
return new Response(JSON.stringify(movie));
|
||||
}
|
||||
|
||||
@ -46,25 +92,41 @@ export const handler = async (
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const movieDetails = await tmdb.getMovie(tmdbId);
|
||||
const movieCredits = await tmdb.getMovieCredits(tmdbId);
|
||||
const movieCredits = !movie.meta.author &&
|
||||
await tmdb.getMovieCredits(tmdbId);
|
||||
|
||||
const releaseDate = movieDetails.release_date;
|
||||
const posterPath = movieDetails.poster_path;
|
||||
const director = movieCredits?.crew?.filter?.((person) =>
|
||||
person.job === "Director"
|
||||
);
|
||||
)[0];
|
||||
|
||||
if (posterPath) {
|
||||
let finalPath = "";
|
||||
if (posterPath && !movie.meta.image) {
|
||||
const poster = await tmdb.getMoviePoster(posterPath);
|
||||
const extension = fileExtension(posterPath);
|
||||
const finalPath = `Media/movies/images/${
|
||||
|
||||
finalPath = `Media/movies/images/${
|
||||
safeFileName(name)
|
||||
}_cover.${extension}`;
|
||||
await createDocument(finalPath, poster);
|
||||
}
|
||||
|
||||
console.log({ releaseDate, director, posterPath });
|
||||
const metadata = {} as Movie["meta"];
|
||||
if (releaseDate) {
|
||||
metadata.date = new Date(releaseDate);
|
||||
}
|
||||
if (finalPath) {
|
||||
metadata.image = finalPath;
|
||||
}
|
||||
if (director) {
|
||||
metadata.author = director.name;
|
||||
}
|
||||
|
||||
await updateMovieMetadata(name, metadata);
|
||||
|
||||
return new Response(JSON.stringify(movieCredits), {
|
||||
headers,
|
||||
});
|
||||
|
@ -24,6 +24,8 @@ export const handler = async (
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
|
||||
console.log("[api] getting movie credits");
|
||||
|
||||
const cacheId = `/movie/credits/${id}`;
|
||||
|
||||
const cachedResponse = await cache.get<CachedMovieCredits>(cacheId);
|
||||
|
Reference in New Issue
Block a user