summaryrefslogtreecommitdiffstats
path: root/src/api/e621
diff options
context:
space:
mode:
authorVolpeon <git@volpeon.ink>2021-10-19 18:14:00 +0200
committerVolpeon <git@volpeon.ink>2021-10-19 18:14:00 +0200
commita1de58f364ef04c97ecad380427e6181f3bf707d (patch)
treeba3c8df7a209081b858b423674e5c42ffec37ebe /src/api/e621
parentCode improvements (diff)
downloadferalbot-a1de58f364ef04c97ecad380427e6181f3bf707d.tar.gz
feralbot-a1de58f364ef04c97ecad380427e6181f3bf707d.tar.bz2
feralbot-a1de58f364ef04c97ecad380427e6181f3bf707d.zip
Code improvements
Diffstat (limited to 'src/api/e621')
-rw-r--r--src/api/e621/index.ts41
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 @@
1import got from "got"; 1import got from "got";
2import config from "../../config"; 2import config from "../../config";
3import fs from "fs/promises";
4import path from "path";
5import delay from "../../util/delay"; 3import delay from "../../util/delay";
4import dedupe from "../../services/dedupe";
6 5
7export interface GetPostQuery { 6export 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
37const dedupePath = path.join(__dirname, "e926dedupe.json");
38const dedupeMax = 50;
39let dedupeDb: number[] | undefined;
40
41async 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
62async function saveDedupeDb() {
63 await fs.writeFile(dedupePath, JSON.stringify(dedupeDb ?? []), "utf8");
64}
65
66export async function getPostById(id: number) { 36export 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
82export async function getRandomPost(query: GetPostQuery) { 52export 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}