huginn_twitch_agent 0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f1cc82e7e80cc2e2aa423f0243c01d9e919e1039a0523a2f49419d219496986a
4
+ data.tar.gz: bb675dcce1e88d61650dcf9f0548d5c174db25d9c47cb4aeda32ed283814c45a
5
+ SHA512:
6
+ metadata.gz: 3052680728fe48f0a355c9ef3f5d09fee0cea334cd5729f21185a1e00de464fbec247290ffceba1dc9e003c62b8c03b965df51aaba8c1351e71e5e2b4beda303
7
+ data.tar.gz: 3b9322ebb8a64aaaebeac4356ac408d6cf29938f4fe9ab86ec3f5dad8d7e6f5345a5e95d34b49b1e502a6defcc295a2062446dcdb6360c1b3903060f127cc30c
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2022 Nicolas Germain
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,222 @@
1
+ module Agents
2
+ class TwitchAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule "never"
7
+
8
+ description do
9
+ <<-MD
10
+ The Twitch Agent is able to find information about users, checking live steam, others.
11
+
12
+ `debug` is used for verbose mode.
13
+
14
+ `user_id` is the id of the user.
15
+
16
+ `client_secret` is the secret of your app.
17
+
18
+ `access_token` is token created for your app.
19
+
20
+ `client_id` is the id of your app.
21
+
22
+ `type` is for the wanted action like get_user_informations/active_streams.
23
+
24
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
25
+ that you anticipate passing without this Agent receiving an incoming Event.
26
+ MD
27
+ end
28
+
29
+ event_description <<-MD
30
+ Events look like this:
31
+
32
+ {
33
+ "data": [
34
+ {
35
+ "id": "187039841",
36
+ "login": "xtivalia",
37
+ "display_name": "xTivalia",
38
+ "type": "",
39
+ "broadcaster_type": "affiliate",
40
+ "description": "Streameuse multigaming de 24ans, joueuse PC depuis 9 ans. Chill, bonne humeur et fun uniquement. ",
41
+ "profile_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/1deae406-248d-40e0-9775-5e67da5eafd9-profile_image-300x300.png",
42
+ "offline_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/f7b1b1da-f573-4c01-b9e2-f8c9d84c08cc-channel_offline_image-1920x1080.jpeg",
43
+ "view_count": 1227,
44
+ "created_at": "2017-12-23T18:56:07Z"
45
+ }
46
+ ]
47
+ }
48
+ MD
49
+
50
+ def default_options
51
+ {
52
+ 'client_id' => '',
53
+ 'user_id' => '',
54
+ 'client_secret' => '',
55
+ 'access_token' => '',
56
+ 'debug' => 'false',
57
+ 'emit_events' => 'false',
58
+ 'expected_receive_period_in_days' => '2',
59
+ }
60
+ end
61
+
62
+ form_configurable :user_id, type: :string
63
+ form_configurable :client_id, type: :string
64
+ form_configurable :client_secret, type: :string
65
+ form_configurable :access_token, type: :string
66
+ form_configurable :debug, type: :boolean
67
+ form_configurable :emit_events, type: :boolean
68
+ form_configurable :expected_receive_period_in_days, type: :string
69
+ form_configurable :type, type: :array, values: ['get_user_informations', 'active_streams']
70
+ def validate_options
71
+ errors.add(:base, "type has invalid value: should be 'get_user_informations', 'active_streams'") if interpolated['type'].present? && !%w(get_user_informations active_streams).include?(interpolated['type'])
72
+
73
+ unless options['user_id'].present?
74
+ errors.add(:base, "user_id is a required field")
75
+ end
76
+
77
+ unless options['client_id'].present?
78
+ errors.add(:base, "client_id is a required field")
79
+ end
80
+
81
+ unless options['client_secret'].present?
82
+ errors.add(:base, "client_secret is a required field")
83
+ end
84
+
85
+ unless options['access_token'].present?
86
+ errors.add(:base, "access_token is a required field")
87
+ end
88
+
89
+ if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
90
+ errors.add(:base, "if provided, emit_events must be true or false")
91
+ end
92
+
93
+ if options.has_key?('debug') && boolify(options['debug']).nil?
94
+ errors.add(:base, "if provided, debug must be true or false")
95
+ end
96
+
97
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
98
+ errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working")
99
+ end
100
+ end
101
+
102
+ def working?
103
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
104
+ end
105
+
106
+ def receive(incoming_events)
107
+ incoming_events.each do |event|
108
+ interpolate_with(event) do
109
+ log event
110
+ trigger_action
111
+ end
112
+ end
113
+ end
114
+
115
+ def check
116
+ trigger_action
117
+ end
118
+
119
+ private
120
+
121
+ def log_curl_output(code,body)
122
+
123
+ log "request status : #{code}"
124
+
125
+ if interpolated['debug'] == 'true'
126
+ log "body"
127
+ log body
128
+ end
129
+
130
+ if interpolated['emit_events'] == 'true'
131
+ create_event payload: body
132
+ end
133
+
134
+ end
135
+
136
+ def active_streams()
137
+
138
+ uri = URI.parse("https://api.twitch.tv/helix/streams?user_id=#{interpolated['user_id']}")
139
+ request = Net::HTTP::Get.new(uri)
140
+ request["Authorization"] = "Bearer #{interpolated['access_token']}"
141
+ request["Client-Id"] = "#{interpolated['client_id']}"
142
+
143
+ req_options = {
144
+ use_ssl: uri.scheme == "https",
145
+ }
146
+
147
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
148
+ http.request(request)
149
+ end
150
+
151
+ if interpolated['debug'] == 'true'
152
+ log "response.body"
153
+ log response.body
154
+ end
155
+
156
+ log "request status : #{response.code}"
157
+ payload = JSON.parse(response.body)
158
+ if payload.to_s != memory['last_status']
159
+ if "#{memory['last_status']}" == ''
160
+ payload['data'].each do |stream|
161
+ log_curl_output(response.code,stream)
162
+ end
163
+ else
164
+ last_status = memory['last_status'].gsub("=>", ": ").gsub(": nil,", ": null,")
165
+ last_status = JSON.parse(last_status)
166
+ payload['data'].each do |stream|
167
+ found = false
168
+ if interpolated['debug'] == 'true'
169
+ log "stream"
170
+ log stream
171
+ end
172
+ last_status['data'].each do |streambis|
173
+ if stream == streambis
174
+ found = true
175
+ end
176
+ if interpolated['debug'] == 'true'
177
+ log "streambis"
178
+ log streambis
179
+ log "found is #{found}!"
180
+ end
181
+ end
182
+ if found == false
183
+ log_curl_output(response.code,stream)
184
+ end
185
+ end
186
+ end
187
+ memory['last_status'] = payload.to_s
188
+ end
189
+ end
190
+
191
+ def get_user_informations()
192
+
193
+ uri = URI.parse("https://api.twitch.tv/helix/users?id=#{interpolated['user_id']}")
194
+ request = Net::HTTP::Get.new(uri)
195
+ request["Authorization"] = "Bearer #{interpolated['access_token']}"
196
+ request["Client-Id"] = "#{interpolated['client_id']}"
197
+
198
+ req_options = {
199
+ use_ssl: uri.scheme == "https",
200
+ }
201
+
202
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
203
+ http.request(request)
204
+ end
205
+
206
+ log_curl_output(response.code,response.body)
207
+
208
+ end
209
+
210
+ def trigger_action
211
+
212
+ case interpolated['type']
213
+ when "get_user_informations"
214
+ get_user_informations()
215
+ when "active_streams"
216
+ active_streams()
217
+ else
218
+ log "Error: type has an invalid value (#{interpolated['type']})"
219
+ end
220
+ end
221
+ end
222
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_twitch_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_twitch_agent/twitch_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::TwitchAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::TwitchAgent.new.default_options
7
+ @checker = Agents::TwitchAgent.new(:name => "TwitchAgent", :options => @valid_options)
8
+ @checker.user = users(:bob)
9
+ @checker.save!
10
+ end
11
+
12
+ pending "add specs here"
13
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_twitch_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-04-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - ngermain@hihouhou.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - lib/huginn_twitch_agent.rb
64
+ - lib/huginn_twitch_agent/twitch_agent.rb
65
+ - spec/twitch_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_twitch_agent
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.1.6
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Write a short summary, because Rubygems requires one.
89
+ test_files:
90
+ - spec/twitch_agent_spec.rb