Files
Hotodog-Discord-Bot/src/plugins/yandere/api.ts

113 lines
2.8 KiB
TypeScript
Executable File

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 function setPage(newpage: number) {
page = newpage;
hyperlinkarray.length = 0;
refresh();
}
export function getPage() {
return page;
}
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;
}