24 lines
957 B
TypeScript
24 lines
957 B
TypeScript
import { env } from "bun";
|
|
import GelbooruApiResponse from "src/types/GelbooruApiResponse";
|
|
import GelbooruServiceResponse from "src/types/GelbooruServiceResponse";
|
|
|
|
class GelbooruApiService {
|
|
async get(): Promise<GelbooruServiceResponse> {
|
|
const LIMIT = env.GELBOORU_IMAGES_PER_REQUEST || 100;
|
|
const TAGS = encodeURIComponent(env.GELBOORU_TAGS || "");
|
|
const url: string = `https://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=${LIMIT}&json=1&tags=${TAGS}`;
|
|
|
|
const response: GelbooruApiResponse = await fetch(url)
|
|
.then(res => {
|
|
if (!res.ok) {
|
|
throw new Error("Error fetching images");
|
|
} else {
|
|
return res.json();
|
|
}
|
|
}) as GelbooruApiResponse;
|
|
|
|
return { posts: response.post.map(post => ({ url: post.file_url, tags: post.tags.split(" ") })) };
|
|
}
|
|
}
|
|
|
|
export default new GelbooruApiService(); |