e621stream 0.0.2 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d44d1f8f30f4f8d3931eb4a5b1f428413191a84297dbf9f879bef61307205b88
4
- data.tar.gz: 7524d28a0ec987855de8e63c9c5efae8c2bd0bd0d3d07d846d03bcd16eaa8402
3
+ metadata.gz: '058c7ea74d261c3f1012be05f6b7d200551d54bfd197a48a8222b235848866e1'
4
+ data.tar.gz: f6b4f2e3065eb0b58a09e5df28f561e77e0b9ed49d202506a8684e5f73bb24d0
5
5
  SHA512:
6
- metadata.gz: f6d4fae99737f6f2b1b57ffe083662ba32b0a64ca6bacf9bc048ced7afe8da4216d0ebe70515757667a1d6ed86fac452310acb2ef500170fea921f91f28ea690
7
- data.tar.gz: d276a848290525a9ca77d3a5c432ca2d8baf17a34142b1d52f756d07688e69d608a6a47d9da4c3a33a7bed99e9bc6d932c5b2ad571ddca5fec3cd1ff843ffa7b
6
+ metadata.gz: 64620623caeb76829554793a7a7096fd2963702cb47de459634932d71c8d7c0735972c9111b8ab7baccded276978b7bb391c492a0097bf83f75cc24d61d2b726
7
+ data.tar.gz: 5a9a4c6718aa17e1cb918e995758ff68794ad824b2e65c72e727f42add2a1e39ab1555ad3dc5efcbad4449d65593fe8e9d75abdc250ec750e5c2cd5b7a08757e
data/README.md CHANGED
@@ -22,11 +22,21 @@ client.on(:error) { |error| warn(error.message) }
22
22
  client.run
23
23
  ```
24
24
 
25
+ Instead of `username:` & `api_key:`, you can pass `token:` with a token from [/authorize](https://e621.stream/authorize).
26
+
25
27
  `run` blocks, unless you're already in a reactor, then it runs as a task and `client.close` stops it.
26
28
 
27
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`.
28
30
 
29
- 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. `client.subscribe([...])` changes subscriptions. Private events like `dmail.create` have to be subscribed to by name.
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
+
33
+ `client.snapshots(resource, ids) { |snapshots| ... }` gets what was last seen of up to 100 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
+ ```
30
40
 
31
41
  ```sh
32
42
  bundle exec rake # rubocop, zeitwerk, tests & sorbet
@@ -8,7 +8,10 @@ module E621Stream
8
8
  # Your e621 username and an api key, from Account > Manage API Access.
9
9
  const(:username, T.nilable(String), default: nil)
10
10
  const(:api_key, T.nilable(String), default: nil)
11
- const(:url, String, default: "wss://e621.stream/ws")
11
+ # A token from https://e621.stream/authorize, instead of a username & api key.
12
+ 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
+ const(:url, String, default: "https://e621.stream")
12
15
  # A name for your client, shown in the server's logs.
13
16
  const(:client, T.nilable(String), default: nil)
14
17
  # An event id (or a millisecond timestamp) to replay from. After that, reconnecting resumes from the last event.
@@ -3,6 +3,7 @@
3
3
 
4
4
  require("async")
5
5
  require("async/http/endpoint")
6
+ require("async/http/internet")
6
7
  require("async/websocket/client")
7
8
 
8
9
  module E621Stream
@@ -15,6 +16,8 @@ module E621Stream
15
16
  extend(T::Sig)
16
17
 
17
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 }
18
21
  # An event type, `<resource>.*`, `*`, or `{ "type" => ..., "filter" => { ... } }`.
19
22
  Subscription = T.type_alias { T.any(String, T::Hash[T.any(String, Symbol), T.untyped]) }
20
23
 
@@ -27,6 +30,25 @@ module E621Stream
27
30
  endpoint = Async::HTTP::Endpoint.parse(url, alpn_protocols: Async::HTTP::Protocol::HTTP11.names)
28
31
  Async::WebSocket::Client.connect(endpoint, &block)
29
32
  end
