feat: some shit
This commit is contained in:
68
routes/api/series/[name].ts
Normal file
68
routes/api/series/[name].ts
Normal file
@ -0,0 +1,68 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { json } from "@lib/helpers.ts";
|
||||
import * as tmdb from "@lib/tmdb.ts";
|
||||
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
|
||||
import { safeFileName } from "@lib/string.ts";
|
||||
import { createDocument } from "@lib/documents.ts";
|
||||
import { AccessDeniedError } from "@lib/errors.ts";
|
||||
import { createSeries, getSeries, Series } from "@lib/resource/series.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(_, ctx) {
|
||||
const series = await getSeries(ctx.params.name);
|
||||
return json(series);
|
||||
},
|
||||
async POST(_, ctx) {
|
||||
const session = ctx.state.session;
|
||||
if (!session) {
|
||||
throw new AccessDeniedError();
|
||||
}
|
||||
|
||||
const tmdbId = parseInt(ctx.params.name);
|
||||
|
||||
const seriesDetails = await tmdb.getSeries(tmdbId);
|
||||
const seriesCredits = await tmdb.getSeriesCredits(tmdbId);
|
||||
|
||||
const releaseDate = seriesDetails.first_air_date;
|
||||
const posterPath = seriesDetails.poster_path;
|
||||
const director =
|
||||
seriesCredits?.crew?.filter?.((person) => person.job === "Director")[0];
|
||||
|
||||
let finalPath = "";
|
||||
const name = seriesDetails.name || seriesDetails.original_name ||
|
||||
ctx.params.name;
|
||||
if (posterPath) {
|
||||
const poster = await tmdb.getMoviePoster(posterPath);
|
||||
const extension = fileExtension(posterPath);
|
||||
|
||||
finalPath = `Media/series/images/${
|
||||
safeFileName(name)
|
||||
}_cover.${extension}`;
|
||||
await createDocument(finalPath, poster);
|
||||
}
|
||||
|
||||
const metadata = {} as Series["meta"];
|
||||
if (releaseDate) {
|
||||
metadata.date = new Date(releaseDate);
|
||||
}
|
||||
if (finalPath) {
|
||||
metadata.image = finalPath;
|
||||
}
|
||||
if (director) {
|
||||
metadata.author = director.name;
|
||||
}
|
||||
|
||||
const series: Series = {
|
||||
id: name,
|
||||
name: name,
|
||||
type: "series",
|
||||
description: "",
|
||||
tags: [],
|
||||
meta: metadata,
|
||||
};
|
||||
|
||||
await createSeries(series);
|
||||
|
||||
return json(series);
|
||||
},
|
||||
};
|
116
routes/api/series/enhance/[name].ts
Normal file
116
routes/api/series/enhance/[name].ts
Normal file
@ -0,0 +1,116 @@
|
||||
import { HandlerContext, Handlers } from "$fresh/server.ts";
|
||||
import {
|
||||
createDocument,
|
||||
getDocument,
|
||||
transformDocument,
|
||||
} from "@lib/documents.ts";
|
||||
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
|
||||
import * as tmdb from "@lib/tmdb.ts";
|
||||
import { parse, stringify } from "https://deno.land/std@0.194.0/yaml/mod.ts";
|
||||
import { formatDate, safeFileName } from "@lib/string.ts";
|
||||
import { json } from "@lib/helpers.ts";
|
||||
import {
|
||||
AccessDeniedError,
|
||||
BadRequestError,
|
||||
NotFoundError,
|
||||
} from "@lib/errors.ts";
|
||||
import { getSeries, Series } from "@lib/resource/series.ts";
|
||||
|
||||
async function updateSeriesMetadata(
|
||||
name: string,
|
||||
metadata: Partial<Series["meta"]>,
|
||||
) {
|
||||
const docId = `Media/series/${name}.md`;
|
||||
|
||||
console.log({ docId, metadata });
|
||||
|
||||
let currentDoc = await getDocument(docId);
|
||||
if (!currentDoc) {
|
||||
throw new NotFoundError();
|
||||
}
|
||||
|
||||
if (!currentDoc.startsWith("---\n---\n")) {
|
||||
currentDoc = `---\n---\n\n${currentDoc}`;
|
||||
}
|
||||
|
||||
const newDoc = transformDocument(currentDoc, (root) => {
|
||||
const frontmatterNode = root.children.find((c) => c.type === "yaml");
|
||||
|
||||
const frontmatter = frontmatterNode?.value as string;
|
||||
|
||||
const value = parse(frontmatter) as Series["meta"];
|
||||
|
||||
const newValue = {
|
||||
...metadata,
|
||||
date: formatDate(metadata.date),
|
||||
...value,
|
||||
};
|
||||
|
||||
frontmatterNode.value = stringify(newValue);
|
||||
|
||||
return root;
|
||||
});
|
||||
|
||||
console.log(newDoc);
|
||||
|
||||
return createDocument(docId, newDoc);
|
||||
}
|
||||
|
||||
const POST = async (
|
||||
req: Request,
|
||||
ctx: HandlerContext,
|
||||
): Promise<Response> => {
|
||||
const session = ctx.state.session;
|
||||
if (!session) {
|
||||
throw new AccessDeniedError();
|
||||
}
|
||||
|
||||
const body = await req.json();
|
||||
const name = ctx.params.name;
|
||||
const { tmdbId } = body;
|
||||
if (!name || !tmdbId) {
|
||||
throw new BadRequestError();
|
||||
}
|
||||
|
||||
const series = await getSeries(ctx.params.name);
|
||||
if (!series) {
|
||||
throw new NotFoundError();
|
||||
}
|
||||
|
||||
const seriesDetails = await tmdb.getSeries(tmdbId);
|
||||
const seriesCredits = !series.meta.author &&
|
||||
await tmdb.getSeriesCredits(tmdbId);
|
||||
|
||||
const releaseDate = seriesDetails.first_air_date;
|
||||
const posterPath = seriesDetails.poster_path;
|
||||
const director = seriesCredits &&
|
||||
seriesCredits.crew?.filter?.((person) => person.job === "Director")[0];
|
||||
|
||||
let finalPath = "";
|
||||
if (posterPath && !series.meta.image) {
|
||||
const poster = await tmdb.getMoviePoster(posterPath);
|
||||
const extension = fileExtension(posterPath);
|
||||
|
||||
finalPath = `Media/series/images/${safeFileName(name)}_cover.${extension}`;
|
||||
await createDocument(finalPath, poster);
|
||||
}
|
||||
|
||||
const metadata = {} as Series["meta"];
|
||||
if (releaseDate) {
|
||||
metadata.date = new Date(releaseDate);
|
||||
}
|
||||
if (finalPath) {
|
||||
metadata.image = finalPath;
|
||||
}
|
||||
if (director && director.name) {
|
||||
metadata.author = director.name;
|
||||
}
|
||||
|
||||
await updateSeriesMetadata(name, metadata);
|
||||
|
||||
return json(seriesCredits);
|
||||
};
|
||||
|
||||
export const handler: Handlers = {
|
||||
POST,
|
||||
};
|
Reference in New Issue
Block a user