Refactor API integration by moving rule34 and yandere APIs to plugins and updating message handling

This commit is contained in:
fzzinchemical
2025-03-26 17:33:25 +01:00
parent b4cc5f7a08
commit 58c7932481
3 changed files with 4 additions and 4 deletions

84
src/plugins/rule34/api.ts Executable file
View File

@@ -0,0 +1,84 @@
type APIResponse = Array<{
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;
}>;
// Define the API URL
const apiUrl =
"https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&json=1";
// Make a GET request
let baseResponse = await fetch(apiUrl, { headers: { "Accept": "application/json" } })
.then(async (response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return <APIResponse> await response.json();
});
const hyperlinkarray: string[] = [];
// ---------------------------------
// import * as stdHTML from "jsr:@std/html@1.0.2";
// Deno.serve({ port: 6969 }, async (request) => {
// const html = hyperlinkarray.map((x) => `<img src="${stdHTML.escape(x)}"/>`)
// .join("\n");
// return new Response(html, {
// headers: { "Content-Type": "text/html;charset=utf-8" },
// });
// });
// ---------------------------------
export async function refresh() {
await fetch(apiUrl, { headers: { "Accept": "application/json" } })
.then(async (response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
baseResponse = <APIResponse> await response.json();
for (const k of baseResponse) {
if (!hyperlinkarray.includes(k.file_url)) {
hyperlinkarray.push(k.file_url);
}
}
});
}
for (const k of baseResponse) {
hyperlinkarray.push(k.file_url);
}
export async function r34test() {
if (hyperlinkarray.length === 0) {
await refresh();
return await r34test();
}
return hyperlinkarray.pop()!;
}
export async function drop5() {
let tmp = "";
for (let i = 0; i < 5; i++) {
tmp += await r34test() + "\n";
}
return tmp;
}