Topic: API findings and questions

Posted under e621 Tools and Applications

Hello,

I maintain my own app, which helps me easily add and organize posts in my sets (sets with specific topics like avian or non avian or etc., that I have maintained for quite some time)
Sadly, like many other devs, my app was broken after the update.
Since then I was looking around for some more descriptive documentation (since the current one hasn't been updated yet) and found this specific file inside your site's source code: https://github.com/zwagoth/e621ng/blob/master/doc/api.txt
It looks like this api reference is somewhat similar to the new one but with missing/wrong information, like the set endpoints, but might be useful in constructing the new docs further.

So my question is: What endpoint do I need to use for adding/removing posts to sets?

Old api:
"https://e621.net/set/add_post.json?login=" + username + "&password_hash=" + apiKey + "&set_id=" + setID + "&post_id=" + postID
"https://e621.net/set/remove_post.json?login=" + username + "&password_hash=" + apiKey + "&set_id=" + setID + "&post_id=" + postID

Pup

Privileged

For editing a set the endpoint is https://e621.net/post_sets/<ID>.json, the new API uses different HTTP methods to say what action you want to perform, such as POST to create a set, DELETE to delete a set and PATCH to update a set.

I've found I can edit a set, but can't find how to add/remove posts from one, also a successful response seems to be HTML rather than JSON for some reason.

The site's code for dealing with sets is here:
https://github.com/zwagoth/e621ng/blob/master/app/controllers/post_sets_controller.rb

The "edit posts > update" button submits to https://e621.net/post_sets/<ID>/update_posts, though I'm pretty sure that's not an endpoint given it returns "That page does not exist" for each of the usual methods.

I've tried several combinations of methods and parameters but still aren't sure how you're meant to add or remove posts.

Also, you should really use HTTP Basic Auth for authorisation, to avoid putting the login parameters in the URL.

If you're using Python, the Requests library makes it really easy:

import requests
from requests.auth import HTTPBasicAuth

session = requests.Session()

headers = {'User-Agent':'<App Name>/<Version> (by <username> on E621)'}

params = {'<parameter>':<value>,
            '<parameter2>':<value2>
            '<parameter3>':<value3>}

response = session.<post/patch/put/etc>(url, headers=headers, params=params, auth=HTTPBasicAuth(apiUsername, apiKey))

print(response.json())
print(response.status_code)

If not I'd still recommend finding a library to handle things for you.

Add only:
POST https://e621.net/post_sets/<Set_ID>/add_posts.json with post_ids[]:"ID"
doesn't seem to support adding multiple at once

post_ids%5B%5D=postid

Returns json

Remove only:
POST https://e621.net/post_sets/<Set_ID>/remove_posts.json with post_ids[]:"ID"
doesn't seem to support removing multiple at once

post_ids%5B%5D=postid

Returns json

Add or remove:
POST https://e621.net/post_sets/<Set_ID>/update_posts.json with post_set[post_ids_string]:"ID1+ID2+ID3..."
supports adding or removing multiple at once

post_set%5Bpost_ids_string%5D=ID1%2BID2

Returns html

Tested all 3 and they work.

Updated

pup said:
Also, you should really use HTTP Basic Auth for authorisation, to avoid putting the login parameters in the URL.

Ah, thanks for the tip there

aobird said:
Tested all 3 and they work.

Hmm, will look into this, thank you very much!

EDIT: It works!
Only problem now, it doesn't respond with a success message, but with the set data json (including all it's posts!)
Oh well, It certainly got my app working again!

Updated

  • 1