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