e621stream 0.0.4 → 0.0.6
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 +4 -2
- data/lib/e621stream/client.rb +20 -6
- data/lib/e621stream/event.rb +7 -0
- data/lib/e621stream/version.rb +1 -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: b0b273776fc1b808a7544474ea58ffc49d3ba07147bbb99d458c749ff26ea875
|
|
4
|
+
data.tar.gz: 8df64f093a820b05bbd334c189f25dd23eb1194445a219dceddcb260ca6515ce
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d94db80a321cd2e0ced4abd6fa43be23e10a6a67b8758c6095d8b88ffa3a939841f1036bfcaea620623b31f4cff3733a8303d4cf5340e7634f428055206b3779
|
|
7
|
+
data.tar.gz: 5f7497b80e5b911302a286c7bc5935bc775906baf32bd56b0d5fec685f61df084f29cf69c25bd1ca7705ec264fbcd3259645600352bd0d21e8862f13ec2edf16
|
data/README.md
CHANGED
|
@@ -26,11 +26,13 @@ Instead of `username:` & `api_key:`, you can pass `token:` with a token from [/a
|
|
|
26
26
|
|
|
27
27
|
`run` blocks, unless you're already in a reactor, then it runs as a task and `client.close` stops it.
|
|
28
28
|
|
|
29
|
-
`on("post.create")` listens for one type, `on(:dispatch)` for everything. Events have `id`, `type`, `time` and `data` (the record as the api returns it), see [Events](https://e621.stream/events/). There's also `:ready`, `:replay_complete`, `:subscribed`, `:reconnecting`, `:close` and `:error`.
|
|
29
|
+
`on("post.create")` listens for one type, `on(:dispatch)` for everything. Events have `id`, `type`, `time` and `data` (the record as the api returns it), see [Events](https://e621.stream/events/). There's also `:ready`, `:replay_complete`, `:subscribed`, `:recovery`, `:reconnecting`, `:close` and `:error`.
|
|
30
|
+
|
|
31
|
+
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.
|
|
30
32
|
|
|
31
33
|
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.
|
|
32
34
|
|
|
33
|
-
`client.snapshots(resource, ids) { |snapshots| ... }` gets what was last seen of up to
|
|
35
|
+
`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. Only after `:ready`, and the block is dropped if the connection closes first.
|
|
34
36
|
|
|
35
37
|
```ruby
|
|
36
38
|
client.on(:ready) do
|
data/lib/e621stream/client.rb
CHANGED
|
@@ -87,11 +87,12 @@ module E621Stream
|
|
|
87
87
|
@last_error = T.let(nil, T.nilable(StreamError))
|
|
88
88
|
@ready = T.let(false, T::Boolean)
|
|
89
89
|
@nonce = T.let(0, Integer)
|
|
90
|
-
|
|
90
|
+
# the callback and what's been received so far, big requests come back in chunks
|
|
91
|
+
@pending_snapshots = T.let({}, T::Hash[String, [SnapshotCallback, Snapshots]])
|
|
91
92
|
end
|
|
92
93
|
|
|
93
94
|
# Listens for an event type (like "post.create"), or one of :dispatch (every event), :ready, :replay_complete,
|
|
94
|
-
# :subscribed, :error, :close and :reconnecting.
|
|
95
|
+
# :subscribed, :recovery, :error, :close and :reconnecting.
|
|
95
96
|
sig { params(name: T.any(String, Symbol), block: Listener).returns(T.self_type) }
|
|
96
97
|
def on(name, &block)
|
|
97
98
|
T.must(@listeners[name.to_s]) << block
|
|
@@ -129,7 +130,7 @@ module E621Stream
|
|
|
129
130
|
send_message({ op: "subscribe", d: { subscriptions: subscriptions } })
|
|
130
131
|
end
|
|
131
132
|
|
|
132
|
-
# Looks up what was last seen of up to
|
|
133
|
+
# Looks up what was last seen of up to 1000 records, see https://e621.stream/snapshots/. The block gets them by id,
|
|
133
134
|
# nil for ones that aren't known. It isn't called if the connection closes first.
|
|
134
135
|
#
|
|
135
136
|
# client.on(:ready) { client.snapshots("post", [1, 2]) { |posts| p(posts[1]&.dig("tags", "general")) } }
|
|
@@ -138,7 +139,7 @@ module E621Stream
|
|
|
138
139
|
raise("not ready") unless @ready
|
|
139
140
|
|
|
140
141
|
nonce = (@nonce += 1).to_s
|
|
141
|
-
@pending_snapshots[nonce] = block
|
|
142
|
+
@pending_snapshots[nonce] = [block, {}]
|
|
142
143
|
send_message({ op: "snapshot", d: { resource: resource, ids: ids, nonce: nonce } })
|
|
143
144
|
end
|
|
144
145
|
|
|
@@ -212,16 +213,29 @@ module E621Stream
|
|
|
212
213
|
@ready = true
|
|
213
214
|
emit("ready", data)
|
|
214
215
|
when "dispatch"
|
|
215
|
-
event = Event.new(id: message["id"], type: message["type"], time: message["time"], data: message["data"],
|
|
216
|
+
event = Event.new(id: message["id"], type: message["type"], time: message["time"], data: message["data"],
|
|
217
|
+
private: message["private"] == true, recovered: message["recovered"] == true)
|
|
216
218
|
@cursor = event.id unless event.private?
|
|
217
219
|
emit("dispatch", event)
|
|
218
220
|
emit(event.type, event)
|
|
221
|
+
when "recovery"
|
|
222
|
+
# these are on the stream too, so they move the cursor
|
|
223
|
+
@cursor = message["id"] if message["id"]
|
|
224
|
+
emit("recovery", data)
|
|
219
225
|
when "replay_complete"
|
|
220
226
|
emit("replay_complete", data)
|
|
221
227
|
when "subscribed"
|
|
222
228
|
emit("subscribed", data["subscriptions"])
|
|
223
229
|
when "snapshot"
|
|
224
|
-
@pending_snapshots
|
|
230
|
+
pending = @pending_snapshots[data["nonce"]]
|
|
231
|
+
return unless pending
|
|
232
|
+
|
|
233
|
+
data["snapshots"].each { |id, snapshot| pending[1][id.to_i] = snapshot }
|
|
234
|
+
# older servers don't send done
|
|
235
|
+
return if data["done"] == false
|
|
236
|
+
|
|
237
|
+
@pending_snapshots.delete(data["nonce"])
|
|
238
|
+
pending[0].call(pending[1])
|
|
225
239
|
when "error"
|
|
226
240
|
@last_error = StreamError.new(data["code"], data["message"])
|
|
227
241
|
end
|
data/lib/e621stream/event.rb
CHANGED
|
@@ -15,10 +15,17 @@ module E621Stream
|
|
|
15
15
|
const(:data, T::Hash[String, T.untyped])
|
|
16
16
|
# Only for private events, which are polled with your own credentials and never stored.
|
|
17
17
|
const(:private, T::Boolean, default: false)
|
|
18
|
+
# Sent late by a recovery, so it's out of order with the rest. See https://e621.stream/replaying/#recoveries
|
|
19
|
+
const(:recovered, T::Boolean, default: false)
|
|
18
20
|
|
|
19
21
|
sig { returns(T::Boolean) }
|
|
20
22
|
def private?
|
|
21
23
|
private
|
|
22
24
|
end
|
|
25
|
+
|
|
26
|
+
sig { returns(T::Boolean) }
|
|
27
|
+
def recovered?
|
|
28
|
+
recovered
|
|
29
|
+
end
|
|
23
30
|
end
|
|
24
31
|
end
|
data/lib/e621stream/version.rb
CHANGED