Donmai

How to get pretty filename with API?

Posted under General

When I GET "https://danbooru.donmai.us/posts/6205254.json" it gives me this file_url:

https://cdn.donmai.us/original/c1/5e/c15e273bb39562ca46528361d3854dc8.jpg

However, that only contains the hash. If I go to the actual post (https://danbooru.donmai.us/posts/6205254) then it has this URL:

https://cdn.donmai.us/original/c1/5e/__takarada_rikka_yuuka_and_reisalin_stout_blue_archive_and_4_more_drawn_by_the_olphy__c15e273bb39562ca46528361d3854dc8.jpg

This URL contains the characters, artist, and the hash. How do I get that "prettier" filename from the API?

If I just concatenate the tag_string_character and tag_string_artist, that doesn't work because Danbooru is doing some fancy stuff to convert the character and artist into a filename.

Talulah said:

You can see the function Danbooru uses to generate that tag string here.

Thanks, that helps a lot! It would be useful if Danbooru included the pretty filename in the JSON response (maybe as a filename field?), but in the meantime I can just copy that code.

Oh, the tag string function relies on the post_count, which means I would need to make a separate GET request for every tag in order to get the post_count.

I'd rather not do that... so that's an argument in favor of including it in the JSON response, to avoid needing to make many tag GETs.

Including it in the API request would make every single post API request slower despite nobody really needing it, so I don't envision it being added. You can always maintain a cache of all the post counts if it's something you need frequently enough that an extra request is too much overhead.

Well that could be easily fixed by just making it a toggle option, like https://danbooru.donmai.us/posts/6205254.json?[filename] so that way it only includes it if the user requests it.

To be clear, it's not just 1 extra request, but potentially hundreds or thousands. I'd like to avoid slamming the server if I can.

There are only four posts on the site with >1000 tags (1000 results being the limit for a single request to /tags), so it should very rarely require more than one additional request.

post = HTTP.get("https://danbooru.donmai.us/posts/6205254.json").parse
tags = HTTP.post("https://danbooru.donmai.us/tags.json", form: {
  "search[name_space]": post["tag_string"],
  limit: 1000
}, headers: {"X-Http-Method-Override": "GET"}).parse
1