Implemented GelbooruApiService

This commit is contained in:
Sugui 2024-03-28 11:41:36 +01:00
parent 2f7b1cc07d
commit 953dcfe38b
4 changed files with 73 additions and 1 deletions

View File

@ -0,0 +1,42 @@
export default interface GelbooruApiResponse {
"@attributes": Attributes
post: Post[]
}
export interface Attributes {
limit: number
offset: number
count: number
}
export interface Post {
id: number
created_at: string
score: number
width: number
height: number
md5: string
directory: string
image: string
rating: string
source: string
change: number
owner: string
creator_id: number
parent_id: number
sample: number
preview_height: number
preview_width: number
tags: string
title: string
has_notes: string
has_comments: string
file_url: string
preview_url: string
sample_url: string
sample_height: number
sample_width: number
status: string
post_locked: number
has_children: string
}

View File

@ -0,0 +1,8 @@
export default interface GelbooruServiceResponse {
posts: GelbooruPostResponse[]
}
interface GelbooruPostResponse {
url: string;
tags: string[];
}

View File

@ -1,4 +1,4 @@
interface IImageData {
export default interface Image {
url: string;
content: string;
}

View File

@ -0,0 +1,22 @@
import GelbooruServiceResponse from "../models/GelbooruServiceResponse";
import GelbooruApiResponse from "../models/GelbooruApiResponse";
class GelbooruApiService {
async get(): Promise<GelbooruServiceResponse> {
const limit = 100;
const url: string = `https://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=${limit}&json=1&tags=2girls+sleeping`
const response: GelbooruApiResponse = await fetch(url)
.then(res => {
if (!res.ok) {
throw new Error("Error fetching images");
} else {
res.json();
}
}) as GelbooruApiResponse;
return { posts: response.post.map(post => ({ url: post.file_url, tags: post.tags.split(" ") })) };
}
}
export default new GelbooruApiService();