Export requestJSON and requestRAW to core, formatting and adapting code and removing yandere code for update.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
//TODO Add optional extensions like limit at Posts with tags etc.
|
||||
import * as xml_parser from "jsr:@melvdouc/xml-parser";
|
||||
import { requestRaw, requestJSON } from "@root/structures/apiRequest.ts";
|
||||
|
||||
export type ImageResponse = {
|
||||
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 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
|
||||
export async function getPostComments(postID: number): Promise<CommentResponse[]> {
|
||||
const response = await requestRaw(`${commentsUrl}&post_id=${postID}`);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
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 { ImageResponse, postUrl } from "./api.ts";
|
||||
|
||||
const keys = ["limit", "id", "pid", "tags"] as const;
|
||||
type PostKeys = typeof keys[number];
|
||||
|
||||
@@ -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!",
|
||||
);
|
||||
});
|
||||
5
src/plugins/yandere/api/api.ts
Normal file
5
src/plugins/yandere/api/api.ts
Normal 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`;
|
||||
46
src/plugins/yandere/api/post.ts
Normal file
46
src/plugins/yandere/api/post.ts
Normal 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;
|
||||
}[];
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
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 { logMessage } from "@root/logging.ts";
|
||||
|
||||
|
||||
0
src/plugins/yandere/plugin.ts
Normal file
0
src/plugins/yandere/plugin.ts
Normal file
@@ -4,7 +4,7 @@ import {
|
||||
getPage,
|
||||
refresh,
|
||||
setPage,
|
||||
} from "@root/plugins/yandere/api.ts";
|
||||
} from "./api/api.ts";
|
||||
import { assertEquals } from "@std/assert/equals";
|
||||
//tests
|
||||
/*
|
||||
|
||||
14
src/structures/apiRequest.ts
Normal file
14
src/structures/apiRequest.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user