rule34 implementation and code upgrades
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import { assert } from "@std/assert/assert";
|
||||
import { requestJSON } from "@root/structures/apiRequest.ts";
|
||||
import { ImageResponse, postUrl } from "./api.ts";
|
||||
import { EmbedBuilder } from "@discordjs";
|
||||
|
||||
const keys = ["limit", "id", "pid", "tags"] as const;
|
||||
type PostKeys = typeof keys[number];
|
||||
|
||||
function isKey(key: string): key is PostKeys {
|
||||
return keys.includes(key as any);
|
||||
export type PostRequestParameters = {
|
||||
limit?: string| null| undefined,
|
||||
id?: string| null| undefined,
|
||||
pid?: string| null| undefined,
|
||||
tags?: string| null| undefined,
|
||||
page?: string| null| undefined,
|
||||
}
|
||||
|
||||
// export async function drop() {
|
||||
@@ -36,67 +37,40 @@ function isKey(key: string): key is PostKeys {
|
||||
// }
|
||||
// }
|
||||
|
||||
// export async function requestWorker(requestString: string) {
|
||||
// try {
|
||||
// const response = await requestJSON<ImageResponse[]>(
|
||||
// generateRequestURL(requestString),
|
||||
// );
|
||||
// const stack: Embed[] = [];
|
||||
// for (const img of response) {
|
||||
// stack.push(
|
||||
// new Embed(
|
||||
// `ID: ${img.id}`,
|
||||
// img.sample_url,
|
||||
// new EmbedAuthor(img.owner),
|
||||
// // [new EmbedField("Tags", img.tags, true)],
|
||||
// new EmbedImage(img.file_url, img.height, img.width),
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
// return stack;
|
||||
// } catch (e) {
|
||||
// console.error((e as Error).message);
|
||||
// return [];
|
||||
// }
|
||||
// }
|
||||
|
||||
//TODO: get help in form of a nice beautiful format
|
||||
export function help() {
|
||||
return "WIP";
|
||||
export async function requestWorker(postRequestParams: PostRequestParameters) {
|
||||
const embeds = [];
|
||||
try {
|
||||
const response = await requestJSON<ImageResponse[]>(
|
||||
generateRequestURL(postRequestParams),
|
||||
);
|
||||
for (const post of response) {
|
||||
embeds.push(
|
||||
new EmbedBuilder()
|
||||
.setTitle(post.id.toString())
|
||||
.setURL(post.file_url)
|
||||
.setAuthor(
|
||||
{
|
||||
name: post.owner,
|
||||
},
|
||||
)
|
||||
.setImage(post.preview_url),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error((e as Error).message);
|
||||
return [];
|
||||
}
|
||||
return embeds;
|
||||
}
|
||||
|
||||
export function requestParser(requestString: string) {
|
||||
const res = requestString.match(/\[([\s*w+:\s*[\d+|\w+,]+)\]/g);
|
||||
const map = new Map<PostKeys, string>();
|
||||
if (res !== null) {
|
||||
res[0]
|
||||
.split(",")
|
||||
.forEach((param) => {
|
||||
const match = param.match(/\s*(\w+)\s*:\s*([\w+,+]+)/);
|
||||
console.debug({ match });
|
||||
if (match !== null) {
|
||||
if (match[1] === undefined || match[2] === undefined) {
|
||||
throw Error("Unreachable");
|
||||
}
|
||||
if (!isKey(match[1])) throw Error(`Key: ${match[1]} is not a Key!`);
|
||||
map.set(match[1], match[2]);
|
||||
} else {
|
||||
throw Error(`match returned null in param = ${Deno.inspect(param)}`);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
throw Error("Request String had some major issues chief");
|
||||
}
|
||||
return map;
|
||||
}
|
||||
export function generateRequestURL(postRequestParams: PostRequestParameters) {
|
||||
const postCpy = new URL(postUrl);
|
||||
const parameterStrings: string[] = []
|
||||
|
||||
// + is replaced with a space... this is pain
|
||||
// see percent code + === 2B
|
||||
export function generateRequestURL(requestString: string) {
|
||||
const postCpy = new URL(postUrl.toString());
|
||||
|
||||
for (const [k, v] of requestParser(requestString)) {
|
||||
postCpy.searchParams.append(k, v);
|
||||
}
|
||||
return postCpy.href.toString().replaceAll("%2B", "+");
|
||||
Object.entries(postRequestParams).forEach(([k, v]) => {
|
||||
if (v !== null) {
|
||||
parameterStrings.push(`&${k}=${v}`)
|
||||
}
|
||||
});
|
||||
return postCpy.href + parameterStrings.join('');
|
||||
}
|
||||
Reference in New Issue
Block a user