Refactor API integration by moving rule34 and yandere APIs to plugins and updating message handling
This commit is contained in:
84
src/plugins/rule34/api.ts
Executable file
84
src/plugins/rule34/api.ts
Executable 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;
|
||||
}
|
||||
Reference in New Issue
Block a user