e621stream 0.0.2 → 0.0.3

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: dc6c807b32f91dc3e755c6f97ca580b91cae5c6f2e3c18ee1db04b0b2e5420c9
4
+ data.tar.gz: 633f8cb92b1c9e65f87bb0d03011a7680ecca1fbcfc15c64cc60445a2430dc00
5
5
  SHA512:
6
- metadata.gz: f6d4fae99737f6f2b1b57ffe083662ba32b0a64ca6bacf9bc048ced7afe8da4216d0ebe70515757667a1d6ed86fac452310acb2ef500170fea921f91f28ea690
7
- data.tar.gz: d276a848290525a9ca77d3a5c432ca2d8baf17a34142b1d52f756d07688e69d608a6a47d9da4c3a33a7bed99e9bc6d932c5b2ad571ddca5fec3cd1ff843ffa7b
6
+ metadata.gz: 01727d098cd998cdf67b6c6d79dac43d1caa156cef0f398fff2dfd1094ce3e5540842727cc188bda0cddd8e88d9c1394aa70af28d456daf2a044899280faaefc
7
+ data.tar.gz: 8cb060095b29f36cff0c5242896bf8e9310febc92854db10fe5a8cb365b1df95032f8ce5b3105c22cbcfe6f1c1056ed1b155a1f26ce46583ea3157c6f71b5d92
data/README.md CHANGED
@@ -22,11 +22,13 @@ 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.
30
32
 
31
33
  ```sh
32
34
  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
@@ -27,6 +28,25 @@ module E621Stream
27
28
  endpoint = Async::HTTP::Endpoint.parse(url, alpn_protocols: Async::HTTP::Protocol::HTTP11.names)
28
29
  Async::WebSocket::Client.connect(endpoint, &block)
29
30
  end
31
+
32
+ # Gets the websocket url from the site's /info, unless it's one already.
33
+ sig { params(url: String).returns(String) }
34
+ def websocket_url(url)
35
+ return url if url.match?(%r{\Awss?://}i)
36
+
37
+ info = URI.join(url, "/info").to_s
38
+ internet = Async::HTTP::Internet.new
39
+ response = internet.get(info)
40
+ raise("#{info} returned #{response.status}") unless response.success?
41
+
42
+ websocket = JSON.parse(response.read)["websocket"]
43
+ raise("#{info} didn't have a websocket url") unless websocket.is_a?(String)
44
+
45
+ URI.join(url, websocket).to_s
46
+ ensure
47
+ response&.close
48
+ internet&.close
49
+ end
30
50
  end
31
51
 
32
52
  HEARTBEAT_SECONDS = 30
@@ -45,6 +65,7 @@ module E621Stream
45
65
  subscriptions: T::Array[Subscription],
46
66
  username: T.nilable(String),
47
67
  api_key: T.nilable(String),
68
+ token: T.nilable(String),
48
69
  url: String,
49
70
  client: T.nilable(String),
50
71
  cursor: T.nilable(T.any(String, Integer)),
@@ -52,8 +73,8 @@ module E621Stream
52
73
  connector: Connector,
53
74
  ).void
54
75
  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)
76
+ def initialize(subscriptions:, username: nil, api_key: nil, token: nil, url: "https://e621.stream", client: nil, cursor: nil, reconnect: true, connector: Connector.new)
77
+ @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
78
  @connector = connector
58
79
  @cursor = T.let(cursor&.to_s, T.nilable(String))
59
80
  @listeners = T.let(Hash.new { |hash, key| hash[key] = [] }, T::Hash[String, T::Array[Listener]])
@@ -114,7 +135,8 @@ module E621Stream
114
135
 
115
136
  sig { returns([Integer, String]) }
116
137
  def connect_once
117
- @connector.connect(options.url) do |connection|
138
+ # fetched every time, so it can move
139
+ @connector.connect(@connector.websocket_url(options.url)) do |connection|
118
140
  @connection = connection
119
141
  @last_message = now
120
142
  heartbeat = Async { heartbeat_loop(connection) }
@@ -157,7 +179,11 @@ module E621Stream
157
179
  case message["op"]
158
180
  when "hello"
159
181
  identify = { subscriptions: options.subscriptions }
160
- identify[:auth] = { username: options.username, api_key: options.api_key } if options.username && options.api_key
182
+ if options.token
183
+ identify[:auth] = { token: options.token }
184
+ elsif options.username && options.api_key
185
+ identify[:auth] = { username: options.username, api_key: options.api_key }
186
+ end
161
187
  identify[:client] = options.client if options.client
162
188
  identify[:cursor] = @cursor if @cursor
163
189
  send_message({ op: "identify", d: identify })
@@ -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.3"
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Donovan_DMC