diff options
-rw-r--r-- | src/api/e621/index.ts | 40 |
1 files changed, 40 insertions, 0 deletions
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 @@ | |||
1 | import got from "got"; | 1 | import got from "got"; |
2 | import config from "../../config"; | 2 | import config from "../../config"; |
3 | import fs from "fs/promises"; | ||
4 | import path from "path"; | ||
3 | 5 | ||
4 | export interface GetPostQuery { | 6 | export interface GetPostQuery { |
5 | tags: readonly string[]; | 7 | tags: readonly string[]; |
@@ -31,6 +33,35 @@ export const client = got.extend({ | |||
31 | }, | 33 | }, |
32 | }); | 34 | }); |
33 | 35 | ||
36 | const dedupePath = path.join(__dirname, "e926dedupe.json"); | ||
37 | const dedupeMax = 50; | ||
38 | let dedupeDb: number[] | undefined; | ||
39 | |||
40 | async function loadDedupeDb() { | ||
41 | if (dedupeDb) { | ||
42 | return; | ||
43 | } | ||
44 | |||
45 | try { | ||
46 | await fs.stat(dedupePath); | ||
47 | } catch { | ||
48 | await saveDedupeDb(); | ||
49 | } | ||
50 | |||
51 | const d = await fs.readFile(dedupePath, "utf8"); | ||
52 | const dd = JSON.parse(d); | ||
53 | |||
54 | if (dd instanceof Array) { | ||
55 | dedupeDb = dd.slice(0, -1 * dedupeMax); | ||
56 | } else { | ||
57 | dedupeDb = []; | ||
58 | } | ||
59 | } | ||
60 | |||
61 | async function saveDedupeDb() { | ||
62 | await fs.writeFile(dedupePath, JSON.stringify(dedupeDb ?? []), "utf8"); | ||
63 | } | ||
64 | |||
34 | export async function getPostById(id: number) { | 65 | export async function getPostById(id: number) { |
35 | const response = await client | 66 | const response = await client |
36 | .get("https://e926.net/posts.json", { | 67 | .get("https://e926.net/posts.json", { |
@@ -48,6 +79,8 @@ export async function getPostById(id: number) { | |||
48 | } | 79 | } |
49 | 80 | ||
50 | export async function getRandomPost(query: GetPostQuery) { | 81 | export async function getRandomPost(query: GetPostQuery) { |
82 | await loadDedupeDb(); | ||
83 | |||
51 | const page = Math.floor(Math.random() * (query.maxPage - 1)) + 1; | 84 | const page = Math.floor(Math.random() * (query.maxPage - 1)) + 1; |
52 | 85 | ||
53 | const response = await client | 86 | const response = await client |
@@ -67,5 +100,12 @@ export async function getRandomPost(query: GetPostQuery) { | |||
67 | const postIndex = Math.floor(Math.random() * response.posts.length); | 100 | const postIndex = Math.floor(Math.random() * response.posts.length); |
68 | const post = response.posts[postIndex]; | 101 | const post = response.posts[postIndex]; |
69 | 102 | ||
103 | if (dedupeDb.includes(post.id)) { | ||
104 | return getRandomPost(query); | ||
105 | } | ||
106 | |||
107 | dedupeDb.push(post.id); | ||
108 | await saveDedupeDb(); | ||
109 | |||
70 | return post; | 110 | return post; |
71 | } | 111 | } |