2024-03-28 11:43:24 +00:00
|
|
|
import GelbooruServiceResponse from "../types/GelbooruServiceResponse";
|
|
|
|
import Image from "../types/Image";
|
|
|
|
import BotApiService from "./BotApiService";
|
|
|
|
import GelbooruApiService from "./GelbooruApiService";
|
|
|
|
|
|
|
|
class ImageService {
|
|
|
|
postsQueue: Image[] = [];
|
2024-04-12 11:42:25 +00:00
|
|
|
|
2024-03-28 11:43:24 +00:00
|
|
|
async get(): Promise<Image> {
|
|
|
|
while (this.postsQueue.length === 0) {
|
|
|
|
const validPosts = await this.getNewValidImages();
|
|
|
|
this.postsQueue = validPosts;
|
|
|
|
}
|
|
|
|
return this.postsQueue.pop() as Image;
|
|
|
|
}
|
2024-03-28 15:52:39 +00:00
|
|
|
|
2024-03-28 11:43:24 +00:00
|
|
|
private async getNewValidImages(): Promise<Image[]> {
|
|
|
|
const gelbooruResponse: GelbooruServiceResponse = await GelbooruApiService.get();
|
|
|
|
const posts = gelbooruResponse.posts;
|
|
|
|
|
|
|
|
const botResponse = await BotApiService.getAll();
|
|
|
|
const imagesUrls = botResponse.images.map(image => image.url);
|
|
|
|
|
2024-04-12 11:42:25 +00:00
|
|
|
const validPosts = Promise.all(posts
|
2024-03-28 11:43:24 +00:00
|
|
|
.filter(post => !imagesUrls.some(url => url === post.url))
|
2024-04-15 08:54:01 +00:00
|
|
|
.map((post): Image => {
|
|
|
|
return { url: post.url, tags: post.tags };
|
2024-04-12 11:42:25 +00:00
|
|
|
}));
|
|
|
|
|
2024-03-28 11:43:24 +00:00
|
|
|
return validPosts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new ImageService();
|