shoulda finished id, move route

This commit is contained in:
creations 2024-12-28 12:57:12 -05:00
parent 645e0a0d2b
commit 1cba63df34
Signed by: creations
GPG key ID: 8F553AA4320FC711
4 changed files with 118 additions and 8 deletions

View file

@ -17,7 +17,7 @@ export const booruConfig: IBooruConfigMap = {
functions: booruDefaults,
},
"realbooru.com": {
enabled: true,
enabled: false,
name: "realbooru.com",
aliases: ["realbooru", "rb", "real34"],
endpoint: "realbooru.com",

View file

@ -1,13 +1,13 @@
import { booruConfig } from "@config/booru";
function timestampToReadable(timestamp?: number): string {
export function timestampToReadable(timestamp?: number): string {
const date: Date =
timestamp && !isNaN(timestamp) ? new Date(timestamp) : new Date();
if (isNaN(date.getTime())) return "Invalid Date";
return date.toISOString().replace("T", " ").replace("Z", "");
}
function tagsToExpectedFormat(
export function tagsToExpectedFormat(
tags: string[] | string | Record<string, string[]>,
minus: boolean = false,
onlyMinus: boolean = false,
@ -43,7 +43,15 @@ function tagsToExpectedFormat(
.join(delimiter);
}
function determineBooru(
export function shufflePosts<T>(posts: T[]): T[] {
for (let i: number = posts.length - 1; i > 0; i--) {
const j: number = Math.floor(Math.random() * (i + 1));
[posts[i], posts[j]] = [posts[j], posts[i]];
}
return posts;
}
export function determineBooru(
booruName: string,
): IBooruConfigMap[keyof IBooruConfigMap] | null {
const booru: IBooruConfigMap[keyof IBooruConfigMap] | undefined =
@ -56,4 +64,44 @@ function determineBooru(
return booru || null;
}
export { determineBooru, tagsToExpectedFormat, timestampToReadable };
export function postExpectedFormat(
booru: IBooruConfig,
posts: BooruPost[],
): { posts: BooruPost[] } | null {
if (!posts) return null;
posts = Array.isArray(posts) ? posts : [posts];
if (posts.length === 0) return null;
if (booru.name === "e621.net")
return {
posts: posts.map(
(post: BooruPost): BooruPost => ({
...post,
file_url: post.file_url,
post_url: `https://${booru.endpoint}/posts/${post.id}`,
tags: Object.keys(post.tags)
.flatMap(
(key: string) =>
post.tags[key as keyof typeof post.tags],
)
.join(" "),
}),
),
};
const fixedDomain: string = booru.endpoint.replace(/^api\./, "");
const formattedPosts: BooruPost[] = posts.map(
(post: BooruPost): BooruPost => {
const postUrl: string = `https://${fixedDomain}/index.php?page=post&s=view&id=${post.id}`;
const imageExtension: string = post.image.substring(
post.image.lastIndexOf(".") + 1,
);
const fileUrl: string = `https://${booru.endpoint}/images/${post.directory}/${post.hash}.${imageExtension}`;
return { ...post, file_url: fileUrl, post_url: postUrl };
},
);
return { posts: formattedPosts };
}

View file

@ -1,4 +1,4 @@
import { determineBooru } from "@helpers/char";
import { determineBooru, postExpectedFormat } from "@helpers/char";
import { fetch } from "bun";
import { redis } from "@/database/redis";
@ -116,14 +116,41 @@ async function handler(
);
}
// let keyString = Array.isArray(data) ? "posts" : "post";
const parsedData: Data = data as Data;
let posts: BooruPost[] = [];
if (parsedData.post) {
posts = [parsedData.post];
} else if (parsedData.posts) {
posts = parsedData.posts;
} else {
posts = Array.isArray(data) ? (data as BooruPost[]) : [];
}
if (posts.length === 0) {
return Response.json(
{
success: false,
code: 404,
error: "Post not found",
},
{
status: 404,
},
);
}
const expectedData: { posts: BooruPost[] } | null = postExpectedFormat(
booruConfig,
posts,
);
return Response.json(
{
success: true,
code: 200,
cache: false,
data,
post: expectedData?.posts[0] || null,
},
{
status: 200,

35
types/booruResponses.d.ts vendored Normal file
View file

@ -0,0 +1,35 @@
type Data = {
post?: Post;
posts?: Post[];
[key: string]: unknown;
};
interface Rule34Post {
preview_url: string;
sample_url: string;
file_url: string;
directory: number;
hash: string;
width: number;
height: number;
id: number;
image: string;
change: number;
owner: string;
parent_id: number;
rating: string;
sample: boolean;
sample_height: number;
sample_width: number;
score: number;
tags: string;
source: string;
status: string;
has_notes: boolean;
comment_count: number;
}
type BooruPost = Rule34Post & {
post_url: string;
file_url: string;
};