Yande.re API fully functional
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { handlePostRequest } from "@root/plugins/yandere/api/post.ts";
|
||||
import { handlePostRequest, postKeys } from "@root/plugins/yandere/api/post.ts";
|
||||
const baseURL = "https://yande.re";
|
||||
|
||||
const apikeys = [
|
||||
@@ -17,40 +17,20 @@ function prepareURLObjectForRequest(type: APIKeys) {
|
||||
const tmp = new URL(baseURL);
|
||||
tmp.searchParams.append("api_version", "3");
|
||||
switch (type) {
|
||||
case "post":
|
||||
tmp.pathname = "post.json";
|
||||
break;
|
||||
case "tags":
|
||||
tmp.pathname = "tag.json";
|
||||
break;
|
||||
case "artist":
|
||||
tmp.pathname = "artist.json";
|
||||
break;
|
||||
case "comments":
|
||||
tmp.pathname = "comment";
|
||||
break;
|
||||
case "wiki":
|
||||
tmp.pathname = "wiki.json";
|
||||
break;
|
||||
case "notes":
|
||||
tmp.pathname = "note.json";
|
||||
break;
|
||||
case "users":
|
||||
tmp.pathname = "user.json";
|
||||
break;
|
||||
case "forum":
|
||||
tmp.pathname = "forum.json";
|
||||
break;
|
||||
case "pools":
|
||||
tmp.pathname = "pool.json";
|
||||
break;
|
||||
default:
|
||||
throw Error("unknown ");
|
||||
case "post": tmp.pathname = "post.json"; break;
|
||||
case "tags": tmp.pathname = "tag.json"; break;
|
||||
case "artist": tmp.pathname = "artist.json"; break;
|
||||
case "comments": tmp.pathname = "comment"; break;
|
||||
case "wiki": tmp.pathname = "wiki.json"; break;
|
||||
case "notes": tmp.pathname = "note.json"; break;
|
||||
case "users": tmp.pathname = "user.json"; break;
|
||||
case "forum": tmp.pathname = "forum.json"; break;
|
||||
case "pools": tmp.pathname = "pool.json"; break;
|
||||
default: throw Error("unknown ");
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
export function getPosts(search? : string){
|
||||
const url = prepareURLObjectForRequest("post")
|
||||
return handlePostRequest(url, search)
|
||||
export async function getPosts(args? : postKeys){
|
||||
return await handlePostRequest(prepareURLObjectForRequest("post"), args)
|
||||
}
|
||||
@@ -46,29 +46,20 @@ type PostResponse = {
|
||||
last_commented_at: string;
|
||||
}[];
|
||||
|
||||
const postSearchkeys = ["limit", "page", "tags"];
|
||||
export type postKeys = {
|
||||
limit?: string | null | undefined;
|
||||
page?: string | null | undefined;
|
||||
tags?: string | null | undefined;
|
||||
};
|
||||
|
||||
function parsePostListArgs(url: URL, args: string) {
|
||||
// const urlCopy: URL = copyObject<URL>(url);
|
||||
const argarr = args.replaceAll(/(\[|\ |\])/g, "").split(",");
|
||||
for (const arg of argarr) {
|
||||
const [k, v] = arg.split(":");
|
||||
if (url === undefined) throw Error("undefined Object: url!")
|
||||
if (k === undefined || v === undefined) {
|
||||
throw Error(
|
||||
`undefined key or value in ${parsePostListArgs.name}, got k:${k}, v:${v}`,
|
||||
);
|
||||
}
|
||||
if (postSearchkeys.includes(k)) {
|
||||
console.debug(k, v)
|
||||
console.debug(JSON.stringify(url))
|
||||
url.searchParams.append(k, v);
|
||||
} else {
|
||||
throw Error(
|
||||
`unknown parameter was given in ${parsePostListArgs.name}, got k:${k}, v:${v}`,
|
||||
);
|
||||
}
|
||||
function parsePostListArgs(url: URL, args: postKeys | undefined) {
|
||||
if (args === undefined) args = { limit: "1" }; //TODO that could be better
|
||||
Object.entries(args).forEach(([key, value]) => {
|
||||
if (value !== null) {
|
||||
console.debug(`appending k:${key}, v:${value}`);
|
||||
url.searchParams.append(key, value);
|
||||
}
|
||||
});
|
||||
return url;
|
||||
}
|
||||
|
||||
@@ -87,29 +78,20 @@ async function returnDiscordEmbeds(
|
||||
.setAuthor(
|
||||
{
|
||||
name: post.author,
|
||||
}
|
||||
)
|
||||
.setImage(post.file_url)
|
||||
},
|
||||
)
|
||||
.setImage(post.preview_url),
|
||||
);
|
||||
}
|
||||
return embeds;
|
||||
}
|
||||
|
||||
// function copyObject<T>(obj: T) {
|
||||
// const newObject: T = Object.
|
||||
|
||||
|
||||
/**
|
||||
* Post-Request Handler Function, that returns DiscordEmbeds.
|
||||
* @param url URL Object
|
||||
* @param search Search string formatted like the following: [tags: something, page: 1, limit: 666], some parameters can miss.
|
||||
* @param args Search string formatted like the following: [tags: something, page: 1, limit: 666], some parameters can miss.
|
||||
*/
|
||||
export async function handlePostRequest(url: URL, search?: string) {
|
||||
// const urlCopy = copyObject<URL>(url);
|
||||
export async function handlePostRequest(url: URL, args?: postKeys) {
|
||||
//parse
|
||||
if (search) {
|
||||
return await returnDiscordEmbeds(parsePostListArgs(url, search));
|
||||
} else {
|
||||
return await returnDiscordEmbeds(url);
|
||||
}
|
||||
return await returnDiscordEmbeds(parsePostListArgs(url, args));
|
||||
}
|
||||
@@ -1,7 +1,4 @@
|
||||
import {
|
||||
ChatInputCommandInteraction,
|
||||
SlashCommandBuilder,
|
||||
} from "@discordjs";
|
||||
import { ChatInputCommandInteraction, SlashCommandBuilder } from "@discordjs";
|
||||
import { getPosts } from "@root/plugins/yandere/api/api.ts";
|
||||
|
||||
// export async function yandereMessageHandler(message: Message) {
|
||||
@@ -34,25 +31,43 @@ export const commands = [
|
||||
.addSubcommand((subcommand) =>
|
||||
subcommand
|
||||
.setName("drop")
|
||||
.setDescription("Drops one or multiple images using the yande.re API.")
|
||||
.addStringOption(option =>
|
||||
option
|
||||
.setDescription(
|
||||
"Drops one or multiple images using the yande.re API.",
|
||||
)
|
||||
.addStringOption((option) => option
|
||||
.setName("limit")
|
||||
.setDescription("Post limitation")
|
||||
)
|
||||
.addStringOption(option =>
|
||||
.addStringOption((option) =>
|
||||
option
|
||||
.setName("tags")
|
||||
.setDescription("Tags to add to query")
|
||||
)
|
||||
.addStringOption(option =>
|
||||
.addStringOption((option) =>
|
||||
option
|
||||
.setName("page")
|
||||
.setDescription("Which page to query")
|
||||
)
|
||||
),
|
||||
async execute(interaction: ChatInputCommandInteraction) {
|
||||
await interaction.reply({embeds: await getPosts("[limit: 5]")});
|
||||
const limit = interaction.options.getString("limit")
|
||||
const tags = interaction.options.getString("tags")
|
||||
const page = interaction.options.getString("page")
|
||||
if (limit === null && tags === null && page === null) {
|
||||
await interaction.reply({
|
||||
embeds: await getPosts({
|
||||
limit: "1"
|
||||
})
|
||||
})
|
||||
} else {
|
||||
await interaction.reply({
|
||||
embeds: await getPosts({
|
||||
limit: limit,
|
||||
tags: tags,
|
||||
page: page
|
||||
}),
|
||||
})
|
||||
}
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user