feat: enhance API functionality with new request handling and export methods
This commit is contained in:
74
src/plugins/rule34/plugin.ts
Normal file
74
src/plugins/rule34/plugin.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { assert } from "@std/assert/assert";
|
||||
import {requestJSON, postUrl, ImageResponse} from "./api.ts"
|
||||
|
||||
|
||||
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 async function drop() {
|
||||
const response = await requestJSON<ImageResponse[]>(`${postUrl}&limit=1`)
|
||||
if (response[0] === undefined) throw Error("Bro, get some sleep")
|
||||
return response[0].file_url
|
||||
}
|
||||
|
||||
export async function requestWorker(requestString: string){
|
||||
const response = await requestJSON<ImageResponse[]>(generateRequestURL(requestString))
|
||||
const stack: string[] = []
|
||||
for (const img of response) {
|
||||
stack.push(img.file_url)
|
||||
}
|
||||
return stack.join("\n")
|
||||
}
|
||||
|
||||
//TODO: get help in form of a nice beautiful format
|
||||
export function help() {
|
||||
return "WIP"
|
||||
}
|
||||
|
||||
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+,]+)/)
|
||||
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(requestString: string) {
|
||||
const postCpy = new URL(postUrl.toString())
|
||||
postCpy.search = new URLSearchParams([...postCpy.searchParams, ...requestParser(requestString)]).toString()
|
||||
return postCpy.toString()
|
||||
}
|
||||
|
||||
Deno.test("Test Request Parser", () => {
|
||||
assert(requestParser("[limit: 12,tags:bro+likes+bread]"), '{ "limit" => "12", "tags" => "bro+likes+bread" }')
|
||||
})
|
||||
|
||||
Deno.test("Test URL Search Parameters", () => {
|
||||
console.debug(generateRequestURL("[limit:3]"))
|
||||
})
|
||||
|
||||
Deno.test("Test Drop", async() => {
|
||||
console.debug(await drop())
|
||||
})
|
||||
|
||||
Deno.test("Test Request Workder", async() => {
|
||||
console.debug(await requestWorker("[limit: 12,tags:sfw]"))
|
||||
})
|
||||
Reference in New Issue
Block a user