feat: get image from tmdb
This commit is contained in:
@ -1,6 +1,21 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { getDocument } from "@lib/documents.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 * as tmdb from "@lib/tmdb.ts";
|
||||
|
||||
function safeFileName(inputString: string): string {
|
||||
// Convert the string to lowercase
|
||||
let fileName = inputString.toLowerCase();
|
||||
|
||||
// Replace spaces with underscores
|
||||
fileName = fileName.replace(/ /g, "_");
|
||||
|
||||
// Remove characters that are not safe for file names
|
||||
fileName = fileName.replace(/[^\w.-]/g, "");
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
export async function getMovie(name: string) {
|
||||
const document = await getDocument(`Media/movies/${name}.md`);
|
||||
@ -14,10 +29,46 @@ export const handler = async (
|
||||
_req: Request,
|
||||
_ctx: HandlerContext,
|
||||
): Promise<Response> => {
|
||||
const movie = await getMovie(_ctx.params.name);
|
||||
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
|
||||
return new Response(JSON.stringify(movie));
|
||||
if (_req.method === "GET") {
|
||||
const movie = await getMovie(_ctx.params.name);
|
||||
return new Response(JSON.stringify(movie));
|
||||
}
|
||||
|
||||
if (_req.method === "POST") {
|
||||
const body = await _req.json();
|
||||
const name = _ctx.params.name;
|
||||
const { tmdbId } = body;
|
||||
if (!name || !tmdbId) {
|
||||
return new Response("Bad Request", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
const movieDetails = await tmdb.getMovie(tmdbId);
|
||||
const movieCredits = await tmdb.getMovieCredits(tmdbId);
|
||||
|
||||
const releaseDate = movieDetails.release_date;
|
||||
const posterPath = movieDetails.poster_path;
|
||||
const director = movieCredits?.crew?.filter?.((person) =>
|
||||
person.job === "Director"
|
||||
);
|
||||
|
||||
if (posterPath) {
|
||||
const poster = await tmdb.getMoviePoster(posterPath);
|
||||
const extension = fileExtension(posterPath);
|
||||
const finalPath = `Media/movies/images/${
|
||||
safeFileName(name)
|
||||
}_cover.${extension}`;
|
||||
await createDocument(finalPath, poster);
|
||||
}
|
||||
|
||||
console.log({ releaseDate, director, posterPath });
|
||||
return new Response(JSON.stringify(movieCredits), {
|
||||
headers,
|
||||
});
|
||||
}
|
||||
|
||||
return new Response();
|
||||
};
|
||||
|
@ -15,9 +15,15 @@ export const handler: Handlers<Movie | null> = {
|
||||
export default function Greet(props: PageProps<Movie>) {
|
||||
const movie = props.data;
|
||||
|
||||
const { author = "", date = "" } = movie.meta;
|
||||
|
||||
return (
|
||||
<MainLayout url={props.url}>
|
||||
<RecipeHero data={movie} backlink="/movies" />
|
||||
<RecipeHero
|
||||
data={movie}
|
||||
subline={[author, date.toString()]}
|
||||
backlink="/movies"
|
||||
/>
|
||||
<KMenu type="main" context={movie} />
|
||||
<div class="px-8 text-white mt-10">
|
||||
<pre
|
||||
|
Reference in New Issue
Block a user