From a1de58f364ef04c97ecad380427e6181f3bf707d Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 19 Oct 2021 18:14:00 +0200 Subject: Code improvements --- src/api/e621/index.ts | 41 ++++------------------------------------- 1 file changed, 4 insertions(+), 37 deletions(-) (limited to 'src/api/e621') diff --git a/src/api/e621/index.ts b/src/api/e621/index.ts index 5c5fd2a..a8abbcf 100644 --- a/src/api/e621/index.ts +++ b/src/api/e621/index.ts @@ -1,8 +1,7 @@ import got from "got"; import config from "../../config"; -import fs from "fs/promises"; -import path from "path"; import delay from "../../util/delay"; +import dedupe from "../../services/dedupe"; export interface GetPostQuery { tags: readonly string[]; @@ -34,35 +33,6 @@ export const client = got.extend({ }, }); -const dedupePath = path.join(__dirname, "e926dedupe.json"); -const dedupeMax = 50; -let dedupeDb: number[] | undefined; - -async function loadDedupeDb() { - if (dedupeDb) { - return; - } - - try { - await fs.stat(dedupePath); - } catch { - await saveDedupeDb(); - } - - const d = await fs.readFile(dedupePath, "utf8"); - const dd = JSON.parse(d); - - if (dd instanceof Array) { - dedupeDb = dd.slice(-1 * dedupeMax); - } else { - dedupeDb = []; - } -} - -async function saveDedupeDb() { - await fs.writeFile(dedupePath, JSON.stringify(dedupeDb ?? []), "utf8"); -} - export async function getPostById(id: number) { const response = await client .get("https://e926.net/posts.json", { @@ -80,8 +50,6 @@ export async function getPostById(id: number) { } export async function getRandomPost(query: GetPostQuery) { - await loadDedupeDb(); - const page = Math.floor(Math.random() * (query.maxPage - 1)) + 1; const response = await client @@ -101,13 +69,12 @@ export async function getRandomPost(query: GetPostQuery) { const postIndex = Math.floor(Math.random() * response.posts.length); const post = response.posts[postIndex]; - if (dedupeDb.includes(post.id)) { + const isDupe = await dedupe.check({ provider: "e926", id: post.id }); + + if (isDupe) { await delay(1000); return getRandomPost(query); } - dedupeDb.push(post.id); - await saveDedupeDb(); - return post; } -- cgit v1.2.3-70-g09d2