Danbooru

API: get random post with safe rating

Posted under General

The idea of an order:random metatag has been tossed around for quite some time: forum #27132, but never has been implemented. I can't find the exact thread but at one point I believe Albert stated with the size of the database, returning a random sampling would continuously time out.

You could try for a emulation of something vaguely random with order:change_desc rating:s.

Better yet, run a query of limit 100, and pick a random page between 1 and 1000 (the upper limit now for queries), and pick a random record from the return results. Something like:

http://danbooru.donmai.us/post/index.xml?limit=100&page=1000&tags=order:change_desc%20rating:s

It's not a great solution since order:change_desc is hardly truly random, the 100,001st most recently edited post and all earlier are unreachable, you have to do more post-processing on your end, and this query (aside from the random order) actually makes the DB do more work. All that aside, it might approximate what you want to do to the degree a random bystander likely couldn't tell.

Getting a true order:random would be the only way around this, and would depend on Albert devising an efficient way to implementing it, and actually getting around to doing so (which I wouldn't expect until after Danbooru 2 is released).

EDIT: If Albert cared to look at this again, here's a stackOverflow thread that might potentially be useful: http://stackoverflow.com/questions/8674718/best-way-to-select-random-rows-postgresql

Updated

Shinjidude said:

The idea of an order:random metatag has been tossed around for quite some time: forum #27132, but never has been implemented. I can't find the exact thread but at one point I believe Albert stated with the size of the database, returning a random sampling would continuously time out.

You could try for a emulation of something vaguely random with order:change_desc rating:s.

Better yet, run a query of limit 100, and pick a random page between 1 and 1000 (the upper limit now for queries), and pick a random record from the return results. Something like:

http://danbooru.donmai.us/post/index.xml?limit=100&page=1000&tags=order:change_desc%20rating:s

It's not a great solution since order:change_desc is hardly truly random, the 100,001st most recently edited post and all earlier are unreachable, you have to do more post-processing on your end, and this query (aside from the random order) actually makes the DB do more work. All that aside, it might approximate what you want to do to the degree a random bystander likely couldn't tell.

Getting a true order:random would be the only way around this, and would depend on Albert devising an efficient way to implementing it, and actually getting around to doing so (which I wouldn't expect until after Danbooru 2 is released).

EDIT: If Albert cared to look at this again, here's a stackOverflow thread that might potentially be useful: http://stackoverflow.com/questions/8674718/best-way-to-select-random-rows-postgresql

looks like albert got around to doing it, sweet

1