From 163d8119c109c42e64ab37b01dec131f2cb5bf61 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 19 Oct 2021 19:25:03 +0200 Subject: Code improvements, support a manual post queue --- src/api/e621/index.ts | 80 --------------------------------------------------- 1 file changed, 80 deletions(-) delete mode 100644 src/api/e621/index.ts (limited to 'src/api/e621/index.ts') diff --git a/src/api/e621/index.ts b/src/api/e621/index.ts deleted file mode 100644 index a8abbcf..0000000 --- a/src/api/e621/index.ts +++ /dev/null @@ -1,80 +0,0 @@ -import got from "got"; -import config from "../../config"; -import delay from "../../util/delay"; -import dedupe from "../../services/dedupe"; - -export interface GetPostQuery { - tags: readonly string[]; - maxPage: number; -} - -export interface Post { - id: number; - file: { - url: string; - }; - sources: readonly string[]; - - tags: { - general: readonly string[]; - species: readonly string[]; - character: readonly string[]; - copyright: readonly string[]; - artist: readonly string[]; - invalid: readonly string[]; - lore: readonly string[]; - meta: readonly string[]; - }; -} - -export const client = got.extend({ - headers: { - "User-Agent": config.e621.userAgent, - }, -}); - -export async function getPostById(id: number) { - const response = await client - .get("https://e926.net/posts.json", { - searchParams: { - tags: `id:${id}`, - }, - }) - .json<{ posts: readonly Post[] }>(); - - if (!response.posts.length) { - throw new Error("No posts received"); - } - - return response.posts[0]; -} - -export async function getRandomPost(query: GetPostQuery) { - const page = Math.floor(Math.random() * (query.maxPage - 1)) + 1; - - const response = await client - .get("https://e926.net/posts.json", { - searchParams: { - limit: 75, - page, - tags: query.tags.join(" "), - }, - }) - .json<{ posts: readonly Post[] }>(); - - if (!response.posts.length) { - throw new Error("No posts received"); - } - - const postIndex = Math.floor(Math.random() * response.posts.length); - const post = response.posts[postIndex]; - - const isDupe = await dedupe.check({ provider: "e926", id: post.id }); - - if (isDupe) { - await delay(1000); - return getRandomPost(query); - } - - return post; -} -- cgit v1.2.3-70-g09d2