feat: enhance API functionality with new request handling and export methods

This commit is contained in:
fzzin
2025-03-30 04:18:28 +02:00
parent f55b5579ad
commit fe4e6690d4
4 changed files with 125 additions and 88 deletions

View File

@@ -1,6 +1,7 @@
//TODO Add optional extensions like limit at Posts with tags etc.
import * as xml_parser from "jsr:@melvdouc/xml-parser";
type ImageResponse = {
export type ImageResponse = {
preview_url: string;
sample_url: string;
file_url: string;
@@ -44,11 +45,11 @@ type TagResponse = {
// Define the API URL
const baseUrl = "https://api.rule34.xxx";
const postUrl = `${baseUrl}/index.php?page=dapi&s=post&q=index&json=1`;
export const postUrl = new URL(`${baseUrl}/index.php?page=dapi&s=post&q=index&json=1`);
const tagUrl = `${baseUrl}/index.php?page=dapi&s=tag&q=index`;
const commentsUrl = `${baseUrl}/index.php?page=dapi&s=comment&q=index`;
async function requestJSON<T>(URL: string) {
export async function requestJSON<T>(URL: string) {
const response = await requestRaw(URL);
return <T> await response.json();
}
@@ -62,41 +63,20 @@ async function requestRaw(URL: string) {
}
return response;
}
//List
async function getPosts(n: number) {
return await requestJSON<ImageResponse[]>(`${postUrl}&limit=${n}`);
}
async function getPostWithID(id: number) {
return await requestJSON<ImageResponse[]>(
`${postUrl}&id=${encodeURIComponent(id)}`,
);
}
async function getPID() {
return await requestJSON<ImageResponse[]>(`${postUrl}&pid`);
}
async function getTags(tags: string[]) {
const list = tags.join("+");
return await requestJSON<ImageResponse[]>(
`${postUrl}&tags=${encodeURIComponent(list)}`,
);
}
// Comments
async function getPostComments(postID: number) {
export async function getPostComments(postID: number): Promise<CommentResponse[]> {
const response = await requestRaw(`${commentsUrl}&post_id=${postID}`);
return XMLtoGenericDatatypeParser(response, parseComment);
}
async function getTagByID(id: number) {
export async function getTagByID(id: number): Promise<TagResponse[]> {
const response = await requestRaw(`${tagUrl}&id=${id}`);
return XMLtoGenericDatatypeParser(response, parseTag);
}
async function getTagList(n: number) {
export async function getTagList(n: number): Promise<TagResponse[]> {
const response = await requestRaw(`${tagUrl}&limit=${n}`);
return XMLtoGenericDatatypeParser(response, parseTag);
}
@@ -150,19 +130,4 @@ async function XMLtoGenericDatatypeParser<T>(
}
}
return stack;
}
Deno.test("Post Comment", async () => {
const response = await getPostComments(1213);
console.debug(response);
});
Deno.test("Get Tag by ID", async () => {
const response = await getTagByID(1);
console.debug(response);
});
Deno.test("Get Tag List", async () => {
const response = await getTagList(6);
console.debug(response);
});
}