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