summaryrefslogtreecommitdiffstats
path: root/src/api/e621
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/e621')
-rw-r--r--src/api/e621/index.ts13
1 files changed, 8 insertions, 5 deletions
diff --git a/src/api/e621/index.ts b/src/api/e621/index.ts
index 411edae..2fc0d2e 100644
--- a/src/api/e621/index.ts
+++ b/src/api/e621/index.ts
@@ -1,6 +1,11 @@
1import got from "got"; 1import got from "got";
2import config from "../../config"; 2import config from "../../config";
3 3
4export interface GetPostQuery {
5 tags: readonly string[];
6 maxPage: number;
7}
8
4export interface Post { 9export interface Post {
5 id: number; 10 id: number;
6 file: { 11 file: {
@@ -26,7 +31,7 @@ export const client = got.extend({
26 }, 31 },
27}); 32});
28 33
29export async function getPost(id: number) { 34export async function getPostById(id: number) {
30 const response = await client 35 const response = await client
31 .get("https://e926.net/posts.json", { 36 .get("https://e926.net/posts.json", {
32 searchParams: { 37 searchParams: {
@@ -42,9 +47,7 @@ export async function getPost(id: number) {
42 return response.posts[0]; 47 return response.posts[0];
43} 48}
44 49
45export async function getRandomPost() { 50export async function getRandomPost(query: GetPostQuery) {
46 const queryIndex = Math.floor(Math.random() * config.e621.queries.length);
47 const query = config.e621.queries[queryIndex];
48 const page = Math.floor(Math.random() * (query.maxPage - 1)) + 1; 51 const page = Math.floor(Math.random() * (query.maxPage - 1)) + 1;
49 52
50 const response = await client 53 const response = await client
@@ -64,5 +67,5 @@ export async function getRandomPost() {
64 const postIndex = Math.floor(Math.random() * response.posts.length); 67 const postIndex = Math.floor(Math.random() * response.posts.length);
65 const post = response.posts[postIndex]; 68 const post = response.posts[postIndex];
66 69
67 return { queryIndex, post }; 70 return post;
68} 71}