Topic: How do I use e621's API?

Posted under General

A year ago I made a pool downloader with regex/cache/php/etc, but it was a real mess and it failed like 75% of the times I try to download. (it also needed tweaks to work on e926/wc/etc.... so that script is kinda useless and I quit it. :P)

A week ago I noticed this site got an API, so I wanted to give a try to remake the pool downloader. The only problem is: I have no idea how to use this site's API. (Yes, I did read the api help and wiki.)

I was able to get the XML from pool, but I am unable to parse XML attributes. (XML elements are nicer and easier)

Can anyone give me some directions?

Updated

It depends on what platform you're doing it

Updated by anonymous

NSFW said:

I was able to get the XML from pool, but I am unable to parse XML attributes. (XML elements are nicer and easier)

Can anyone give me some directions?

Yes. Use your language's libxml2 bindings, or whatever other library it has available . Don't roll your own, that's just the path to blowing up repeatedly.

That said, XML is annoying to parse even when you have great libraries. I suggest going with JSON instead, as e621 supports it for many types of request -- for example, https://e621.net/pool/show.json?id=12&page=1
. Pretty much every language has decent JSON support by now, and IME it's much easier to handle than XML.

In my case, I use Python's standard 'json' library to parse the JSON, and for getting the JSON from the e621 servers I use the amazingly easy-to-use 'requests' library.

For me, that looks like (quick example that just gets the pool name)

import json
import requests
data = json.loads(requests.get('https://e621.net/pool/show.json?id=12&page=1').text)
pool_name = data['name']
print (pool_name)

Updated by anonymous

I was using php's simpleXML for this. XML itself isn't hard, but XML attributes are annoying to parse.

Indeed... JSON seems to be a lot easier to parse (or even regex it). Now all I need to do is to figure how to use JSON, but nothing on php is hard to learn.

ht tp s://e621.net/pool/show.json?id=$1&page=$2 returns the pool and pages. Not that hard. :D

Thank you

Updated by anonymous

NSFW said:
I was using php's simpleXML for this. XML itself isn't hard, but XML attributes are annoying to parse.

Indeed... JSON seems to be a lot easier to parse (or even regex it). Now all I need to do is to figure how to use JSON, but nothing on php is hard to learn.

https://e621.net/pool/show.json?id=$1&page=$2 returns the pool and pages. Not that hard. :D

Thank you

JSON is really easy to understand, it's just

{"key1":value1, "key2":value2, "keyN":valueN}

This should come in handy for you (taken from JSON.org site)

Updated by anonymous

Almost done.
The only problem I got is: It doesn't download all imgs.
I'm testing the script using the pool #12. This pool has 18 imgs, but the script downloads 17 only. Even the JSON it returns 17 imgs only and there is no page 2, but if you check https://e621.net/pool/show/12 , this pool has 18 imgs. :P

Well, that's funny. This pool doesn't work right, but 1188 seems to work correctly. =P

Updated by anonymous

NSFW said:
Almost done.
The only problem I got is: It doesn't download all imgs.
I'm testing the script using the pool #12. This pool has 18 imgs, but the script downloads 17 only. Even the JSON it returns 17 imgs only and there is no page 2, but if you check https://e621.net/pool/show/12 , this pool has 18 imgs. :P

Well, that's funny. This pool doesn't work right, but 1188 seems to work correctly. =P

That's because there's a deleted image in pool #12.

Updated by anonymous

parasprite said:
That's because there's a deleted image in pool #12.

Oh, I see. I spent last 30 minutes trying to figure what happened. lolz

Well the script: http://pastebin.com/Hxv3krKj
For now, it'll no user/web interface, but this isn't hard to use: script.php?id=pool_id&pg=page_id

All you'll need is: http://www.wampserver.com/en/ (or if you're using Linux, you could try LAMP. I'm not 100% sure, but it should work with linux. You also may need to chmod 777 the folder/script.)

Have fun :P

Updated by anonymous

  • 1