summaryrefslogtreecommitdiffstats
path: root/src/api/e621
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/e621')
-rw-r--r--src/api/e621/index.ts50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/api/e621/index.ts b/src/api/e621/index.ts
new file mode 100644
index 0000000..6aa6a35
--- /dev/null
+++ b/src/api/e621/index.ts
@@ -0,0 +1,50 @@
1import got from "got";
2import config from "../../config";
3
4export interface Post {
5 id: number;
6 file: {
7 url: string;
8 };
9 sources: readonly string[];
10
11 tags: {
12 general: readonly string[];
13 species: readonly string[];
14 character: readonly string[];
15 copyright: readonly string[];
16 artist: readonly string[];
17 invalid: readonly string[];
18 lore: readonly string[];
19 meta: readonly string[];
20 };
21}
22
23export const client = got.extend({
24 headers: {
25 "User-Agent": config.e621.userAgent,
26 },
27});
28
29export async function randomPost() {
30 const page = Math.floor(Math.random() * (config.e621.maxPage - 1)) + 1;
31
32 const response = await client
33 .get("https://e926.net/posts.json", {
34 searchParams: {
35 limit: 75,
36 page,
37 tags: config.e621.tags.join(" "),
38 },
39 })
40 .json<{ posts: readonly Post[] }>();
41
42 if (!response.posts.length) {
43 throw new Error("No posts received");
44 }
45
46 const postIndex = Math.floor(Math.random() * response.posts.length);
47 const post = response.posts[postIndex];
48
49 return post;
50}