diff options
| -rw-r--r-- | src/api/e621/index.ts | 13 | ||||
| -rw-r--r-- | src/config.ts | 4 | ||||
| -rw-r--r-- | src/index.ts | 8 |
3 files changed, 16 insertions, 9 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 @@ | |||
| 1 | import got from "got"; | 1 | import got from "got"; |
| 2 | import config from "../../config"; | 2 | import config from "../../config"; |
| 3 | 3 | ||
| 4 | export interface GetPostQuery { | ||
| 5 | tags: readonly string[]; | ||
| 6 | maxPage: number; | ||
| 7 | } | ||
| 8 | |||
| 4 | export interface Post { | 9 | export 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 | ||
| 29 | export async function getPost(id: number) { | 34 | export 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 | ||
| 45 | export async function getRandomPost() { | 50 | export 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 | } |
diff --git a/src/config.ts b/src/config.ts index d854725..6b0816d 100644 --- a/src/config.ts +++ b/src/config.ts | |||
| @@ -1,4 +1,6 @@ | |||
| 1 | const mainQuery = { | 1 | import { GetPostQuery } from "./api/e621"; |
| 2 | |||
| 3 | const mainQuery: GetPostQuery = { | ||
| 2 | tags: [ | 4 | tags: [ |
| 3 | "feral", | 5 | "feral", |
| 4 | "-anthro", | 6 | "-anthro", |
diff --git a/src/index.ts b/src/index.ts index e2fee01..3b8b0a8 100644 --- a/src/index.ts +++ b/src/index.ts | |||
| @@ -19,7 +19,9 @@ const args = cliArgs.parse<{ | |||
| 19 | async function postRandomPicture() { | 19 | async function postRandomPicture() { |
| 20 | console.log("Fetching random post..."); | 20 | console.log("Fetching random post..."); |
| 21 | 21 | ||
| 22 | const { queryIndex, post } = await e621.getRandomPost(); | 22 | const queryIndex = Math.floor(Math.random() * config.e621.queries.length); |
| 23 | const query = config.e621.queries[queryIndex]; | ||
| 24 | const post = await e621.getRandomPost(query); | ||
| 23 | 25 | ||
| 24 | console.log(`Got ${post.id} via query ${queryIndex}`); | 26 | console.log(`Got ${post.id} via query ${queryIndex}`); |
| 25 | 27 | ||
| @@ -27,9 +29,9 @@ async function postRandomPicture() { | |||
| 27 | } | 29 | } |
| 28 | 30 | ||
| 29 | async function postSpecificPicture(id: number) { | 31 | async function postSpecificPicture(id: number) { |
| 30 | console.log("Fetching post ${id}..."); | 32 | console.log(`Fetching post ${id}...`); |
| 31 | 33 | ||
| 32 | const post = await e621.getPost(id); | 34 | const post = await e621.getPostById(id); |
| 33 | 35 | ||
| 34 | console.log(`Got ${post.id}`); | 36 | console.log(`Got ${post.id}`); |
| 35 | 37 | ||
