Code Monkey home page Code Monkey logo

e621-py's Introduction

e621

e621 is a feature-rich high-level e621 and e926 API wrapper.

It provides access to almost all of the endpoints available. The only exceptions are unstable and admin-only endpoints.

e621 API documentation is currently highly undocumented, unstable, and sometimes even untruthful. We tried to wrap it in a sanest possible way, properly documenting all the possible ways to interact with it. However, if you still have any questions, bugs to report, or features to request -- please, create an issue on our github page and we will reply as soon as we can.

Installation

$ pip install e621

Quickstart

We translate everything the API returns to python data types created with pydantic. Everything is 100% typehinted so you get autocomplete everywhere and your IDE will warn you if you are sending invalid arguments or using nonexistent attributes.

Creating the api client

  • To create the most basic client, all you need to do is
from e621 import E621

api = E621()
  • If you wish to get information about your account, use your blacklist or create/update/delete any of the e621's entities, you will have to create an api key and put it into the API client as such:
api = E621(("your_e621_login", "your_e621_api_key"))

Searching

The majority of the endpoints allow you to query for a list of their entities, be it posts, pools or tags.

  • To search for posts that match the "canine" but not the "3d" tag:
posts = api.posts.search("canine -3d")
# Or
posts = api.posts.search(["canine", "-3d"])
  • To search for pools whose names start with "hello" and end with "kitty":
posts = api.pools.search(name_matches="hello*kitty")
  • e621 searching api is paginated, which means that if you want to get a lot of posts, you will have to make multiple requests with a different "page" parameter. To simplify interactions with paginated queries, all of our searching endpoints support the "limit", "page", and "ignore_pagination" parameters. If you wish to get a specific number of entities, simply pass the "limit" and "ignore_pagination" arguments:
tags = api.tags.search(name_matches="large_*", limit=900, ignore_pagination=True)

Accessing Attributes

When you have retrieved the entities, you can access any of their attributes without dealing with json.

for post in posts:
    print(post.score.total, post.all_tags, post.relationships.parent_id)
    with open(f"{post.id}.{post.file.ext}", "wb") as f:
        f.write(requests.get(post.file.url).content)

Getting

Many entities that have unique identifiers (such as post_id or username) support indexing using these ids:

post = api.posts.get(3291457)
posts = api.posts.get([3291457, 3069995])
pool = api.pools.get(28232)
user = api.users.get("fox")

Updating

api.posts.update(3291457, tag_string_diff="canine -male", description="Rick roll?")

Creating

from pathlib import Path

api.posts.create(
    tag_string="canine 3d rick_roll",
    file=Path("path/to/rickroll.webm"),
    rating="s",
    sources=[],
    description="Rick roll?"
)

FAQ

  • For more information on these and other api endpoints, please, visit our endpoint reference

e621-py's People

Contributors

eoan-ermine avatar hmiku8338 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

e621-py's Issues

Add blacklist matching by name alias

Currently, blacklist is applied to posts in a naive way -- assuming the user inputted all the tags correctly. However, it does not take the tag aliases into account. The reason we haven't implemented it with name aliases to begin with is speed: e621 does not give us a fast way to query a multitude of name aliases (for example, with a single request). So if a blacklist contains 15 tags, it will require us to do 15 web requests. So even if we cache the correct blacklist, it will still take quite a bit of time to filter any number of initial posts.

So the request is simple: Implement some fancy way of doing it quickly :)

Import errors `NameError: name 'autogenerated' is not defined`

Just after running pip3 install e621 and then starting python, trying to import e621 (>>> import e621) errors out with the following error:

File "/home/username/.local/lib/python3.10/site-packages/e621/models.py", line 14, in <module>
    class Field720p(autogenerated.Field720p):
NameError: name 'autogenerated' is not defined

I attempted to try and fix the issue myself by changing line 8 in e621/models.py to

from . import autogenerated_models as autogenerated

which wasn't the simple fix I was hoping for. Fixing the new errors that came from that just starts to lead down a rabbit hole so I figured, before I keep going trying to fix all of this, am I just doing something wrong?

Certain Posts Cause Pydantic Errors

Description

Running the following code will cause a pydantic validation error due to the alternatives:

from e621.api import E621
api = E621()
posts = api.posts.search(tags=["id:536580"])

Stacktrace:

Traceback (most recent call last):
  File "/home/ben/Documents/api/test.py", line 3, in <module>
    posts = api.posts.search(tags=["id:536580"])
  File "/home/ben/Documents/api/venv/lib/python3.10/site-packages/e621/endpoints.py", line 222, in search
    posts = self._default_search({"tags": tags}, limit, page, ignore_pagination)
  File "/home/ben/Documents/api/venv/lib/python3.10/site-packages/e621/endpoints.py", line 121, in _default_search
    return self._model.from_response(self._api.session.get(self._url, params=params), self._api, expect=list)
  File "/home/ben/Documents/api/venv/lib/python3.10/site-packages/e621/base_model.py", line 53, in from_response
    return cls.from_list(json, e621api)
  File "/home/ben/Documents/api/venv/lib/python3.10/site-packages/e621/base_model.py", line 23, in from_list
    return [cls(**obj, e621api=api) for obj in list]
  File "/home/ben/Documents/api/venv/lib/python3.10/site-packages/e621/base_model.py", line 23, in <listcomp>
    return [cls(**obj, e621api=api) for obj in list]
  File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 4 validation errors for Post
sample -> alternates -> 720p -> urls -> 0
  none is not an allowed value (type=type_error.none.not_allowed)
sample -> alternates -> 720p -> urls -> 1
  none is not an allowed value (type=type_error.none.not_allowed)
sample -> alternates -> 480p -> urls -> 0
  none is not an allowed value (type=type_error.none.not_allowed)
sample -> alternates -> 480p -> urls -> 1
  none is not an allowed value (type=type_error.none.not_allowed)

Specs

OS: Ubuntu 20.04
e621: 0.0.6
python: 3.10.4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.