From 12ee614ef30f7218bbcf1f358ae097019ad7be19 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 19 Oct 2021 08:59:42 +0200 Subject: Avoid duplicate posts --- src/api/e621/index.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/api/e621/index.ts b/src/api/e621/index.ts index 2fc0d2e..47d5dd1 100644 --- a/src/api/e621/index.ts +++ b/src/api/e621/index.ts @@ -1,5 +1,7 @@ import got from "got"; import config from "../../config"; +import fs from "fs/promises"; +import path from "path"; export interface GetPostQuery { tags: readonly string[]; @@ -31,6 +33,35 @@ 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(0, -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", { @@ -48,6 +79,8 @@ 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 @@ -67,5 +100,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)) { + return getRandomPost(query); + } + + dedupeDb.push(post.id); + await saveDedupeDb(); + return post; } -- cgit v1.2.3-70-g09d2