Notice: Visit your account options to access your Patreon rewards. This offer is for a limited time only! Experiencing slow speeds? We are out of bandwidth with no way to afford an upgrade. If you notice slowness, we are working on it.
Ticket Information - ID: #835
|ID:||Category:||Severity||Reproducibility||Date Submitted||Updated By:|
|0000835||Feature Request||normal||N/A||04/28/16 12:43PM||Alzarath|
|Summary:||Random image from tags for API|
|Description:||It would be nice if we could fetch a random image from given tags.|
Alzarath replied at 2016-04-29 07:39:20
|For example, adding &random=1 would fetch random images from all of the available images that could be found from the site that contains the &tags results.|
It would be preferred that they're sorted randomly so results with few images don't end up giving the same order of results.
Jerl replied at 2016-04-29 18:53:36
|For now, it's pretty simple to just select a random image yourself programmatically.|
"<posts count="x" offset="0">"
Take x and integer divide it by 100 to get n. Since it's integer division, it'll ignore the remainder.
Generate a random number r between 0 and n. That'll give you a random page.
Load the search again with the offset set to r * 100. That'll load the random page.
Load the post entries into an array or list. Generate a random number from 0 through the number of posts on the page - 1 (should normally be 99 unless you managed to get the last page). Select the entry at that index from the array/list. You have your random post.
For my purposes, I actually just use that last paragraph and only select a random post from the first page of results since that's sufficient for my needs, but your needs might be different.
Alzarath replied at 2016-05-02 00:45:20
|No doubt it'd be pretty easy to get it on the client-side, but needing to fetch 100 results for the sake of adequate randomization just seems unnecessarily bandwidth-intensive. As someone without a high-end internet connection available to them, it can be a bit of a big deal.|
I'll probably do it for now, but I'd like if it were supported by the API in the end.
Jerl replied at 2016-05-02 02:04:13
|You don't need to fetch 100 results at a time - you can change the number of results the API gives you per page to whatever you want. 100 is only the default.|
If you're short on bandwidth, you can instead do it like this:
First, load the API for the search you're trying to perform INCLUDING the limit of 1 post per page. You can do this by adding "&limit=1" to the end of the request URL. You'll get a result something like
"<posts count="100651" offset="0">"
Generate a random number smaller than the count. In this case, I'm going to use 23515. Set that number as the offset. You can do that by adding "&pid=23515" to the end of the previous request URL. You'll get something like
"<posts count="100651" offset="23515">
<post height="1131" score="50" file_url="http://simg4.gelbooru.com/images/05/47/05472a6c5c0c4b3aad40d99467e86318.jpeg" parent_id="" sample_url="http://simg4.gelbooru.com/images/05/47/05472a6c5c0c4b3aad40d99467e86318.jpeg" sample_width="800" sample_height="1131" preview_url="http://gelbooru.com/thumbnails/05/47/thumbnail_05472a6c5c0c4b3aad40d99467e86318.jpg" rating="e" tags=" 5boys 5girls aihara_enju black_bullet black_hair blonde_hair blue_eyes blue_hair braid everyone fellatio ikuma_shougen loli multiple_boys multiple_girls oral orange_hair pink_hair satomi_rentarou senju_kayo short_hair twintails y.ssanoha " id="2317366" width="800" change="1432247980" md5="05472a6c5c0c4b3aad40d99467e86318" creator_id="124225" has_children="false" created_at="Thu Jul 03 11:35:04 -0500 2014" status="active" source="http://www.pixiv.net/member_illust.php?mode=medium&illust_id=44393104" has_notes="false" has_comments="true" preview_width="106" preview_height="150"/>
That's your random post. Total bandwidth used is ~1.5-2KB, which is a negligible difference from just loading one page.
Ultimately this is probably the better way to do it than the method I explained above, but requires more understanding of how the API works.
Alzarath replied at 2016-05-02 06:52:25
|Oh, I understand what you mean now. Yeah, that could definitely work for what I need, only I've been using the json mode which doesn't not seem to have a `posts count`.|
I'll look into seeing if I can get xml to work with my application. It would be nice if the json included this information as well, though.