e621stream 0.0.5 → 0.1.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 +4 -4
- data/README.md +14 -9
- data/lib/e621stream/client/options.rb +0 -6
- data/lib/e621stream/client.rb +29 -26
- data/lib/e621stream/close_codes.rb +5 -3
- data/lib/e621stream/event.rb +8 -5
- data/lib/e621stream/events.rb +22 -15
- data/lib/e621stream/stream_error.rb +0 -1
- data/lib/e621stream/version.rb +1 -1
- data/lib/e621stream.rb +0 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 79a697f56acf9e01824147dd6b02d6d895945b1eaba99c769073c8df09c967d1
|
|
4
|
+
data.tar.gz: de0b37eb1a37fc5a7c54d48dcee613ddfac51b0068b771479ebe3fc2077c252c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: db78caee15dc50614bf21bb73e2e65f5bd8d500a2f5eeb5ab1113767c0ef35fb622d57edac84c28289cbc295d8c751f098c4dcd6c9d2e9b78ab54d25a4f14ae5
|
|
7
|
+
data.tar.gz: 8a21d51542f7b11cec640c98b86bc89a2537ca18f9d43a370736923d39791a1c91008c8714fd6f61bde1c7a5d2ff0749b9852f5bc2b58387c255a3ec0d534d9b
|
data/README.md
CHANGED
|
@@ -10,31 +10,36 @@ gem install e621stream
|
|
|
10
10
|
require("e621stream")
|
|
11
11
|
|
|
12
12
|
client = E621Stream::Client.new(
|
|
13
|
-
|
|
14
|
-
api_key: "your e621 api key",
|
|
13
|
+
token: "e6s_...",
|
|
15
14
|
subscriptions: ["post.create", { "type" => "post.update", "filter" => { "tags" => "fox -solo" } }, "comment.*"],
|
|
16
15
|
)
|
|
17
16
|
|
|
18
|
-
client.on("post.create") { |event| puts(event.data["
|
|
19
|
-
client.on("post.update") { |event|
|
|
17
|
+
client.on("post.create") { |event| puts(event.data["id"]) }
|
|
18
|
+
client.on("post.update") { |event| puts("#{event.data["id"]} edited by #{event.context&.dig("updater_id")}") }
|
|
20
19
|
client.on(:error) { |error| warn(error.message) }
|
|
21
20
|
|
|
22
21
|
client.run
|
|
23
22
|
```
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
Make a token on [your tokens page](https://e621.stream/user), or pass `username:` & `api_key:` instead.
|
|
26
25
|
|
|
27
26
|
`run` blocks, unless you're already in a reactor, then it runs as a task and `client.close` stops it.
|
|
28
27
|
|
|
29
|
-
`on("post.create")` listens for one type, `on(:dispatch)` for everything. Events have `id`, `type`, `time
|
|
28
|
+
`on("post.create")` listens for one type, `on(:dispatch)` for everything. Events have `id`, `type`, `time`, `data` (the record as the api returns it) and `context` (what the record doesn't say itself, like who made the change, or nil), see [Events](https://e621.stream/events). There's also `:ready`, `:replay_complete`, `:subscribed`, `:recovery`, `:slow_mode`, `:reconnecting`, `:close` and `:error`.
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
If the server missed events and goes back for them (a [recovery](https://e621.stream/replaying#recoveries)), `:recovery` gets the details when it starts and finishes, and the events it sends are `recovered?`. They can be hours old, so don't assume `time` only goes up.
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
The server can poll e621 slower for a while when it's responding slowly. `:slow_mode` gets `enabled`, `scale` and `since` when that's turned on or off, and `ready`'s `slow_mode` has it if it's already on. Events just arrive later than usual, nothing's missed.
|
|
33
|
+
|
|
34
|
+
It reconnects when the connection drops (`reconnect: false` turns that off), and picks up from `client.cursor`. Pass `cursor:` to replay from an event id or ms timestamp. `url:` is the site (`https://e621.stream`), the websocket url comes from its `/info`, or pass a `ws://`/`wss://` url to connect to it directly. `client.subscribe([...])` changes subscriptions. Private events like `dmail.create` have to be subscribed to by name, and need an api key or a token with private events turned on.
|
|
35
|
+
|
|
36
|
+
It connects with the protocol version it was written for (`?v=`). If the server has moved past it, the connection closes with `4014` and it doesn't reconnect, update the gem.
|
|
37
|
+
|
|
38
|
+
`client.snapshots(resource, ids) { |snapshots| ... }` gets what was last seen of up to 1000 records (see [Snapshots](https://e621.stream/snapshots)), by id, nil for ones that aren't known. Each has `"data"` (the record), `"id"`, `"resource"` and `"version"`. Only after `:ready`, and the block is dropped if the connection closes first.
|
|
34
39
|
|
|
35
40
|
```ruby
|
|
36
41
|
client.on(:ready) do
|
|
37
|
-
client.snapshots("post", [6843484, 6843485]) { |posts| p(posts[6843484]&.dig("tags", "general")) }
|
|
42
|
+
client.snapshots("post", [6843484, 6843485]) { |posts| p(posts[6843484]&.dig("data", "tags", "general")) }
|
|
38
43
|
end
|
|
39
44
|
```
|
|
40
45
|
|
|
@@ -5,18 +5,12 @@ module E621Stream
|
|
|
5
5
|
class Client
|
|
6
6
|
class Options < T::Struct
|
|
7
7
|
const(:subscriptions, T::Array[Subscription])
|
|
8
|
-
# Your e621 username and an api key, from Account > Manage API Access.
|
|
9
8
|
const(:username, T.nilable(String), default: nil)
|
|
10
9
|
const(:api_key, T.nilable(String), default: nil)
|
|
11
|
-
# A token from https://e621.stream/authorize, instead of a username & api key.
|
|
12
10
|
const(:token, T.nilable(String), default: nil)
|
|
13
|
-
# The site, the websocket url comes from its /info. A ws:// or wss:// url is connected to directly.
|
|
14
11
|
const(:url, String, default: "https://e621.stream")
|
|
15
|
-
# A name for your client, shown in the server's logs.
|
|
16
12
|
const(:client, T.nilable(String), default: nil)
|
|
17
|
-
# An event id (or a millisecond timestamp) to replay from. After that, reconnecting resumes from the last event.
|
|
18
13
|
const(:cursor, T.nilable(T.any(String, Integer)), default: nil)
|
|
19
|
-
# Reconnects with backoff when the connection drops, unless the server closed it for something retrying won't fix.
|
|
20
14
|
const(:reconnect, T::Boolean, default: true)
|
|
21
15
|
end
|
|
22
16
|
end
|
data/lib/e621stream/client.rb
CHANGED
|
@@ -7,21 +7,14 @@ require("async/http/internet")
|
|
|
7
7
|
require("async/websocket/client")
|
|
8
8
|
|
|
9
9
|
module E621Stream
|
|
10
|
-
# A connection to E621Stream. Listeners run inside the client's reactor, between reading messages.
|
|
11
|
-
#
|
|
12
|
-
# client = E621Stream::Client.new(username: "...", api_key: "...", subscriptions: ["post.create"])
|
|
13
|
-
# client.on("post.create") { |event| puts(event.data["post"]["id"]) }
|
|
14
|
-
# client.run
|
|
15
10
|
class Client
|
|
16
11
|
extend(T::Sig)
|
|
17
12
|
|
|
18
13
|
Listener = T.type_alias { T.proc.params(arg0: T.untyped).void }
|
|
19
14
|
Snapshots = T.type_alias { T::Hash[Integer, T.nilable(T::Hash[String, T.untyped])] }
|
|
20
15
|
SnapshotCallback = T.type_alias { T.proc.params(snapshots: Snapshots).void }
|
|
21
|
-
# An event type, `<resource>.*`, `*`, or `{ "type" => ..., "filter" => { ... } }`.
|
|
22
16
|
Subscription = T.type_alias { T.any(String, T::Hash[T.any(String, Symbol), T.untyped]) }
|
|
23
17
|
|
|
24
|
-
# Tests swap this out.
|
|
25
18
|
class Connector
|
|
26
19
|
extend(T::Sig)
|
|
27
20
|
|
|
@@ -31,7 +24,6 @@ module E621Stream
|
|
|
31
24
|
Async::WebSocket::Client.connect(endpoint, &block)
|
|
32
25
|
end
|
|
33
26
|
|
|
34
|
-
# Gets the websocket url from the site's /info, unless it's one already.
|
|
35
27
|
sig { params(url: String).returns(String) }
|
|
36
28
|
def websocket_url(url)
|
|
37
29
|
return url if url.match?(%r{\Awss?://}i)
|
|
@@ -41,7 +33,7 @@ module E621Stream
|
|
|
41
33
|
response = internet.get(info)
|
|
42
34
|
raise("#{info} returned #{response.status}") unless response.success?
|
|
43
35
|
|
|
44
|
-
websocket = JSON.parse(response.read)["
|
|
36
|
+
websocket = JSON.parse(response.read)["websocket_url"]
|
|
45
37
|
raise("#{info} didn't have a websocket url") unless websocket.is_a?(String)
|
|
46
38
|
|
|
47
39
|
URI.join(url, websocket).to_s
|
|
@@ -58,7 +50,6 @@ module E621Stream
|
|
|
58
50
|
sig { returns(Options) }
|
|
59
51
|
attr_reader(:options)
|
|
60
52
|
|
|
61
|
-
# The newest event received, which reconnecting resumes from.
|
|
62
53
|
sig { returns(T.nilable(String)) }
|
|
63
54
|
attr_reader(:cursor)
|
|
64
55
|
|
|
@@ -87,20 +78,15 @@ module E621Stream
|
|
|
87
78
|
@last_error = T.let(nil, T.nilable(StreamError))
|
|
88
79
|
@ready = T.let(false, T::Boolean)
|
|
89
80
|
@nonce = T.let(0, Integer)
|
|
90
|
-
# the callback and what's been received so far, big requests come back in chunks
|
|
91
81
|
@pending_snapshots = T.let({}, T::Hash[String, [SnapshotCallback, Snapshots]])
|
|
92
82
|
end
|
|
93
83
|
|
|
94
|
-
# Listens for an event type (like "post.create"), or one of :dispatch (every event), :ready, :replay_complete,
|
|
95
|
-
# :subscribed, :error, :close and :reconnecting.
|
|
96
84
|
sig { params(name: T.any(String, Symbol), block: Listener).returns(T.self_type) }
|
|
97
85
|
def on(name, &block)
|
|
98
86
|
T.must(@listeners[name.to_s]) << block
|
|
99
87
|
self
|
|
100
88
|
end
|
|
101
89
|
|
|
102
|
-
# Connects and keeps the connection going until #close is called, or the server closes it for something
|
|
103
|
-
# reconnecting won't fix. Blocks, or runs as a task when called inside a reactor already.
|
|
104
90
|
sig { void }
|
|
105
91
|
def run
|
|
106
92
|
@closing = false
|
|
@@ -123,17 +109,12 @@ module E621Stream
|
|
|
123
109
|
end
|
|
124
110
|
end
|
|
125
111
|
|
|
126
|
-
# Replaces the current subscriptions, and keeps them for reconnecting.
|
|
127
112
|
sig { params(subscriptions: T::Array[Subscription]).void }
|
|
128
113
|
def subscribe(subscriptions)
|
|
129
114
|
@options = Options.new(**options.serialize.transform_keys(&:to_sym), subscriptions: subscriptions)
|
|
130
115
|
send_message({ op: "subscribe", d: { subscriptions: subscriptions } })
|
|
131
116
|
end
|
|
132
117
|
|
|
133
|
-
# Looks up what was last seen of up to 1000 records, see https://e621.stream/snapshots/. The block gets them by id,
|
|
134
|
-
# nil for ones that aren't known. It isn't called if the connection closes first.
|
|
135
|
-
#
|
|
136
|
-
# client.on(:ready) { client.snapshots("post", [1, 2]) { |posts| p(posts[1]&.dig("tags", "general")) } }
|
|
137
118
|
sig { params(resource: String, ids: T::Array[Integer], block: SnapshotCallback).void }
|
|
138
119
|
def snapshots(resource, ids, &block)
|
|
139
120
|
raise("not ready") unless @ready
|
|
@@ -143,7 +124,6 @@ module E621Stream
|
|
|
143
124
|
send_message({ op: "snapshot", d: { resource: resource, ids: ids, nonce: nonce } })
|
|
144
125
|
end
|
|
145
126
|
|
|
146
|
-
# Closes the connection for good.
|
|
147
127
|
sig { void }
|
|
148
128
|
def close
|
|
149
129
|
@closing = true
|
|
@@ -154,8 +134,7 @@ module E621Stream
|
|
|
154
134
|
|
|
155
135
|
sig { returns([Integer, String]) }
|
|
156
136
|
def connect_once
|
|
157
|
-
|
|
158
|
-
@connector.connect(@connector.websocket_url(options.url)) do |connection|
|
|
137
|
+
@connector.connect(versioned(@connector.websocket_url(options.url))) do |connection|
|
|
159
138
|
@connection = connection
|
|
160
139
|
@last_message = now
|
|
161
140
|
heartbeat = Async { heartbeat_loop(connection) }
|
|
@@ -171,7 +150,6 @@ module E621Stream
|
|
|
171
150
|
@pending_snapshots.clear
|
|
172
151
|
end
|
|
173
152
|
end
|
|
174
|
-
# anything but a normal close raises out of read
|
|
175
153
|
[1000, ""]
|
|
176
154
|
rescue Protocol::WebSocket::ClosedError => e
|
|
177
155
|
[e.code || 1006, e.message]
|
|
@@ -199,6 +177,14 @@ module E621Stream
|
|
|
199
177
|
data = message["d"]
|
|
200
178
|
case message["op"]
|
|
201
179
|
when "hello"
|
|
180
|
+
unless data["version"] == Events::PROTOCOL_VERSION
|
|
181
|
+
# a server that ignored ?v= would send messages this can't read
|
|
182
|
+
message = "the server speaks protocol version #{data['version']}, this client is for #{Events::PROTOCOL_VERSION}, update it"
|
|
183
|
+
@last_error = StreamError.new(CloseCodes::UNSUPPORTED_VERSION, message)
|
|
184
|
+
@connection&.close(CloseCodes::UNSUPPORTED_VERSION, "unsupported protocol version")
|
|
185
|
+
raise(Protocol::WebSocket::ClosedError.new(message, CloseCodes::UNSUPPORTED_VERSION))
|
|
186
|
+
end
|
|
187
|
+
|
|
202
188
|
identify = { subscriptions: options.subscriptions }
|
|
203
189
|
if options.token
|
|
204
190
|
identify[:auth] = { token: options.token }
|
|
@@ -213,10 +199,19 @@ module E621Stream
|
|
|
213
199
|
@ready = true
|
|
214
200
|
emit("ready", data)
|
|
215
201
|
when "dispatch"
|
|
216
|
-
|
|
202
|
+
# s is the event's id, t when it happened
|
|
203
|
+
event = Event.new(id: message["s"], type: data["type"], time: message["t"], data: data["data"], context: data["context"],
|
|
204
|
+
private: data["private"] == true, recovered: data["recovered"] == true)
|
|
217
205
|
@cursor = event.id unless event.private?
|
|
218
206
|
emit("dispatch", event)
|
|
219
207
|
emit(event.type, event)
|
|
208
|
+
# notices are on the stream too, so they move the cursor
|
|
209
|
+
when "recovery"
|
|
210
|
+
@cursor = message["s"] if message["s"]
|
|
211
|
+
emit("recovery", data["data"])
|
|
212
|
+
when "slow_mode"
|
|
213
|
+
@cursor = message["s"] if message["s"]
|
|
214
|
+
emit("slow_mode", data["data"])
|
|
220
215
|
when "replay_complete"
|
|
221
216
|
emit("replay_complete", data)
|
|
222
217
|
when "subscribed"
|
|
@@ -226,7 +221,6 @@ module E621Stream
|
|
|
226
221
|
return unless pending
|
|
227
222
|
|
|
228
223
|
data["snapshots"].each { |id, snapshot| pending[1][id.to_i] = snapshot }
|
|
229
|
-
# older servers don't send done
|
|
230
224
|
return if data["done"] == false
|
|
231
225
|
|
|
232
226
|
@pending_snapshots.delete(data["nonce"])
|
|
@@ -236,6 +230,15 @@ module E621Stream
|
|
|
236
230
|
end
|
|
237
231
|
end
|
|
238
232
|
|
|
233
|
+
# so a server that's moved past this version closes with 4014 rather than sending what this can't read
|
|
234
|
+
sig { params(url: String).returns(String) }
|
|
235
|
+
def versioned(url)
|
|
236
|
+
uri = URI.parse(url)
|
|
237
|
+
query = URI.decode_www_form(uri.query || "").to_h.except("v")
|
|
238
|
+
uri.query = URI.encode_www_form(query.merge("v" => Events::PROTOCOL_VERSION.to_s))
|
|
239
|
+
uri.to_s
|
|
240
|
+
end
|
|
241
|
+
|
|
239
242
|
sig { params(payload: T::Hash[Symbol, T.untyped]).void }
|
|
240
243
|
def send_message(payload)
|
|
241
244
|
connection = @connection
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
# typed: strict
|
|
3
3
|
|
|
4
4
|
module E621Stream
|
|
5
|
-
# Why the server closed the connection, see https://e621.stream/close-codes/
|
|
6
5
|
module CloseCodes
|
|
7
6
|
UNKNOWN_ERROR = 4000
|
|
8
7
|
UNKNOWN_OP = 4001
|
|
@@ -18,8 +17,11 @@ module E621Stream
|
|
|
18
17
|
AUTHENTICATION_EXPIRED = 4011
|
|
19
18
|
DISCONNECTED = 4012
|
|
20
19
|
BANNED = 4013
|
|
20
|
+
UNSUPPORTED_VERSION = 4014
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
FATAL = T.let([
|
|
23
|
+
UNKNOWN_OP, DECODE_ERROR, AUTHENTICATION_FAILED, ALREADY_IDENTIFIED, INVALID_SUBSCRIPTION, INVALID_CURSOR,
|
|
24
|
+
AUTHENTICATION_EXPIRED, DISCONNECTED, BANNED, UNSUPPORTED_VERSION,
|
|
25
|
+
].freeze, T::Array[Integer])
|
|
24
26
|
end
|
|
25
27
|
end
|
data/lib/e621stream/event.rb
CHANGED
|
@@ -2,23 +2,26 @@
|
|
|
2
2
|
# typed: strict
|
|
3
3
|
|
|
4
4
|
module E621Stream
|
|
5
|
-
# One event from the stream. `data` is the record as e621's api returns it, or for update events
|
|
6
|
-
# `{ "<resource>" => record, "changes" => ... }`, see https://e621.stream/events/
|
|
7
5
|
class Event < T::Struct
|
|
8
6
|
extend(T::Sig)
|
|
9
7
|
|
|
10
|
-
# The event's cursor, pass it as `cursor:` to resume after it. Private events have no cursor.
|
|
11
8
|
const(:id, String)
|
|
12
9
|
const(:type, String)
|
|
13
|
-
# When it happened on e621.
|
|
14
10
|
const(:time, String)
|
|
15
11
|
const(:data, T::Hash[String, T.untyped])
|
|
16
|
-
#
|
|
12
|
+
# What the record doesn't say itself, like who made the change.
|
|
13
|
+
const(:context, T.nilable(T::Hash[String, T.untyped]), default: nil)
|
|
17
14
|
const(:private, T::Boolean, default: false)
|
|
15
|
+
const(:recovered, T::Boolean, default: false)
|
|
18
16
|
|
|
19
17
|
sig { returns(T::Boolean) }
|
|
20
18
|
def private?
|
|
21
19
|
private
|
|
22
20
|
end
|
|
21
|
+
|
|
22
|
+
sig { returns(T::Boolean) }
|
|
23
|
+
def recovered?
|
|
24
|
+
recovered
|
|
25
|
+
end
|
|
23
26
|
end
|
|
24
27
|
end
|
data/lib/e621stream/events.rb
CHANGED
|
@@ -3,23 +3,30 @@
|
|
|
3
3
|
|
|
4
4
|
module E621Stream
|
|
5
5
|
module Events
|
|
6
|
-
#
|
|
6
|
+
# The protocol version this client speaks, sent as ?v= when connecting.
|
|
7
|
+
PROTOCOL_VERSION = 2
|
|
8
|
+
|
|
9
|
+
# Generated by scripts/clientTypes.ts in the E621Stream repository, from the server's src/gateway/events.ts.
|
|
7
10
|
EVENT_TYPES = %w[
|
|
8
|
-
artist.create artist.
|
|
9
|
-
avoid_posting.delete avoid_posting.destroy avoid_posting.undelete
|
|
10
|
-
avoid_posting_version.create ban.create ban.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
artist.create artist.destroy artist.update artist_url.create artist_url.destroy artist_url.update
|
|
12
|
+
artist_version.create avoid_posting.create avoid_posting.delete avoid_posting.destroy avoid_posting.undelete
|
|
13
|
+
avoid_posting.update avoid_posting_version.create ban.create ban.destroy ban.update blip.create blip.delete
|
|
14
|
+
blip.destroy blip.undelete blip.update bulk_update_request.create bulk_update_request.update comment.create
|
|
15
|
+
comment.destroy comment.hide comment.unhide comment.update forum_category.create forum_category.destroy
|
|
16
|
+
forum_category.update forum_post.create forum_post.destroy forum_post.hide forum_post.unhide forum_post.update
|
|
17
|
+
forum_topic.create forum_topic.destroy forum_topic.hide forum_topic.unhide forum_topic.update help_page.create
|
|
18
|
+
help_page.destroy help_page.update mod_action.create news_update.create news_update.destroy news_update.update
|
|
19
|
+
note.create note.delete note.undelete note.update note_version.create pool.add_posts pool.create pool.destroy
|
|
20
|
+
pool.remove_posts pool.update pool_version.create post.create post.destroy post.stats post.update
|
|
21
|
+
post_approval.create post_approval.destroy post_event.create post_flag.create post_flag.update
|
|
17
22
|
post_replacement.create post_replacement.destroy post_replacement.update post_set.add_posts post_set.create
|
|
18
|
-
post_set.
|
|
19
|
-
tag.create tag.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
post_set.destroy post_set.remove_posts post_set.update post_version.create post_version.hide post_version.unhide
|
|
24
|
+
tag.create tag.destroy tag.update tag_type_version.create tag_alias.create tag_alias.update
|
|
25
|
+
tag_implication.create tag_implication.update takedown.create takedown.destroy takedown.update
|
|
26
|
+
upload_karma_event.create upload_url_rule.create upload_url_rule.destroy upload_url_rule.update
|
|
27
|
+
upload_whitelist.create upload_whitelist.destroy upload_whitelist.update user.create user.destroy user.update
|
|
28
|
+
user_feedback.create user_feedback.delete user_feedback.destroy user_feedback.undelete user_feedback.update
|
|
29
|
+
wiki_page.create wiki_page.destroy wiki_page.update wiki_page_version.create
|
|
23
30
|
].freeze
|
|
24
31
|
|
|
25
32
|
# Polled with your own credentials and only sent to you. They have to be subscribed to by name, `*` doesn't include
|
data/lib/e621stream/version.rb
CHANGED
data/lib/e621stream.rb
CHANGED