summaryrefslogtreecommitdiffstats
path: root/src/api/e621/index.ts
diff options
context:
space:
mode:
authorVolpeon <git@volpeon.ink>2021-10-17 16:54:53 +0200
committerVolpeon <git@volpeon.ink>2021-10-17 16:54:53 +0200
commit30b0adcacef48ac53aea13cbdc3288db0bd8d103 (patch)
tree402f4cc31a2fa4f0e746c81755b537b5f5137e4c /src/api/e621/index.ts
downloadferalbot-30b0adcacef48ac53aea13cbdc3288db0bd8d103.tar.gz
feralbot-30b0adcacef48ac53aea13cbdc3288db0bd8d103.tar.bz2
feralbot-30b0adcacef48ac53aea13cbdc3288db0bd8d103.zip
Init
Diffstat (limited to 'src/api/e621/index.ts')
-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}