e621stream 0.0.3 → 0.0.5
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 +8 -0
- data/lib/e621stream/client.rb +32 -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: '08ef4491a9583e9ec39053e925526b63b55e93832dcdd3a7d72abe30f3328f1a'
|
|
4
|
+
data.tar.gz: b068046e0a4e360846adbdd1ef8ea31ca0dde651dbee9047f2947f133d59a866
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cd1cd57370807d2e3d268022fe2b4720288698de0543199a3dc0dcada3475750823192caa359b4f5013e90d7f97d4622837bb0a22ad723eb62467cc59b9e975a
|
|
7
|
+
data.tar.gz: 6cc7e3d99f09660f6bbe41802eec4ee2f50e90d891433cd43c4bd8e735ecf40f66e0a4b203bf5eed64289deaf1b4ef04de108bfdbd9ee165bee01dda2f3aa906
|
data/README.md
CHANGED
|
@@ -30,6 +30,14 @@ Instead of `username:` & `api_key:`, you can pass `token:` with a token from [/a
|
|
|
30
30
|
|
|
31
31
|
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
32
|
|
|
33
|
+
`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
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
client.on(:ready) do
|
|
37
|
+
client.snapshots("post", [6843484, 6843485]) { |posts| p(posts[6843484]&.dig("tags", "general")) }
|
|
38
|
+
end
|
|
39
|
+
```
|
|
40
|
+
|
|
33
41
|
```sh
|
|
34
42
|
bundle exec rake # rubocop, zeitwerk, tests & sorbet
|
|
35
43
|
```
|
data/lib/e621stream/client.rb
CHANGED
|
@@ -16,6 +16,8 @@ module E621Stream
|
|
|
16
16
|
extend(T::Sig)
|
|
17
17
|
|
|
18
18
|
Listener = T.type_alias { T.proc.params(arg0: T.untyped).void }
|
|
19
|
+
Snapshots = T.type_alias { T::Hash[Integer, T.nilable(T::Hash[String, T.untyped])] }
|
|
20
|
+
SnapshotCallback = T.type_alias { T.proc.params(snapshots: Snapshots).void }
|
|
19
21
|
# An event type, `<resource>.*`, `*`, or `{ "type" => ..., "filter" => { ... } }`.
|
|
20
22
|
Subscription = T.type_alias { T.any(String, T::Hash[T.any(String, Symbol), T.untyped]) }
|
|
21
23
|
|
|
@@ -83,6 +85,10 @@ module E621Stream
|
|
|
83
85
|
@attempt = T.let(0, Integer)
|
|
84
86
|
@last_message = T.let(0.0, Float)
|
|
85
87
|
@last_error = T.let(nil, T.nilable(StreamError))
|
|
88
|
+
@ready = T.let(false, T::Boolean)
|
|
89
|
+
@nonce = T.let(0, Integer)
|
|
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]])
|
|
86
92
|
end
|
|
87
93
|
|
|
88
94
|
# Listens for an event type (like "post.create"), or one of :dispatch (every event), :ready, :replay_complete,
|
|
@@ -124,6 +130,19 @@ module E621Stream
|
|
|
124
130
|
send_message({ op: "subscribe", d: { subscriptions: subscriptions } })
|
|
125
131
|
end
|
|
126
132
|
|
|
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
|
+
sig { params(resource: String, ids: T::Array[Integer], block: SnapshotCallback).void }
|
|
138
|
+
def snapshots(resource, ids, &block)
|
|
139
|
+
raise("not ready") unless @ready
|
|
140
|
+
|
|
141
|
+
nonce = (@nonce += 1).to_s
|
|
142
|
+
@pending_snapshots[nonce] = [block, {}]
|
|
143
|
+
send_message({ op: "snapshot", d: { resource: resource, ids: ids, nonce: nonce } })
|
|
144
|
+
end
|
|
145
|
+
|
|
127
146
|
# Closes the connection for good.
|
|
128
147
|
sig { void }
|
|
129
148
|
def close
|
|
@@ -148,6 +167,8 @@ module E621Stream
|
|
|
148
167
|
ensure
|
|
149
168
|
heartbeat.stop
|
|
150
169
|
@connection = nil
|
|
170
|
+
@ready = false
|
|
171
|
+
@pending_snapshots.clear
|
|
151
172
|
end
|
|
152
173
|
end
|
|
153
174
|
# anything but a normal close raises out of read
|
|
@@ -189,6 +210,7 @@ module E621Stream
|
|
|
189
210
|
send_message({ op: "identify", d: identify })
|
|
190
211
|
when "ready"
|
|
191
212
|
@attempt = 0
|
|
213
|
+
@ready = true
|
|
192
214
|
emit("ready", data)
|
|
193
215
|
when "dispatch"
|
|
194
216
|
event = Event.new(id: message["id"], type: message["type"], time: message["time"], data: message["data"], private: message["private"] == true)
|
|
@@ -199,6 +221,16 @@ module E621Stream
|
|
|
199
221
|
emit("replay_complete", data)
|
|
200
222
|
when "subscribed"
|
|
201
223
|
emit("subscribed", data["subscriptions"])
|
|
224
|
+
when "snapshot"
|
|
225
|
+
pending = @pending_snapshots[data["nonce"]]
|
|
226
|
+
return unless pending
|
|
227
|
+
|
|
228
|
+
data["snapshots"].each { |id, snapshot| pending[1][id.to_i] = snapshot }
|
|
229
|
+
# older servers don't send done
|
|
230
|
+
return if data["done"] == false
|
|
231
|
+
|
|
232
|
+
@pending_snapshots.delete(data["nonce"])
|
|
233
|
+
pending[0].call(pending[1])
|
|
202
234
|
when "error"
|
|
203
235
|
@last_error = StreamError.new(data["code"], data["message"])
|
|
204
236
|
end
|
data/lib/e621stream/version.rb
CHANGED