Export requestJSON and requestRAW to core, formatting and adapting code and removing yandere code for update.

This commit is contained in:
fzzin
2025-04-08 16:27:58 +02:00
parent 8f903af37d
commit 1e8f195714
9 changed files with 70 additions and 147 deletions

View File

@@ -1,5 +1,6 @@
//TODO Add optional extensions like limit at Posts with tags etc. //TODO Add optional extensions like limit at Posts with tags etc.
import * as xml_parser from "jsr:@melvdouc/xml-parser"; import * as xml_parser from "jsr:@melvdouc/xml-parser";
import { requestRaw, requestJSON } from "@root/structures/apiRequest.ts";
export type ImageResponse = { export type ImageResponse = {
preview_url: string; preview_url: string;
@@ -49,22 +50,6 @@ export const postUrl = new URL(`${baseUrl}/index.php?page=dapi&s=post&q=index&js
const tagUrl = `${baseUrl}/index.php?page=dapi&s=tag&q=index`; const tagUrl = `${baseUrl}/index.php?page=dapi&s=tag&q=index`;
const commentsUrl = `${baseUrl}/index.php?page=dapi&s=comment&q=index`; const commentsUrl = `${baseUrl}/index.php?page=dapi&s=comment&q=index`;
export async function requestJSON<T>(url: string) {
const response = await requestRaw(url);
return <T> await response.json();
}
async function requestRaw(url: string) {
const response = await fetch(url, {
headers: { "Accept": "application/json" },
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response;
}
//List
// Comments // Comments
export async function getPostComments(postID: number): Promise<CommentResponse[]> { export async function getPostComments(postID: number): Promise<CommentResponse[]> {
const response = await requestRaw(`${commentsUrl}&post_id=${postID}`); const response = await requestRaw(`${commentsUrl}&post_id=${postID}`);

View File

@@ -1,6 +1,7 @@
import { assert } from "@std/assert/assert"; import { assert } from "@std/assert/assert";
import { ImageResponse, postUrl, requestJSON } from "./api.ts"; import { requestJSON } from "@root/structures/apiRequest.ts";
import { Embed, EmbedAuthor, EmbedImage } from "@root/structures/embeds.ts"; import { Embed, EmbedAuthor, EmbedImage } from "@root/structures/embeds.ts";
import { ImageResponse, postUrl } from "./api.ts";
const keys = ["limit", "id", "pid", "tags"] as const; const keys = ["limit", "id", "pid", "tags"] as const;
type PostKeys = typeof keys[number]; type PostKeys = typeof keys[number];

View File

@@ -1,128 +0,0 @@
import { assert } from "@std/assert/assert";
const apiUrl = "https://yande.re/post.json?api_version=2";
type APIResponse = {
posts: {
id: number;
tags: string;
created_at: string;
creator_id: number;
approver_id: number;
author: string;
change: number;
source: string;
score: number;
md5: string;
file_size: number;
file_ext: string;
file_url: string;
is_shown_in_index: boolean;
preview_url: string;
preview_width: number;
preview_height: number;
actual_preview_width: number;
actual_preview_height: number;
sample_url: string;
sample_width: number;
sample_height: number;
sample_file_size: number;
jpeg_url: string;
jpeg_width: number;
jpeg_height: number;
rating: string;
is_rating_locked: boolean;
has_children: boolean;
parent_id: number;
status: string;
is_pending: boolean;
width: number;
height: number;
is_held: boolean;
frames_pending_string: string;
frames_pending: [];
frames_string: string;
frames: [];
is_note_locked: boolean;
last_noted_at: string;
last_commented_at: string;
}[];
};
let page = 1;
let baseRequest = 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[] = [];
for (const k of baseRequest.posts) {
hyperlinkarray.push(k.file_url);
}
export async function setPage(newpage: number) {
page = newpage;
hyperlinkarray.length = 0;
await refresh();
}
export function getPage() {
return page;
}
//duplicate code, also found in rule34/api.ts
export async function refresh() {
await fetch(`${apiUrl}&page=${page}`, {
headers: { "Accept": "application/json" },
})
.then(async (response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
baseRequest = <APIResponse> await response.json();
for (const k of baseRequest.posts) {
if (!hyperlinkarray.includes(k.file_url)) {
hyperlinkarray.push(k.file_url);
}
}
});
}
export async function fetchNextPage() {
page += 1;
await refresh();
}
export async function dropYandere() {
if (hyperlinkarray.length === 0) {
await fetchNextPage();
return await dropYandere();
}
return hyperlinkarray.pop()!;
}
export async function dropYandere5() {
let tmp = "";
for (let i = 0; i < 5; i++) {
tmp += await dropYandere() + "\n";
}
return tmp;
}
//Test Stack resize when fetching new data
Deno.test("Refresh Stack", async () => {
await refresh();
const previousStack = [...hyperlinkarray];
await new Promise((r) => setTimeout(r, 10000));
await refresh();
assert(
previousStack.length <= hyperlinkarray.length,
"Stack-size did not increase as expected!",
);
});

View File

@@ -0,0 +1,5 @@
import { assert } from "@std/assert/assert";
import { requestJSON, requestRaw } from "@root/structures/apiRequest.ts";
const baseURL = "https://yande.re"
const postURL = `${baseURL}/post.json?api_version=2`;

View File

@@ -0,0 +1,46 @@
type PostResponse = {
posts: {
id: number;
tags: string;
created_at: string;
creator_id: number;
approver_id: number;
author: string;
change: number;
source: string;
score: number;
md5: string;
file_size: number;
file_ext: string;
file_url: string;
is_shown_in_index: boolean;
preview_url: string;
preview_width: number;
preview_height: number;
actual_preview_width: number;
actual_preview_height: number;
sample_url: string;
sample_width: number;
sample_height: number;
sample_file_size: number;
jpeg_url: string;
jpeg_width: number;
jpeg_height: number;
rating: string;
is_rating_locked: boolean;
has_children: boolean;
parent_id: number;
status: string;
is_pending: boolean;
width: number;
height: number;
is_held: boolean;
frames_pending_string: string;
frames_pending: [];
frames_string: string;
frames: [];
is_note_locked: boolean;
last_noted_at: string;
last_commented_at: string;
}[];
};

View File

@@ -1,5 +1,5 @@
import { Bot, Message } from "npm:discordeno@18.0.1"; import { Bot, Message } from "npm:discordeno@18.0.1";
import { dropYandere, dropYandere5, getPage, refresh, setPage } from "./api.ts"; import { dropYandere, dropYandere5, getPage, refresh, setPage } from "./api/api.ts";
import { defaultString } from "@root/defaultString.ts"; import { defaultString } from "@root/defaultString.ts";
import { logMessage } from "@root/logging.ts"; import { logMessage } from "@root/logging.ts";

View File

View File

@@ -4,7 +4,7 @@ import {
getPage, getPage,
refresh, refresh,
setPage, setPage,
} from "@root/plugins/yandere/api.ts"; } from "./api/api.ts";
import { assertEquals } from "@std/assert/equals"; import { assertEquals } from "@std/assert/equals";
//tests //tests
/* /*

View File

@@ -0,0 +1,14 @@
export async function requestJSON<T>(url: string) {
const response = await requestRaw(url);
return <T> await response.json();
}
export async function requestRaw(url: string) {
const response = await fetch(url, {
headers: { "Accept": "application/json" },
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response;
}