summaryrefslogtreecommitdiffstats
path: root/src/api/e621/index.ts
blob: 47d5dd1dffccc29f0535ada92c4062808b168af1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import got from "got";
import config from "../../config";
import fs from "fs/promises";
import path from "path";

export interface GetPostQuery {
    tags: readonly string[];
    maxPage: number;
}

export interface Post {
    id: number;
    file: {
        url: string;
    };
    sources: readonly string[];

    tags: {
        general: readonly string[];
        species: readonly string[];
        character: readonly string[];
        copyright: readonly string[];
        artist: readonly string[];
        invalid: readonly string[];
        lore: readonly string[];
        meta: readonly string[];
    };
}

export const client = got.extend({
    headers: {
        "User-Agent": config.e621.userAgent,
    },
});

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", {
            searchParams: {
                tags: `id:${id}`,
            },
        })
        .json<{ posts: readonly Post[] }>();

    if (!response.posts.length) {
        throw new Error("No posts received");
    }

    return response.posts[0];
}

export async function getRandomPost(query: GetPostQuery) {
    await loadDedupeDb();

    const page = Math.floor(Math.random() * (query.maxPage - 1)) + 1;

    const response = await client
        .get("https://e926.net/posts.json", {
            searchParams: {
                limit: 75,
                page,
                tags: query.tags.join(" "),
            },
        })
        .json<{ posts: readonly Post[] }>();

    if (!response.posts.length) {
        throw new Error("No posts received");
    }

    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;
}