diff options
Diffstat (limited to 'src/api/e621')
| -rw-r--r-- | src/api/e621/index.ts | 80 |
1 files changed, 0 insertions, 80 deletions
diff --git a/src/api/e621/index.ts b/src/api/e621/index.ts deleted file mode 100644 index a8abbcf..0000000 --- a/src/api/e621/index.ts +++ /dev/null | |||
| @@ -1,80 +0,0 @@ | |||
| 1 | import got from "got"; | ||
| 2 | import config from "../../config"; | ||
| 3 | import delay from "../../util/delay"; | ||
| 4 | import dedupe from "../../services/dedupe"; | ||
| 5 | |||
| 6 | export interface GetPostQuery { | ||
| 7 | tags: readonly string[]; | ||
| 8 | maxPage: number; | ||
| 9 | } | ||
| 10 | |||
| 11 | export interface Post { | ||
| 12 | id: number; | ||
| 13 | file: { | ||
| 14 | url: string; | ||
| 15 | }; | ||
| 16 | sources: readonly string[]; | ||
| 17 | |||
| 18 | tags: { | ||
| 19 | general: readonly string[]; | ||
| 20 | species: readonly string[]; | ||
| 21 | character: readonly string[]; | ||
| 22 | copyright: readonly string[]; | ||
| 23 | artist: readonly string[]; | ||
| 24 | invalid: readonly string[]; | ||
| 25 | lore: readonly string[]; | ||
| 26 | meta: readonly string[]; | ||
| 27 | }; | ||
| 28 | } | ||
| 29 | |||
| 30 | export const client = got.extend({ | ||
| 31 | headers: { | ||
| 32 | "User-Agent": config.e621.userAgent, | ||
| 33 | }, | ||
| 34 | }); | ||
| 35 | |||
| 36 | export async function getPostById(id: number) { | ||
| 37 | const response = await client | ||
| 38 | .get("https://e926.net/posts.json", { | ||
| 39 | searchParams: { | ||
| 40 | tags: `id:${id}`, | ||
| 41 | }, | ||
| 42 | }) | ||
| 43 | .json<{ posts: readonly Post[] }>(); | ||
| 44 | |||
| 45 | if (!response.posts.length) { | ||
| 46 | throw new Error("No posts received"); | ||
| 47 | } | ||
| 48 | |||
| 49 | return response.posts[0]; | ||
| 50 | } | ||
| 51 | |||
| 52 | export async function getRandomPost(query: GetPostQuery) { | ||
| 53 | const page = Math.floor(Math.random() * (query.maxPage - 1)) + 1; | ||
| 54 | |||
| 55 | const response = await client | ||
| 56 | .get("https://e926.net/posts.json", { | ||
| 57 | searchParams: { | ||
| 58 | limit: 75, | ||
| 59 | page, | ||
| 60 | tags: query.tags.join(" "), | ||
| 61 | }, | ||
| 62 | }) | ||
| 63 | .json<{ posts: readonly Post[] }>(); | ||
| 64 | |||
| 65 | if (!response.posts.length) { | ||
| 66 | throw new Error("No posts received"); | ||
| 67 | } | ||
| 68 | |||
| 69 | const postIndex = Math.floor(Math.random() * response.posts.length); | ||
| 70 | const post = response.posts[postIndex]; | ||
| 71 | |||
| 72 | const isDupe = await dedupe.check({ provider: "e926", id: post.id }); | ||
| 73 | |||
| 74 | if (isDupe) { | ||
| 75 | await delay(1000); | ||
| 76 | return getRandomPost(query); | ||
| 77 | } | ||
| 78 | |||
| 79 | return post; | ||
| 80 | } | ||
