From 2f973f910ba236542e99765c3544064d2c0d388c Mon Sep 17 00:00:00 2001 From: Volpeon Date: Mon, 18 Oct 2021 13:57:48 +0200 Subject: Better interfaces and function responsibilities --- src/api/e621/index.ts | 13 ++++++++----- src/config.ts | 4 +++- src/index.ts | 8 +++++--- 3 files changed, 16 insertions(+), 9 deletions(-) (limited to 'src') 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 @@ import got from "got"; import config from "../../config"; +export interface GetPostQuery { + tags: readonly string[]; + maxPage: number; +} + export interface Post { id: number; file: { @@ -26,7 +31,7 @@ export const client = got.extend({ }, }); -export async function getPost(id: number) { +export async function getPostById(id: number) { const response = await client .get("https://e926.net/posts.json", { searchParams: { @@ -42,9 +47,7 @@ export async function getPost(id: number) { return response.posts[0]; } -export async function getRandomPost() { - const queryIndex = Math.floor(Math.random() * config.e621.queries.length); - const query = config.e621.queries[queryIndex]; +export async function getRandomPost(query: GetPostQuery) { const page = Math.floor(Math.random() * (query.maxPage - 1)) + 1; const response = await client @@ -64,5 +67,5 @@ export async function getRandomPost() { const postIndex = Math.floor(Math.random() * response.posts.length); const post = response.posts[postIndex]; - return { queryIndex, post }; + return post; } 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 @@ -const mainQuery = { +import { GetPostQuery } from "./api/e621"; + +const mainQuery: GetPostQuery = { tags: [ "feral", "-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<{ async function postRandomPicture() { console.log("Fetching random post..."); - const { queryIndex, post } = await e621.getRandomPost(); + const queryIndex = Math.floor(Math.random() * config.e621.queries.length); + const query = config.e621.queries[queryIndex]; + const post = await e621.getRandomPost(query); console.log(`Got ${post.id} via query ${queryIndex}`); @@ -27,9 +29,9 @@ async function postRandomPicture() { } async function postSpecificPicture(id: number) { - console.log("Fetching post ${id}..."); + console.log(`Fetching post ${id}...`); - const post = await e621.getPost(id); + const post = await e621.getPostById(id); console.log(`Got ${post.id}`); -- cgit v1.2.3-70-g09d2