-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
209 lines (189 loc) · 7.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const request = require('request'); // npm install request
function formatTags(txt) {
return txt.replace(/,/g, ' '); // replace all commas with spaces to format tags
}
/**
* @typedef User
* @property {number} id
* @property {string} username
* @property {number} active
*/
/**
* @typedef Post
* @property {number} id - The id of the post.
* @property {Date} created_at - The date the post was created.
* @property {number} score - The score of the post.
* @property {number} width - The width of the image in the post.
* @property {number} height - The height of the image in the post.
* @property {string} md5 - The md5 hash of the image in the post.
* @property {string} directory - The directory of the image in the post.
* @property {string} image - The image file name of the post.
* @property {string} rating - The rating of the post. (safe, questionable, explicit)
* @property {string} source - The source of the post. (Image)
* @property {Date} change - The date the post was last updated. (UNIX)
* @property {string} owner - The owner of the post.
* @property {number} creator_id - Post creator'r id
* @property {number} parent_id - The parent id of the post.
* @property {number} sample
* @property {number} preview_height - The height of the preview image in the post.
* @property {number} preview_width - The width of the preview image in the post.
* @property {string} tags - The tags of the post.
* @property {string} title - The title of the post.
* @property {string} has_notes - Whether the post has notes.
* @property {string} has_comments - Whether the post has comments.
* @property {string} file_url - The url of the image in the post.
* @property {string} preview_url - The url of the preview image in the post.
* @property {string} sample_url - The url of the sample image in the post.
* @property {number} sample_height - The height of the sample image in the post.
* @property {number} sample_width - The width of the sample image in the post.
* @property {string} status - The status of the post. (active)
* @property {number} post_locked - Whether the post is locked.
* @property {string} has_children - Whether the post has children.
*/
/**
* @typedef Tag
* @property {number} id - The id of the tag, you can use it for searching.
* @property {string} name - The name of the tag.
* @property {number} count - Number of posts with this tag.
*/
class Gelbooru {
/**
* @param {string} tags - Name of tag to search. (Separate tags by commas or spaces)
* @param {number} limit - Number of posts to return.
*/
constructor(tags = '', limit = 10) {
this.baseUrl = 'https://gelbooru.com/index.php?page=dapi&q=index&json=1'; // Base URL for Gelbooru API, you can change it if gelbooru changes it.
this.tags = formatTags(tags); // Tags to search for.
this.limit = 10;
this.lastPosts = [];
}
/**
* @type {Promise}
* @param {string} tags - Tags to search for.
* @param {number} limit - Number of posts to return.
* @param {number} pid - Page to start from (default is 0).
* @return {Promise<Post>}
*/
async getPosts(tags = this.tags, limit = this.limit, pid = 0) {
return new Promise((resolve, reject) => {
request(`${this.baseUrl}&s=post&tags=${tags}&limit=${limit}&pid=${pid}`, (err, res, body) => {
if (err) {
reject(err);
} else {
const posts = JSON.parse(body);
if (!posts.post) resolve({ success: false, message: 'No posts found' });
this.lastPosts = posts;
resolve(posts.post);
}
});
})
}
/**
* @type {Promise}
* @param {string} tags - Tags to search for.
* @param {number} limit - Number of posts to return.
* @param {number} pid - Page to start from (default is 0).
* @return {Promise<Post>}
*/
async getRandomPost(tags = this.tags) {
return new Promise(async (resolve, reject) => {
const posts = await this.getPosts("sort:random "+tags, 1, 1);
if (posts.success == false) resolve(posts);
resolve(posts[0]);
})
}
/**
* @type {Promise}
* @param {number} limit - Number of tags to return.
* @param {string} orderBy - Order by a field ( date, count, name )
* @param {number} afterId - Grab tags whose ID is greater than this value.
* @return {Promise<Tag>}
*/
async getTags(limit = 10, orderBy = 'count', afterId = 0) {
return new Promise((resolve, reject) => {
request(`${this.baseUrl}&s=tag&limit=${limit}&order=${orderBy}&after_id=${afterId}`, (err, res, body) => {
if (err) {
reject(err);
} else {
const tags = JSON.parse(body);
if (!tags.tag) resolve({ success: false, message: 'No tags found' });
resolve(tags.tag);
}
});
})
}
/**
* @type {Promise}
* @param {string} tags - Name of tag to search. (Separate tags by commas or spaces)
* @return {Promise<Tag>}
*/
async searchTags(tags = '') {
return new Promise((resolve, reject) => {
request(`${this.baseUrl}&s=tag&names=${tags}`, (err, res, body) => {
if (err) {
reject(err);
} else {
const tags = JSON.parse(body);
if (!tags.tag) resolve({ success: false, message: 'No tags found' });
resolve(tags.tag);
}
});
})
}
/**
* @type {Promise}
* @param {string} name - Name of user to search.
* @param {number} limit - Number of users to return.
* @return {Promise<User>}
*/
async searchUser(name = '',) {
return new Promise((resolve, reject) => {
request(`${this.baseUrl}&s=user&name=${name}`, (err, res, body) => {
if (err) {
reject(err);
} else {
const user = JSON.parse(body);
if (!user.user) resolve({ success: false, message: 'No user found' });
resolve(user.user);
}
});
})
}
/**
* @type {Promise}
* @param {number} id - Post id to search.
* @return {Promise<Post>}
*/
async getPostById(id = 0) {
return new Promise((resolve, reject) => {
request(`${this.baseUrl}&s=post&id=${id}`, (err, res, body) => {
if (err) {
reject(err);
} else {
const post = JSON.parse(body);
if (!post.post) resolve({ success: false, message: 'No post found' });
resolve(post.post);
}
});
})
}
/**
* @type {Promise}
* @param {string} id - Tag id to search for.
* @return {Promise<Tag>}
*/
async getTagById(id = 0) {
return new Promise((resolve, reject) => {
request(`${this.baseUrl}&s=tag&id=${id}`, (err, res, body) => {
if (err) {
reject(err);
} else {
const tag = JSON.parse(body);
if (!tag.tag) resolve({ success: false, message: 'No tag found' });
resolve(tag.tag);
}
});
})
}
}
module.exports = Gelbooru;