diff options
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/e621/index.ts | 50 | ||||
-rw-r--r-- | src/api/mastodon/index.ts | 58 | ||||
-rw-r--r-- | src/api/misskey/index.ts | 26 |
3 files changed, 134 insertions, 0 deletions
diff --git a/src/api/e621/index.ts b/src/api/e621/index.ts new file mode 100644 index 0000000..6aa6a35 --- /dev/null +++ b/src/api/e621/index.ts | |||
@@ -0,0 +1,50 @@ | |||
1 | import got from "got"; | ||
2 | import config from "../../config"; | ||
3 | |||
4 | export interface Post { | ||
5 | id: number; | ||
6 | file: { | ||
7 | url: string; | ||
8 | }; | ||
9 | sources: readonly string[]; | ||
10 | |||
11 | tags: { | ||
12 | general: readonly string[]; | ||
13 | species: readonly string[]; | ||
14 | character: readonly string[]; | ||
15 | copyright: readonly string[]; | ||
16 | artist: readonly string[]; | ||
17 | invalid: readonly string[]; | ||
18 | lore: readonly string[]; | ||
19 | meta: readonly string[]; | ||
20 | }; | ||
21 | } | ||
22 | |||
23 | export const client = got.extend({ | ||
24 | headers: { | ||
25 | "User-Agent": config.e621.userAgent, | ||
26 | }, | ||
27 | }); | ||
28 | |||
29 | export async function randomPost() { | ||
30 | const page = Math.floor(Math.random() * (config.e621.maxPage - 1)) + 1; | ||
31 | |||
32 | const response = await client | ||
33 | .get("https://e926.net/posts.json", { | ||
34 | searchParams: { | ||
35 | limit: 75, | ||
36 | page, | ||
37 | tags: config.e621.tags.join(" "), | ||
38 | }, | ||
39 | }) | ||
40 | .json<{ posts: readonly Post[] }>(); | ||
41 | |||
42 | if (!response.posts.length) { | ||
43 | throw new Error("No posts received"); | ||
44 | } | ||
45 | |||
46 | const postIndex = Math.floor(Math.random() * response.posts.length); | ||
47 | const post = response.posts[postIndex]; | ||
48 | |||
49 | return post; | ||
50 | } | ||
diff --git a/src/api/mastodon/index.ts b/src/api/mastodon/index.ts new file mode 100644 index 0000000..2d8636e --- /dev/null +++ b/src/api/mastodon/index.ts | |||
@@ -0,0 +1,58 @@ | |||
1 | import got from "got"; | ||
2 | import FormData from "form-data"; | ||
3 | import fileType from "file-type"; | ||
4 | import { nanoid } from "nanoid"; | ||
5 | import config from "../../config"; | ||
6 | |||
7 | export interface Attachment { | ||
8 | id: string; | ||
9 | } | ||
10 | |||
11 | export interface Status { | ||
12 | url: string; | ||
13 | } | ||
14 | |||
15 | export const client = got.extend({ | ||
16 | prefixUrl: config.mastodon.instance, | ||
17 | headers: { | ||
18 | Authorization: `Bearer ${config.mastodon.token}`, | ||
19 | }, | ||
20 | }); | ||
21 | |||
22 | export async function upload(buf: Buffer, filename: string) { | ||
23 | const type = await fileType.fromBuffer(buf); | ||
24 | |||
25 | const body = new FormData(); | ||
26 | body.append("i", config.mastodon.token); | ||
27 | body.append("file", buf, { filename: `${filename}.${type.ext}`, contentType: type.mime }); | ||
28 | |||
29 | return client.post("api/v1/media", { body }).json<Attachment>(); | ||
30 | } | ||
31 | |||
32 | export async function createStatus( | ||
33 | postUrl: string, | ||
34 | sourceUrl: string | undefined, | ||
35 | spoiler: string[], | ||
36 | attachmentId: string | ||
37 | ) { | ||
38 | let lines = [postUrl]; | ||
39 | if (sourceUrl) { | ||
40 | lines.push(`Source: ${sourceUrl}`); | ||
41 | } | ||
42 | |||
43 | const spoilerText = spoiler.length ? `CW: ${spoiler.join(", ")}` : undefined; | ||
44 | |||
45 | return client | ||
46 | .post("api/v1/statuses", { | ||
47 | headers: { | ||
48 | "Idempotency-Key": nanoid(), | ||
49 | }, | ||
50 | json: { | ||
51 | status: lines.join("\n"), | ||
52 | media_ids: [attachmentId], | ||
53 | sensitive: true, | ||
54 | spoiler_text: spoilerText, | ||
55 | }, | ||
56 | }) | ||
57 | .json<Status>(); | ||
58 | } | ||
diff --git a/src/api/misskey/index.ts b/src/api/misskey/index.ts new file mode 100644 index 0000000..64bfe67 --- /dev/null +++ b/src/api/misskey/index.ts | |||
@@ -0,0 +1,26 @@ | |||
1 | /* | ||
2 | import got from "got"; | ||
3 | import FormData from "form-data"; | ||
4 | import stream from "stream"; | ||
5 | import config from "../../config"; | ||
6 | |||
7 | export interface DriveFile { | ||
8 | id: string; | ||
9 | } | ||
10 | |||
11 | export const client = got.extend({ | ||
12 | prefixUrl: config.misskey.instance, | ||
13 | }); | ||
14 | |||
15 | export function upload(buf: Buffer, filename: string) { | ||
16 | const body = new FormData(); | ||
17 | body.append("i", config.misskey.token); | ||
18 | body.append("file", buf, { filename }); | ||
19 | |||
20 | return client.post("drive/files/create", { body }).json<DriveFile>(); | ||
21 | }; | ||
22 | |||
23 | export function createNote() { | ||
24 | |||
25 | } | ||
26 | */ | ||