e621 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +140 -0
- data/lib/e621/client.rb +102 -0
- data/lib/e621/constants.rb +12 -0
- data/lib/e621/error.rb +7 -0
- data/lib/e621/models/api_key.rb +10 -0
- data/lib/e621/models/appeal.rb +25 -0
- data/lib/e621/models/artist.rb +25 -0
- data/lib/e621/models/artist_url.rb +10 -0
- data/lib/e621/models/artist_version.rb +10 -0
- data/lib/e621/models/avoid_posting.rb +30 -0
- data/lib/e621/models/avoid_posting_version.rb +10 -0
- data/lib/e621/models/ban.rb +10 -0
- data/lib/e621/models/base.rb +88 -0
- data/lib/e621/models/basic_post.rb +10 -0
- data/lib/e621/models/blip.rb +35 -0
- data/lib/e621/models/bulk_update_request.rb +25 -0
- data/lib/e621/models/comment.rb +45 -0
- data/lib/e621/models/comment_vote.rb +25 -0
- data/lib/e621/models/current_user.rb +25 -0
- data/lib/e621/models/db_export.rb +10 -0
- data/lib/e621/models/dmail.rb +30 -0
- data/lib/e621/models/edit_history.rb +10 -0
- data/lib/e621/models/email_blacklist.rb +15 -0
- data/lib/e621/models/exception_log.rb +10 -0
- data/lib/e621/models/extended_post.rb +10 -0
- data/lib/e621/models/forum_post.rb +50 -0
- data/lib/e621/models/forum_post_vote.rb +15 -0
- data/lib/e621/models/forum_topic.rb +40 -0
- data/lib/e621/models/full_current_user.rb +10 -0
- data/lib/e621/models/full_user.rb +10 -0
- data/lib/e621/models/help_page.rb +20 -0
- data/lib/e621/models/ip_ban.rb +15 -0
- data/lib/e621/models/iqdb_post.rb +10 -0
- data/lib/e621/models/mascot.rb +20 -0
- data/lib/e621/models/minimal_user.rb +10 -0
- data/lib/e621/models/mod_action.rb +15 -0
- data/lib/e621/models/news_update.rb +20 -0
- data/lib/e621/models/note.rb +25 -0
- data/lib/e621/models/note_version.rb +15 -0
- data/lib/e621/models/pool.rb +35 -0
- data/lib/e621/models/pool_version.rb +15 -0
- data/lib/e621/models/post.rb +10 -0
- data/lib/e621/models/post_actions.rb +143 -0
- data/lib/e621/models/post_approval.rb +15 -0
- data/lib/e621/models/post_deletion.rb +10 -0
- data/lib/e621/models/post_disapproval.rb +10 -0
- data/lib/e621/models/post_event.rb +10 -0
- data/lib/e621/models/post_flag.rb +20 -0
- data/lib/e621/models/post_replacement.rb +35 -0
- data/lib/e621/models/post_set.rb +35 -0
- data/lib/e621/models/post_version.rb +30 -0
- data/lib/e621/models/post_vote.rb +25 -0
- data/lib/e621/models/rising_search_trend.rb +10 -0
- data/lib/e621/models/saved_search.rb +20 -0
- data/lib/e621/models/search_trend.rb +10 -0
- data/lib/e621/models/search_trend_blacklist.rb +20 -0
- data/lib/e621/models/search_trend_hourly.rb +10 -0
- data/lib/e621/models/staff_file.rb +20 -0
- data/lib/e621/models/staff_note.rb +25 -0
- data/lib/e621/models/staff_wiki.rb +30 -0
- data/lib/e621/models/staff_wiki_version.rb +10 -0
- data/lib/e621/models/tag.rb +30 -0
- data/lib/e621/models/tag_alias.rb +25 -0
- data/lib/e621/models/tag_correction.rb +15 -0
- data/lib/e621/models/tag_implication.rb +25 -0
- data/lib/e621/models/tag_preview.rb +10 -0
- data/lib/e621/models/tag_subscription.rb +15 -0
- data/lib/e621/models/tag_type_version.rb +10 -0
- data/lib/e621/models/takedown.rb +45 -0
- data/lib/e621/models/thumbnail_post.rb +10 -0
- data/lib/e621/models/ticket.rb +25 -0
- data/lib/e621/models/upload.rb +10 -0
- data/lib/e621/models/upload_karma_event.rb +10 -0
- data/lib/e621/models/upload_url_rule.rb +10 -0
- data/lib/e621/models/upload_whitelist.rb +20 -0
- data/lib/e621/models/user.rb +35 -0
- data/lib/e621/models/user_alt.rb +10 -0
- data/lib/e621/models/user_feedback.rb +30 -0
- data/lib/e621/models/user_name_change_request.rb +15 -0
- data/lib/e621/models/wiki_page.rb +25 -0
- data/lib/e621/models/wiki_page_version.rb +15 -0
- data/lib/e621/modules/appeals.rb +36 -0
- data/lib/e621/modules/artist_urls.rb +13 -0
- data/lib/e621/modules/artist_versions.rb +13 -0
- data/lib/e621/modules/artists.rb +51 -0
- data/lib/e621/modules/avoid_posting_versions.rb +13 -0
- data/lib/e621/modules/avoid_postings.rb +50 -0
- data/lib/e621/modules/bans.rb +20 -0
- data/lib/e621/modules/base.rb +73 -0
- data/lib/e621/modules/blips.rb +52 -0
- data/lib/e621/modules/bulk_update_requests.rb +43 -0
- data/lib/e621/modules/comment_votes.rb +36 -0
- data/lib/e621/modules/comments.rb +57 -0
- data/lib/e621/modules/db_exports.rb +13 -0
- data/lib/e621/modules/dmails.rb +44 -0
- data/lib/e621/modules/doorkeeper_openid_connect_discovery.rb +18 -0
- data/lib/e621/modules/doorkeeper_openid_connect_userinfo.rb +13 -0
- data/lib/e621/modules/doorkeeper_token_info.rb +13 -0
- data/lib/e621/modules/doorkeeper_tokens.rb +23 -0
- data/lib/e621/modules/dtext.rb +13 -0
- data/lib/e621/modules/edit_histories.rb +20 -0
- data/lib/e621/modules/email_blacklists.rb +24 -0
- data/lib/e621/modules/favorites.rb +25 -0
- data/lib/e621/modules/forum_post_votes.rb +24 -0
- data/lib/e621/modules/forum_posts.rb +52 -0
- data/lib/e621/modules/forum_topics.rb +64 -0
- data/lib/e621/modules/health.rb +13 -0
- data/lib/e621/modules/help_pages.rb +42 -0
- data/lib/e621/modules/ip_bans.rb +24 -0
- data/lib/e621/modules/iqdb_queries.rb +32 -0
- data/lib/e621/modules/mascots.rb +30 -0
- data/lib/e621/modules/mod_actions.rb +20 -0
- data/lib/e621/modules/news_updates.rb +30 -0
- data/lib/e621/modules/note_versions.rb +13 -0
- data/lib/e621/modules/notes.rb +43 -0
- data/lib/e621/modules/pool_versions.rb +13 -0
- data/lib/e621/modules/pools.rb +59 -0
- data/lib/e621/modules/popular.rb +15 -0
- data/lib/e621/modules/post_approvals.rb +26 -0
- data/lib/e621/modules/post_deletions.rb +20 -0
- data/lib/e621/modules/post_disapprovals.rb +18 -0
- data/lib/e621/modules/post_events.rb +14 -0
- data/lib/e621/modules/post_flags.rb +35 -0
- data/lib/e621/modules/post_replacements.rb +54 -0
- data/lib/e621/modules/post_sets.rb +63 -0
- data/lib/e621/modules/post_versions.rb +29 -0
- data/lib/e621/modules/post_votes.rb +36 -0
- data/lib/e621/modules/posts.rb +145 -0
- data/lib/e621/modules/rails_health.rb +13 -0
- data/lib/e621/modules/related_tags.rb +13 -0
- data/lib/e621/modules/saved_searches.rb +47 -0
- data/lib/e621/modules/search_trend_blacklists.rb +30 -0
- data/lib/e621/modules/search_trend_hourlies.rb +13 -0
- data/lib/e621/modules/search_trends.rb +38 -0
- data/lib/e621/modules/sessions.rb +23 -0
- data/lib/e621/modules/staff_automod_dmails.rb +32 -0
- data/lib/e621/modules/staff_dmails.rb +20 -0
- data/lib/e621/modules/staff_exception_logs.rb +18 -0
- data/lib/e621/modules/staff_files.rb +35 -0
- data/lib/e621/modules/staff_ip_addrs.rb +18 -0
- data/lib/e621/modules/staff_notes.rb +40 -0
- data/lib/e621/modules/staff_reowner.rb +13 -0
- data/lib/e621/modules/staff_stuck_dnp.rb +13 -0
- data/lib/e621/modules/staff_user_alts.rb +13 -0
- data/lib/e621/modules/staff_user_cleanups.rb +33 -0
- data/lib/e621/modules/staff_users.rb +13 -0
- data/lib/e621/modules/staff_vote_trends.rb +15 -0
- data/lib/e621/modules/staff_wiki_versions.rb +18 -0
- data/lib/e621/modules/staff_wikis.rb +51 -0
- data/lib/e621/modules/stats.rb +13 -0
- data/lib/e621/modules/tag_aliases.rb +49 -0
- data/lib/e621/modules/tag_corrections.rb +18 -0
- data/lib/e621/modules/tag_implications.rb +49 -0
- data/lib/e621/modules/tag_subscriptions.rb +26 -0
- data/lib/e621/modules/tag_type_versions.rb +13 -0
- data/lib/e621/modules/tags.rb +37 -0
- data/lib/e621/modules/takedowns.rb +63 -0
- data/lib/e621/modules/tickets.rb +36 -0
- data/lib/e621/modules/upload_karma_events.rb +13 -0
- data/lib/e621/modules/upload_url_rules.rb +18 -0
- data/lib/e621/modules/upload_whitelists.rb +35 -0
- data/lib/e621/modules/uploads.rb +45 -0
- data/lib/e621/modules/user_feedbacks.rb +49 -0
- data/lib/e621/modules/user_name_change_requests.rb +30 -0
- data/lib/e621/modules/users.rb +80 -0
- data/lib/e621/modules/wiki_page_versions.rb +20 -0
- data/lib/e621/modules/wiki_pages.rb +43 -0
- data/lib/e621/oauth_callback_error.rb +24 -0
- data/lib/e621/oauth_client.rb +225 -0
- data/lib/e621/options.rb +33 -0
- data/lib/e621/post_format.rb +12 -0
- data/lib/e621/ratelimit_error.rb +32 -0
- data/lib/e621/ratelimit_info.rb +15 -0
- data/lib/e621/ratelimit_options.rb +16 -0
- data/lib/e621/unexpected_response_error.rb +22 -0
- data/lib/e621/version.rb +10 -0
- data/lib/e621.rb +396 -0
- data/rbi/e621/attributes.rbi +3011 -0
- metadata +273 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7cc63cf06ca005a347b2ceb99a1c07ecb91f0832dad91736e45fa67bb082719a
|
|
4
|
+
data.tar.gz: fc4fc7511a6d7a002e91e9ec2d34373ca484dcb1b8cc6a8c82ea0cc917776e18
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d6125292927867829e0d8509105f1c5ef9020fb769f3e1c973fccd5a3123b1fb004dee033f8651b1a15d38c0887ccc3e6d888dffce131962c5fa8d68e9489b92
|
|
7
|
+
data.tar.gz: 531fe30b3c959311ce9f374b60221cb74a746c8dab61c808e0d0f913b4b2175896220aceb76b57630e48f8152835fce4cd886f3d6d6821456dc42b98bd22c7d2
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Donovan Daniels
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# E621.rb
|
|
2
|
+
|
|
3
|
+
The most comprehensive wrapper for e621 you'll ever find, for Ruby.
|
|
4
|
+
|
|
5
|
+
We have as close to 100% coverage of the API as possible. This is the Ruby version of [E621](https://github.com/DonovanDMC/E621), with the same modules, models, and convenience methods.
|
|
6
|
+
|
|
7
|
+
### This module is not officially supported or endorsed by any of the e621 staff. Do not go to them if something does not work.
|
|
8
|
+
|
|
9
|
+
Requests are made through [e621_openapi](https://rubygems.org/gems/e621_openapi), which is generated from the [OpenAPI Spec](https://e621.wiki). If a parameter is missing, a path is wrong, a route is missing, etc., then it should be reported to the [spec's repository](https://github.com/DonovanDMC/E621OpenAPI/issues) and not here.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Ruby **3.4.1** or higher is required.
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
bundle add e621
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
This gem is structured with modules corresponding to separate areas of the site. All of them are on the main class:
|
|
22
|
+
```ruby
|
|
23
|
+
require("e621")
|
|
24
|
+
|
|
25
|
+
# no options are required, see E621::Options for all of them
|
|
26
|
+
e621 = E621.new
|
|
27
|
+
|
|
28
|
+
e621 = E621.new(
|
|
29
|
+
auth_user: "",
|
|
30
|
+
auth_key: "",
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# all of the available modules stem from the main class,
|
|
34
|
+
# e.g. e621.posts.METHOD, e621.users.METHOD
|
|
35
|
+
posts = e621.posts.search(tags: "fox rating:s", limit: 10)
|
|
36
|
+
post = e621.posts.get(123) # nil if it doesn't exist
|
|
37
|
+
post&.favorite
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Options are keyword arguments. Search options are passed flat, the same as e621's own `search[...]` parameters without the prefix, so `search[name_matches]` is `name_matches:`. The same goes for create and update options, e.g. `artist[name]` is `name:`.
|
|
41
|
+
```ruby
|
|
42
|
+
e621.artists.search(name: "zackary911", limit: 5)
|
|
43
|
+
e621.artists.create(name: "fox", other_names: ["vulpine"])
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Returned records are models, with every attribute of the response as a method (`post.id`, `artist.name`), `[]` and `to_h` for the raw data, and convenience methods that call back into the modules, like `post.update(tag_string_diff: "fox")` or `artist.revert(version_id)`.
|
|
47
|
+
|
|
48
|
+
Responses that aren't successful raise `E621::UnexpectedResponseError` (a subclass of `E621OpenAPI::Error`), with the `status`, the parsed `body`, and the `request` and `response`. Lookups by ID or name return `nil` on 404 instead.
|
|
49
|
+
|
|
50
|
+
### Bearer Tokens & OAuth
|
|
51
|
+
|
|
52
|
+
Instead of `auth_user`/`auth_key`, you can authenticate with a bearer token (e.g. an OAuth access token). It can be a string, or a proc or method that returns one, which is called for every request. If both are given, the bearer token wins.
|
|
53
|
+
```ruby
|
|
54
|
+
e621 = E621.new(auth_token: "...")
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`E621::OAuthClient` handles the OAuth 2.0 authorization code flow (with PKCE) for you, and keeps the access token refreshed. **OAuth has not been publicly released on e621 yet.**
|
|
58
|
+
```ruby
|
|
59
|
+
oauth = E621::OAuthClient.new(
|
|
60
|
+
client_id: "",
|
|
61
|
+
client_secret: "", # leave out for public clients
|
|
62
|
+
redirect_uri: "https://example.com/callback",
|
|
63
|
+
scopes: %w[openid full],
|
|
64
|
+
token: saved_token, # optional, restore a previous token
|
|
65
|
+
on_token: ->(token) { save_token(token) }, # called when the token is exchanged, refreshed, or revoked
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# keep state & code_verifier (e.g. in the session), and send the user to url
|
|
69
|
+
request = oauth.create_authorization_url
|
|
70
|
+
|
|
71
|
+
# once they're redirected back, this checks the state and exchanges the code
|
|
72
|
+
oauth.handle_callback(callback_url, state: request.state, code_verifier: request.code_verifier)
|
|
73
|
+
|
|
74
|
+
# access_token refreshes the token when it's about to expire
|
|
75
|
+
e621 = E621.new(auth_token: oauth.method(:access_token))
|
|
76
|
+
|
|
77
|
+
oauth.user_info
|
|
78
|
+
oauth.revoke
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Default Post Format
|
|
82
|
+
|
|
83
|
+
Post-returning methods (`posts.get`, `posts.search`, `favorites.search`, `popular.get`, `iqdb.get`/`.post`, and convenience methods on returned posts like `post.update`) accept `v1:`/`mode:` and default to the basic format when they're left out (`v1: true` returns the deprecated legacy format). Set `default_post_format` once to change that default everywhere:
|
|
84
|
+
```ruby
|
|
85
|
+
e621 = E621.new(default_post_format: { mode: "extended" })
|
|
86
|
+
|
|
87
|
+
post = e621.posts.get(123) # E621::Models::ExtendedPost
|
|
88
|
+
post&.update(tag_string_diff: "fox") # also an ExtendedPost
|
|
89
|
+
|
|
90
|
+
# a per-call option still overrides the default for that one call
|
|
91
|
+
legacy = e621.posts.get(123, v1: true) # E621::Models::Post
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Ratelimit Handling
|
|
95
|
+
|
|
96
|
+
Every request that comes back `429 Too Many Requests` is retried automatically, waiting for the duration in the response's `Retry-After` header (or an exponential backoff starting at `retry_delay` seconds if that header is missing). If it's still ratelimited after `max_retries` attempts, or `retry` is turned off, an `E621::RatelimitError` (see `retry_after`) is raised instead:
|
|
97
|
+
```ruby
|
|
98
|
+
e621 = E621.new(
|
|
99
|
+
ratelimit: {
|
|
100
|
+
max_retries: 5, # default: 3
|
|
101
|
+
retry_delay: 2, # default: 1 (seconds, doubled per attempt as a fallback)
|
|
102
|
+
max_retry_delay: 30, # default: 60 (seconds, caps both the header and the fallback)
|
|
103
|
+
on_ratelimit: ->(info) { warn("ratelimited, retrying in #{info.retry_after}s") },
|
|
104
|
+
},
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
begin
|
|
108
|
+
e621.posts.get(123)
|
|
109
|
+
rescue E621::RatelimitError => e
|
|
110
|
+
warn("still ratelimited, retry after #{e.retry_after}s")
|
|
111
|
+
end
|
|
112
|
+
```
|
|
113
|
+
Set `ratelimit: { retry: false }` to disable automatic retries and raise `E621::RatelimitError` on the first `429`.
|
|
114
|
+
|
|
115
|
+
### Logging
|
|
116
|
+
|
|
117
|
+
Pass a `Logger` as `logger:` to log every request and response at debug level.
|
|
118
|
+
|
|
119
|
+
## Types
|
|
120
|
+
|
|
121
|
+
The gem is typed with [Sorbet](https://sorbet.org). Model attributes are typed from the spec's schemas in `rbi/e621/attributes.rbi`, which [Tapioca](https://github.com/Shopify/tapioca) picks up automatically when it generates the gem's RBI.
|
|
122
|
+
|
|
123
|
+
## Raw Client
|
|
124
|
+
|
|
125
|
+
The request methods this gem is built on come from [e621_openapi](https://rubygems.org/gems/e621_openapi). If you only need the raw requests, without models, convenience methods, or ratelimit handling, you can use it on its own, or through `e621.client`, which has a method for every operation:
|
|
126
|
+
```ruby
|
|
127
|
+
e621.client.posts_index(tags: "fox", limit: 10) # an array of hashes
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Development
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
bundle exec rake # rubocop, zeitwerk, tests, and sorbet
|
|
134
|
+
bundle exec rake generate # regenerate rbi/e621/attributes.rbi after updating e621_openapi
|
|
135
|
+
bin/tapioca gems # regenerate the gem RBIs after updating dependencies
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Tests live under `test/` and run on minitest, with requests stubbed by WebMock. `test/operations_test.rb` checks that every operation the modules call, and every schema the models use, still exists in the installed e621_openapi.
|
|
139
|
+
|
|
140
|
+
Releases are published to RubyGems by `.github/workflows/publish.yml` when a GitHub release is published, through [trusted publishing](https://guides.rubygems.org/trusted-publishing/).
|
data/lib/e621/client.rb
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# typed: strict
|
|
3
|
+
|
|
4
|
+
require("net/http")
|
|
5
|
+
|
|
6
|
+
class E621
|
|
7
|
+
# The e621_openapi client, with bearer tokens, ratelimit retries, logging, and the Location of redirects.
|
|
8
|
+
class Client < E621OpenAPI::Client
|
|
9
|
+
extend(T::Sig)
|
|
10
|
+
|
|
11
|
+
sig { returns(Options) }
|
|
12
|
+
attr_reader(:options)
|
|
13
|
+
|
|
14
|
+
sig { params(options: Options).void }
|
|
15
|
+
def initialize(options)
|
|
16
|
+
@options = options
|
|
17
|
+
super(
|
|
18
|
+
user_agent: options.resolved_user_agent,
|
|
19
|
+
base_url: options.base_url,
|
|
20
|
+
open_timeout: options.request_timeout,
|
|
21
|
+
read_timeout: options.request_timeout,
|
|
22
|
+
)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
sig { returns(T.nilable(String)) }
|
|
26
|
+
def authorization
|
|
27
|
+
token = options.auth_token
|
|
28
|
+
token = token.call if token.is_a?(Proc) || token.is_a?(Method)
|
|
29
|
+
return "Bearer #{token}" if token && !token.empty?
|
|
30
|
+
user = options.auth_user
|
|
31
|
+
key = options.auth_key
|
|
32
|
+
"Basic #{["#{user}:#{key}"].pack('m0')}" if user && key
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
sig { returns(String) }
|
|
36
|
+
def inspect
|
|
37
|
+
"#<#{self.class} base_url=#{base_url} auth_user=#{options.auth_user.inspect}>"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
# replaces E621OpenAPI::Client#request, which has no way to add a bearer token or retry
|
|
43
|
+
sig { params(method: Symbol, path: String, query: T.nilable(T::Hash[Symbol, T.untyped]), body: T.nilable(T::Hash[Symbol, T.untyped]), body_type: T.nilable(String)).returns(T.untyped) }
|
|
44
|
+
def request(method, path, query: nil, body: nil, body_type: nil)
|
|
45
|
+
uri = base_url.dup
|
|
46
|
+
uri.path = path
|
|
47
|
+
pairs = flatten(query)
|
|
48
|
+
uri.query = URI.encode_www_form(pairs) unless pairs.empty?
|
|
49
|
+
|
|
50
|
+
req = REQUESTS.fetch(method).new(uri)
|
|
51
|
+
req["User-Agent"] = user_agent
|
|
52
|
+
req["Accept"] = "application/json"
|
|
53
|
+
# encoded once, files can't be read again for a retry
|
|
54
|
+
encode_body(req, body, body_type) if body_type
|
|
55
|
+
|
|
56
|
+
ratelimit = options.ratelimit
|
|
57
|
+
attempt = 0
|
|
58
|
+
loop do
|
|
59
|
+
# per attempt, so a refreshed token is picked up
|
|
60
|
+
authorization = self.authorization
|
|
61
|
+
authorization ? req["Authorization"] = authorization : req.delete("Authorization")
|
|
62
|
+
log("-> #{req.method} #{uri}")
|
|
63
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: options.request_timeout, read_timeout: options.request_timeout) do |http|
|
|
64
|
+
http.request(req)
|
|
65
|
+
end
|
|
66
|
+
log("<- #{req.method} #{uri} #{response.code} #{response.message}")
|
|
67
|
+
return handle(req, response) unless response.is_a?(Net::HTTPTooManyRequests)
|
|
68
|
+
|
|
69
|
+
retry_after = RatelimitError.parse_retry_after(response)
|
|
70
|
+
will_retry = ratelimit.retry && attempt < ratelimit.max_retries
|
|
71
|
+
ratelimit.on_ratelimit&.call(RatelimitInfo.new(
|
|
72
|
+
attempt: attempt + 1,
|
|
73
|
+
max_retries: ratelimit.max_retries,
|
|
74
|
+
request: req,
|
|
75
|
+
response: response,
|
|
76
|
+
retry_after: retry_after,
|
|
77
|
+
will_retry: will_retry,
|
|
78
|
+
))
|
|
79
|
+
raise(RatelimitError.new(req, response, parse(response))) unless will_retry
|
|
80
|
+
|
|
81
|
+
delay = [retry_after || (ratelimit.retry_delay * (2**attempt)), ratelimit.max_retry_delay].min
|
|
82
|
+
log("<- #{req.method} #{uri} ratelimited, retrying in #{delay}s (attempt #{attempt + 1}/#{ratelimit.max_retries})")
|
|
83
|
+
sleep(delay)
|
|
84
|
+
attempt += 1
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
sig { params(req: Net::HTTPRequest, response: Net::HTTPResponse).returns(T.untyped) }
|
|
89
|
+
def handle(req, response)
|
|
90
|
+
case response
|
|
91
|
+
when Net::HTTPSuccess then parse(response)
|
|
92
|
+
when Net::HTTPRedirection then response["Location"]
|
|
93
|
+
else raise(UnexpectedResponseError.new(req, response, parse(response)))
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
sig { params(message: String).void }
|
|
98
|
+
def log(message)
|
|
99
|
+
options.logger&.debug("E621") { message }
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# typed: strict
|
|
3
|
+
|
|
4
|
+
require("sorbet-runtime")
|
|
5
|
+
require_relative("version")
|
|
6
|
+
|
|
7
|
+
class E621
|
|
8
|
+
module Constants
|
|
9
|
+
DEFAULT_BASE_URL = "https://e621.net"
|
|
10
|
+
USER_AGENT = T.let("E621.rb/#{VERSION} (#{WEBSITE})".freeze, String)
|
|
11
|
+
end
|
|
12
|
+
end
|
data/lib/e621/error.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# typed: strict
|
|
3
|
+
|
|
4
|
+
class E621
|
|
5
|
+
module Models
|
|
6
|
+
class Appeal < Base
|
|
7
|
+
schema("Appeal")
|
|
8
|
+
|
|
9
|
+
sig { returns(Models::Appeal) }
|
|
10
|
+
def claim
|
|
11
|
+
e621.appeals.claim(id)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
sig { returns(Models::Appeal) }
|
|
15
|
+
def unclaim
|
|
16
|
+
e621.appeals.unclaim(id)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
sig { params(options: T.untyped).returns(NilClass) }
|
|
20
|
+
def update(**options)
|
|
21
|
+
e621.appeals.update(id, **options)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# typed: strict
|
|
3
|
+
|
|
4
|
+
class E621
|
|
5
|
+
module Models
|
|
6
|
+
class Artist < Base
|
|
7
|
+
schema("Artist")
|
|
8
|
+
|
|
9
|
+
sig { returns(NilClass) }
|
|
10
|
+
def delete
|
|
11
|
+
e621.artists.delete(id)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
sig { params(version_id: Integer).returns(NilClass) }
|
|
15
|
+
def revert(version_id)
|
|
16
|
+
e621.artists.revert(id, version_id)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
sig { params(options: T.untyped).returns(NilClass) }
|
|
20
|
+
def update(**options)
|
|
21
|
+
e621.artists.update(id, **options)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# typed: strict
|
|
3
|
+
|
|
4
|
+
class E621
|
|
5
|
+
module Models
|
|
6
|
+
class AvoidPosting < Base
|
|
7
|
+
schema("AvoidPosting")
|
|
8
|
+
|
|
9
|
+
sig { returns(NilClass) }
|
|
10
|
+
def delete
|
|
11
|
+
e621.avoid_postings.delete(id)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
sig { returns(NilClass) }
|
|
15
|
+
def destroy
|
|
16
|
+
e621.avoid_postings.destroy(id)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
sig { returns(NilClass) }
|
|
20
|
+
def undelete
|
|
21
|
+
e621.avoid_postings.undelete(id)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
sig { params(options: T.untyped).returns(NilClass) }
|
|
25
|
+
def update(**options)
|
|
26
|
+
e621.avoid_postings.update(id, **options)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# typed: strict
|
|
3
|
+
|
|
4
|
+
require("json")
|
|
5
|
+
|
|
6
|
+
class E621
|
|
7
|
+
module Models
|
|
8
|
+
# Attributes are read from the response, and typed in rbi/e621/attributes.rbi from the model's schema.
|
|
9
|
+
class Base
|
|
10
|
+
extend(T::Sig)
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
extend(T::Sig)
|
|
14
|
+
|
|
15
|
+
# the e621_openapi schema the model's attributes come from
|
|
16
|
+
sig { params(name: T.nilable(String)).returns(T.nilable(String)) }
|
|
17
|
+
def schema(name = nil)
|
|
18
|
+
@schema = T.let(name, T.nilable(String)) if name
|
|
19
|
+
parent = superclass
|
|
20
|
+
@schema || (parent && parent < Base ? T.unsafe(parent).schema : nil)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
sig { returns(E621) }
|
|
25
|
+
attr_reader(:e621)
|
|
26
|
+
|
|
27
|
+
sig { returns(T::Hash[Symbol, T.untyped]) }
|
|
28
|
+
attr_reader(:data)
|
|
29
|
+
|
|
30
|
+
sig { params(e621: E621, data: T::Hash[Symbol, T.untyped]).void }
|
|
31
|
+
def initialize(e621, data)
|
|
32
|
+
@e621 = e621
|
|
33
|
+
@data = data
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
sig { params(key: T.any(Symbol, String)).returns(T.untyped) }
|
|
37
|
+
def [](key)
|
|
38
|
+
data[key.to_sym]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
sig { params(key: T.any(Symbol, String)).returns(T::Boolean) }
|
|
42
|
+
def key?(key)
|
|
43
|
+
data.key?(key.to_sym)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
sig { returns(T::Hash[Symbol, T.untyped]) }
|
|
47
|
+
def to_h
|
|
48
|
+
data
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
sig { params(args: T.untyped).returns(String) }
|
|
52
|
+
def to_json(*args)
|
|
53
|
+
T.unsafe(data).to_json(*args)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
sig { params(other: T.untyped).returns(T::Boolean) }
|
|
57
|
+
def ==(other)
|
|
58
|
+
return false unless other.is_a?(Base) && other.instance_of?(self.class)
|
|
59
|
+
other.data == data
|
|
60
|
+
end
|
|
61
|
+
alias eql? ==
|
|
62
|
+
|
|
63
|
+
sig { returns(Integer) }
|
|
64
|
+
def hash
|
|
65
|
+
[self.class, data].hash
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
sig { returns(String) }
|
|
69
|
+
def inspect
|
|
70
|
+
"#<#{self.class} #{data.map { |key, value| "#{key}=#{value.inspect}" }.join(' ')}>"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
sig { params(name: Symbol, args: T.untyped, block: T.untyped).returns(T.untyped) }
|
|
76
|
+
def method_missing(name, *args, &block)
|
|
77
|
+
return data[name] if args.empty? && block.nil? && data.key?(name)
|
|
78
|
+
super
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
sig { params(name: Symbol, include_private: T::Boolean).returns(T::Boolean) }
|
|
82
|
+
def respond_to_missing?(name, include_private = false)
|
|
83
|
+
return true if data.key?(name)
|
|
84
|
+
super ? true : false
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# typed: strict
|
|
3
|
+
|
|
4
|
+
class E621
|
|
5
|
+
module Models
|
|
6
|
+
class Blip < Base
|
|
7
|
+
schema("Blip")
|
|
8
|
+
|
|
9
|
+
sig { returns(String) }
|
|
10
|
+
def delete
|
|
11
|
+
e621.blips.delete(id)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
sig { returns(NilClass) }
|
|
15
|
+
def destroy
|
|
16
|
+
e621.blips.destroy(id)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
sig { params(type: T.untyped).returns(T.untyped) }
|
|
20
|
+
def mark(type)
|
|
21
|
+
e621.blips.mark(id, type)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
sig { returns(String) }
|
|
25
|
+
def undelete
|
|
26
|
+
e621.blips.undelete(id)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
sig { params(options: T.untyped).returns(NilClass) }
|
|
30
|
+
def update(**options)
|
|
31
|
+
e621.blips.update(id, **options)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# typed: strict
|
|
3
|
+
|
|
4
|
+
class E621
|
|
5
|
+
module Models
|
|
6
|
+
class BulkUpdateRequest < Base
|
|
7
|
+
schema("BulkUpdateRequest")
|
|
8
|
+
|
|
9
|
+
sig { returns(NilClass) }
|
|
10
|
+
def approve
|
|
11
|
+
e621.bulk_update_requests.approve(id)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
sig { returns(NilClass) }
|
|
15
|
+
def reject
|
|
16
|
+
e621.bulk_update_requests.reject(id)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
sig { params(options: T.untyped).returns(NilClass) }
|
|
20
|
+
def update(**options)
|
|
21
|
+
e621.bulk_update_requests.update(id, **options)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# typed: strict
|
|
3
|
+
|
|
4
|
+
class E621
|
|
5
|
+
module Models
|
|
6
|
+
class Comment < Base
|
|
7
|
+
schema("Comment")
|
|
8
|
+
|
|
9
|
+
sig { params(score: T.untyped, no_unvote: T.nilable(T::Boolean)).returns(T.untyped) }
|
|
10
|
+
def create_vote(score, no_unvote = nil)
|
|
11
|
+
e621.comment_votes.create(id, score, no_unvote)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
sig { returns(NilClass) }
|
|
15
|
+
def delete
|
|
16
|
+
e621.comments.delete(id)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
sig { returns(Models::Comment) }
|
|
20
|
+
def hide
|
|
21
|
+
e621.comments.hide(id)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
sig { params(type: T.untyped).returns(T.untyped) }
|
|
25
|
+
def mark(type)
|
|
26
|
+
e621.comments.mark(id, type)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
sig { returns(Models::Comment) }
|
|
30
|
+
def unhide
|
|
31
|
+
e621.comments.unhide(id)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
sig { returns(NilClass) }
|
|
35
|
+
def unvote
|
|
36
|
+
e621.comment_votes.unvote(id)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
sig { params(options: T.untyped).returns(NilClass) }
|
|
40
|
+
def update(**options)
|
|
41
|
+
e621.comments.update(id, **options)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# typed: strict
|
|
3
|
+
|
|
4
|
+
class E621
|
|
5
|
+
module Models
|
|
6
|
+
class CommentVote < Base
|
|
7
|
+
schema("CommentVote")
|
|
8
|
+
|
|
9
|
+
sig { returns(NilClass) }
|
|
10
|
+
def delete
|
|
11
|
+
e621.comment_votes.unvote(comment_id)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
sig { returns(NilClass) }
|
|
15
|
+
def lock
|
|
16
|
+
e621.comment_votes.lock([id])
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
sig { returns(NilClass) }
|
|
20
|
+
def staff_delete
|
|
21
|
+
e621.comment_votes.delete([id])
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|