33
+
34
+ # Gets the websocket url from the site's /info, unless it's one already.
35
+ sig { params(url: String).returns(String) }
36
+ def websocket_url(url)
37
+ return url if url.match?(%r{\Awss?://}i)
38
+
39
+ info = URI.join(url, "/info").to_s
40
+ internet = Async::HTTP::Internet.new
41
+ response = internet.get(info)
42
+ raise("#{info} returned #{response.status}") unless response.success?
43
+
44
+ websocket = JSON.parse(response.read)["websocket"]
45
+ raise("#{info} didn't have a websocket url") unless websocket.is_a?(String)
46
+
47
+ URI.join(url, websocket).to_s
48
+ ensure
49
+ response&.close
50
+ internet&.close
51
+ end
30
52
  end
31
53
 
32
54
  HEARTBEAT_SECONDS = 30
@@ -45,6 +67,7 @@ module E621Stream
45
67
  subscriptions: T::Array[Subscription],
46
68
  username: T.nilable(String),
47
69
  api_key: T.nilable(String),
70
+ token: T.nilable(String),
48
71
  url: String,
49
72
  client: T.nilable(String),
50
73
  cursor: T.nilable(T.any(String, Integer)),
@@ -52,8 +75,8 @@ module E621Stream
52
75
  connector: Connector,
53
76
  ).void
54
77
  end
55
- def initialize(subscriptions:, username: nil, api_key: nil, url: "wss://e621.stream/ws", client: nil, cursor: nil, reconnect: true, connector: Connector.new)
56
- @options = T.let(Options.new(subscriptions: subscriptions, username: username, api_key: api_key, url: url, client: client, cursor: cursor, reconnect: reconnect), Options)
78
+ def initialize(subscriptions:, username: nil, api_key: nil, token: nil, url: "https://e621.stream", client: nil, cursor: nil, reconnect: true, connector: Connector.new)
79
+ @options = T.let(Options.new(subscriptions: subscriptions, username: username, api_key: api_key, token: token, url: url, client: client, cursor: cursor, reconnect: reconnect), Options)
57
80
  @connector = connector
58
81
  @cursor = T.let(cursor&.to_s, T.nilable(String))
59
82
  @listeners = T.let(Hash.new { |hash, key| hash[key] = [] }, T::Hash[String, T::Array[Listener]])
@@ -62,6 +85,9 @@ module E621Stream
62
85
  @attempt = T.let(0, Integer)
63
86
  @last_message = T.let(0.0, Float)
64
87
  @last_error = T.let(nil, T.nilable(StreamError))
88
+ @ready = T.let(false, T::Boolean)
89
+ @nonce = T.let(0, Integer)
90
+ @pending_snapshots = T.let({}, T::Hash[String, SnapshotCallback])
65
91
  end
66
92
 
67
93
  # Listens for an event type (like "post.create"), or one of :dispatch (every event), :ready, :replay_complete,
@@ -103,6 +129,19 @@ module E621Stream
103
129
  send_message({ op: "subscribe", d: { subscriptions: subscriptions } })
104
130
  end
105
131
 
132
+ # Looks up what was last seen of up to 100 records, see https://e621.stream/snapshots/. The block gets them by id,
133
+ # nil for ones that aren't known. It isn't called if the connection closes first.
134
+ #
135
+ # client.on(:ready) { client.snapshots("post", [1, 2]) { |posts| p(posts[1]&.dig("tags", "general")) } }
136
+ sig { params(resource: String, ids: T::Array[Integer], block: SnapshotCallback).void }
137
+ def snapshots(resource, ids, &block)
138
+ raise("not ready") unless @ready
139
+
140
+ nonce = (@nonce += 1).to_s
141
+ @pending_snapshots[nonce] = block
142
+ send_message({ op: "snapshot", d: { resource: resource, ids: ids, nonce: nonce } })
143
+ end
144
+
106
145
  # Closes the connection for good.
107
146
  sig { void }
108
147
  def close
@@ -114,7 +153,8 @@ module E621Stream
114
153
 
115
154
  sig { returns([Integer, String]) }
116
155
  def connect_once
117
- @connector.connect(options.url) do |connection|
156
+ # fetched every time, so it can move
157
+ @connector.connect(@connector.websocket_url(options.url)) do |connection|
118
158
  @connection = connection
119
159
  @last_message = now
120
160
  heartbeat = Async { heartbeat_loop(connection) }
@@ -126,6 +166,8 @@ module E621Stream
126
166
  ensure
127
167
  heartbeat.stop
128
168
  @connection = nil
169
+ @ready = false
170
+ @pending_snapshots.clear
129
171
  end
130
172
  end
131
173
  # anything but a normal close raises out of read
@@ -157,12 +199,17 @@ module E621Stream
157
199
  case message["op"]
158
200
  when "hello"
159
201
  identify = { subscriptions: options.subscriptions }
160
- identify[:auth] = { username: options.username, api_key: options.api_key } if options.username && options.api_key
202
+ if options.token
203
+ identify[:auth] = { token: options.token }
204
+ elsif options.username && options.api_key
205
+ identify[:auth] = { username: options.username, api_key: options.api_key }
206
+ end
161
207
  identify[:client] = options.client if options.client
162
208
  identify[:cursor] = @cursor if @cursor
163
209
  send_message({ op: "identify", d: identify })
164
210
  when "ready"
165
211
  @attempt = 0
212
+ @ready = true
166
213
  emit("ready", data)
167
214
  when "dispatch"
168
215
  event = Event.new(id: message["id"], type: message["type"], time: message["time"], data: message["data"], private: message["private"] == true)
@@ -173,6 +220,8 @@ module E621Stream
173
220
  emit("replay_complete", data)
174
221
  when "subscribed"
175
222
  emit("subscribed", data["subscriptions"])
223
+ when "snapshot"
224
+ @pending_snapshots.delete(data["nonce"])&.call(data["snapshots"].to_h { |id, snapshot| [id.to_i, snapshot] })
176
225
  when "error"
177
226
  @last_error = StreamError.new(data["code"], data["message"])
178
227
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module E621Stream
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.4"
5
5
  end
@@ -11,12 +11,12 @@
11
11
  # Released under the MIT License.
12
12
  # Copyright, 2025, by Samuel Williams.
13
13
  #
14
- # pkg:gem/async-http#lib/async/http/protocol/defaulton.rb:6
14
+ # pkg:gem/async-http#lib/async/http/protocol/configurable.rb:6
15
15
  module Async; end
16
16
 
17
17
  # @namespace
18
18
  #
19
- # pkg:gem/async-http#lib/async/http/protocol/defaulton.rb:7
19
+ # pkg:gem/async-http#lib/async/http/protocol/configurable.rb:7
20
20
  module Async::HTTP; end
21
21
 
22
22
  # @namespace
@@ -313,9 +313,88 @@ end
313
313
  # pkg:gem/async-http#lib/async/http/endpoint.rb:24
314
314
  Async::HTTP::Endpoint::SCHEMES = T.let(T.unsafe(nil), Hash)
315
315
 
316
+ # A high-level HTTP client for making requests to any URL, managing a pool of persistent connections keyed by host.
317
+ #
318
+ # pkg:gem/async-http#lib/async/http/internet.rb:17
319
+ class Async::HTTP::Internet
320
+ # Initialize the internet client.
321
+ # @parameter options [Hash] Options to pass to the underlying {Client} instances.
322
+ #
323
+ # pkg:gem/async-http#lib/async/http/internet.rb:20
324
+ def initialize(**options); end
325
+
326
+ # Make a request to the internet with the given `method` and `url`.
327
+ #
328
+ # If you provide non-frozen headers, they may be mutated.
329
+ #
330
+ # @parameter method [String] The request method, e.g. `GET`.
331
+ # @parameter url [String] The URL to request, e.g. `https://www.codeotaku.com`.
332
+ # @parameter headers [Hash | Protocol::HTTP::Headers] The headers to send with the request.
333
+ # @parameter body [String | Protocol::HTTP::Body] The body to send with the request.
334
+ #
335
+ # pkg:gem/async-http#lib/async/http/internet.rb:48
336
+ def call(verb, url, *arguments, **options, &block); end
337
+
338
+ # Get or create a client for the given endpoint.
339
+ # @parameter endpoint [Endpoint] The endpoint to connect to.
340
+ # @returns [Client] A client suitable for making requests to the endpoint.
341
+ #
342
+ # pkg:gem/async-http#lib/async/http/internet.rb:32
343
+ def client_for(endpoint); end
344
+
345
+ # A cache of clients.
346
+ # @attribute [Hash(URI, Client)]
347
+ #
348
+ # pkg:gem/async-http#lib/async/http/internet.rb:27
349
+ def clients; end
350
+
351
+ # Close all cached clients and release their resources.
352
+ #
353
+ # pkg:gem/async-http#lib/async/http/internet.rb:69
354
+ def close; end
355
+
356
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
357
+ def connect(url, *arguments, **options, &block); end
358
+
359
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
360
+ def delete(url, *arguments, **options, &block); end
361
+
362
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
363
+ def get(url, *arguments, **options, &block); end
364
+
365
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
366
+ def head(url, *arguments, **options, &block); end
367
+
368
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
369
+ def options(url, *arguments, **options, &block); end
370
+
371
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
372
+ def patch(url, *arguments, **options, &block); end
373
+
374
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
375
+ def post(url, *arguments, **options, &block); end
376
+
377
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
378
+ def put(url, *arguments, **options, &block); end
379
+
380
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
381
+ def query(url, *arguments, **options, &block); end
382
+
383
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
384
+ def trace(url, *arguments, **options, &block); end
385
+
386
+ protected
387
+
388
+ # pkg:gem/async-http#lib/async/http/internet.rb:91
389
+ def host_key(endpoint); end
390
+
391
+ # pkg:gem/async-http#lib/async/http/internet.rb:85
392
+ def make_client(endpoint); end
393
+ end
394
+
316
395
  # A protocol specifies a way in which to communicate with a remote peer.
317
396
  #
318
- # pkg:gem/async-http#lib/async/http/protocol/defaulton.rb:8
397
+ # pkg:gem/async-http#lib/async/http/protocol/configurable.rb:8
319
398
  module Async::HTTP::Protocol; end
320
399
 
321
400
  # Provides a `new` method that creates a {Configured} wrapper, allowing protocols to be instantiated with custom options.
@@ -2,3 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  # Add your extra requires here (`bin/tapioca require` can be used to bootstrap this list)
5
+
6
+ require("async/http/internet")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: e621stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Donovan_DMC