Notice: My personal stance on AI generated artwork. Retweet and share if you agree. Let us discuss, and not immediately scream bloody murder.

Now Viewing: Way to obtain image IDs for certain tag?
Keep it civil, do not flame or bait other users. If you notice anything illegal or inappropriate being discussed, contact an administrator or moderator.

chevady666 - Group: Member - Total Posts: 2
user_avatar
Way to obtain image IDs for certain tag?
Posted on: 12/28/23 01:17AM

Are there any approved methods for obtaining all image IDs associated with specific tag? I aim to create a random image display limited to certain tag. How can I achieve this?

Currently, the only approach I've come up with is to identify the desired tag and then crawl through each page to extract all corresponding IDs. However, I'm uncertain if this is the optimal solution.

thanks!!



Jerl - Group: The Real Administrator - Total Posts: 6704
user_avatar
Posted on: 12/28/23 06:32AM

Wow, you're trying the most roundabout way to accomplish what you're trying to do.

Easier method:
Step 1: Use the tag API to get the number of posts with that tag. E.g. for the tag "no_humans", the query would be gelbooru.com/index.php?pa...=index&name=no_humans
Step 2: Generate a random number less than that number. Let's call that number N.
Step 3: Query the posts API for that tag, limiting the number of posts to 1 per page using the limit parameter and jumping straight to page N using the pid parameter. The query looks like this: gelbooru.com/index.php?pa...mans&limit=1&pid=N

...But unless you really want to use your own random function, that's silly when Gelbooru literally has a random function built in with the "sort:random" metatag, which means that you can do it in one step.

Easiest and correct method:
Step 1: Use "sort:random" with a limit of 1 post.
gelbooru.com/index.php?pa...s+sort:random&limit=1



chevady666 - Group: Member - Total Posts: 2
user_avatar
Posted on: 12/28/23 08:57AM

thanks a lot!!
yes i have already tried the easier method

but i just can't figure out that there's actually an easiest method!!! (Don't know can use +sort:random)

Thanks for helping~



Strahan - Group: Member - Total Posts: 81
user_avatar
Posted on: 12/30/23 06:01PM

Jerl said:
Easiest and correct method:
Step 1: Use "sort:random" with a limit of 1 post.
gelbooru.com/index.php?pa...s+sort:random&limit=1

That's pretty cool. I never knew about that either. It isn't mentioned on the API docs, at least not the doc page I'm familiar with (gelbooru.com/index.php?page=wiki&s=view&id=18780). Is there a more comprehensive documentation somewhere, or is that it?



Jerl - Group: The Real Administrator - Total Posts: 6704
user_avatar
Posted on: 12/30/23 06:06PM

It isn't mentioned in the API docs because it isn't an API feature. It's a normal search feature, also available in the normal search bar. It's explained on the tag cheatsheet along with the other sorting options.



Strahan - Group: Member - Total Posts: 81
user_avatar
Posted on: 12/30/23 06:41PM

Ahh, good point, d'oh. Thanks.



add_replyAdd Reply


1