nakitwitch 0.0.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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/lib/nakitwitch.rb +88 -0
  4. data/nakitwitch.gemspec +15 -0
  5. metadata +74 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3970325c621105476820e5a9eb57dccf0898b65a0519c21c79e9e43dd3ab7a03
4
+ data.tar.gz: 1b7ac3dce0bd24a0e0600a3f35abf2376cb26eaaafcd34ad49b5e8d527093203
5
+ SHA512:
6
+ metadata.gz: 1dad8a601b2687515443b1bbf83ef04c970e37ad81fa0b8002878d5ed57d07306f17d8b3cd935e6e517f8d48f84a15dabbf4693cd4defed522bfc85063509cec
7
+ data.tar.gz: d299d0da57e897c025aa6161e9cd378af1dfcf61a0943dc61fdc70af5498342d4cb0480a062e8965578a48871b8c0fb63b14bd0f6c38aba2ccafa2bd6ad6e09a
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Victor Maslov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/lib/nakitwitch.rb ADDED
@@ -0,0 +1,88 @@
1
+ module Twitch
2
+ Error = ::Class.new ::RuntimeError
3
+
4
+ # AUTH
5
+ def self.configure client_id, client_secret, tokens_filename
6
+ @client_id = client_id
7
+ @client_secret = client_secret
8
+ @tokens_filename = tokens_filename
9
+ end
10
+ def self.refresh
11
+ ::File.write @tokens_filename, ::NetHTTPUtils.request_data("https://id.twitch.tv/oauth2/token", :POST, form: {
12
+ client_id: @client_id || raise(Error, "missing configuration (#{::Module.nesting}.configure)"),
13
+ client_secret: @client_secret || raise(Error, "missing configuration (#{::Module.nesting}.configure)"),
14
+ grant_type: "refresh_token",
15
+ refresh_token: ::JSON.load(::File.read(@tokens_filename)).fetch("refresh_token")
16
+ } )
17
+ end
18
+ def self.oauth_token
19
+ unless ::File.exist? @tokens_filename
20
+ puts <<~HEREDOC
21
+ 1. create app: https://dev.twitch.tv/console/apps/create
22
+ 2. find <client_id> and create client_secret on apps properties page, save both
23
+ 3. set OAuth Redirect URL: http://localhost:3000
24
+ 4. obtain <auth code> by opening in web browser (example scopes are for a chat bot):
25
+ https://id.twitch.tv/oauth2/authorize?response_type=code&client_id=<client_id>&redirect_uri=http://localhost:3000&scope=chat:read+chat:edit+moderator:read:followers
26
+ 5. get tokens (the auth code is a single time use):
27
+ $ curl https://id.twitch.tv/oauth2/token -d "client_id=$( \
28
+ cat client_id.twitch.secret \
29
+ )&client_secret=$( \
30
+ cat client_secret.twitch.secret \
31
+ )&grant_type=authorization_code&code=<auth code>&redirect_uri=http://localhost:3000" \
32
+ >tokens.twitch.secret.json
33
+ HEREDOC
34
+ end
35
+ "oauth:#{(::JSON.load ::File.read @tokens_filename)["access_token"]}"
36
+ end
37
+
38
+ def self.banword str
39
+ str.
40
+ gsub(/н(ег|иге)р/, "<censored>").
41
+ gsub(/п[еи]д[ао]рас/, "<censored>")
42
+ end
43
+
44
+ SCHEMA = {
45
+ "videos" => { hash: {
46
+ "data" => {
47
+ size: 0..1,
48
+ each: { hash_req: {
49
+ "user_id" => /\A[0-9]+\z/,
50
+ "user_login" => /\A\S+\z/,
51
+ "user_name" => /\S/,
52
+ "title" => /\S/,
53
+ "published_at" => /\A202\d-(0[1-9]|1[0-2])-([012][0-9]|3[01])T[012][0-9]:[0-5][0-9]:[0-5][0-9]Z\z/,
54
+ "view_count" => 0..1000000000,
55
+ } },
56
+ },
57
+ "pagination" => { hash: {} }
58
+ } },
59
+ "channels/followers" => { hash: {
60
+ "total" => 0..1000000000,
61
+ "data" => [[]],
62
+ "pagination" => { hash: {} },
63
+ } },
64
+ }
65
+ require "nethttputils"
66
+ require "json"
67
+ require "nakischema"
68
+ def self.request mtd, retry_delay = 5, **form
69
+ ::JSON.load( begin
70
+ ::NetHTTPUtils.request_data "https://api.twitch.tv/helix/#{mtd}", form: form, header: {
71
+ "Authorization" => "Bearer #{::JSON.load(::File.read(@tokens_filename)).fetch "access_token"}",
72
+ "client-id" => @client_id || raise(Error, "missing configuration (#{::Module.nesting}.configure)")
73
+ }
74
+ rescue ::NetHTTPUtils::Error
75
+ fail unless '{"error":"Unauthorized","status":401,"message":"Invalid OAuth token"}' == $!.body
76
+ refresh
77
+ sleep retry_delay
78
+ retry
79
+ end ).tap do |_|
80
+ ::Nakischema.validate _, SCHEMA[mtd] if SCHEMA.key? mtd
81
+ end
82
+ end
83
+
84
+ def self.login_to_id login
85
+ request("users", "login" => login)["data"][0]["id"]
86
+ end
87
+
88
+ end
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "nakitwitch"
3
+ spec.version = "0.0.0"
4
+ spec.summary = "common Twitch API routines"
5
+
6
+ spec.author = "Victor Maslov aka Nakilon"
7
+ spec.email = "nakilon@gmail.com"
8
+ spec.license = "MIT"
9
+ spec.metadata = {"source_code_uri" => "https://github.com/nakilon/nakitwitch"}
10
+
11
+ spec.add_dependency "nethttputils"
12
+ spec.add_dependency "nakischema"
13
+
14
+ spec.files = %w{ LICENSE nakitwitch.gemspec lib/nakitwitch.rb }
15
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nakitwitch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Victor Maslov aka Nakilon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nethttputils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nakischema
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email: nakilon@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - LICENSE
48
+ - lib/nakitwitch.rb
49
+ - nakitwitch.gemspec
50
+ homepage:
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ source_code_uri: https://github.com/nakilon/nakitwitch
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.1.6
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: common Twitch API routines
74
+ test_files: []