blob: 6aa6a35ead05307329f0ff357260fc1aaa692f76 (
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
|
import got from "got";
import config from "../../config";
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,
},
});
export async function randomPost() {
const page = Math.floor(Math.random() * (config.e621.maxPage - 1)) + 1;
const response = await client
.get("https://e926.net/posts.json", {
searchParams: {
limit: 75,
page,
tags: config.e621.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];
return post;
}
|