huginn_twitch_agent 0.1 → 0.1.10

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: f1cc82e7e80cc2e2aa423f0243c01d9e919e1039a0523a2f49419d219496986a
4
- data.tar.gz: bb675dcce1e88d61650dcf9f0548d5c174db25d9c47cb4aeda32ed283814c45a
3
+ metadata.gz: b234df5a2bdef0249a384baa501336d780e8f7a95a60cb7dcd7ac8cf21d6a589
4
+ data.tar.gz: aa13278c00cb19c23afdae6479e0adb0b9800e1300f2c817468f8bc903c90cb6
5
5
  SHA512:
6
- metadata.gz: 3052680728fe48f0a355c9ef3f5d09fee0cea334cd5729f21185a1e00de464fbec247290ffceba1dc9e003c62b8c03b965df51aaba8c1351e71e5e2b4beda303
7
- data.tar.gz: 3b9322ebb8a64aaaebeac4356ac408d6cf29938f4fe9ab86ec3f5dad8d7e6f5345a5e95d34b49b1e502a6defcc295a2062446dcdb6360c1b3903060f127cc30c
6
+ metadata.gz: 7ebeb2c38b502e63f46ad1cec46ad0e351f6577d858aba2213c18dce916975b96d5ae8aad9b2225f916ab0355ae9d6c8083643e15e625ed190c7c34f559f0592
7
+ data.tar.gz: aaace31486dae0fbf08272c96aba9df8ddc6e6aece20f6caac3d182427c075f35e2f90ec106c369df64e29190a1f469ba71826f439bfea9c97f7e3b2475152c9
@@ -7,7 +7,7 @@ module Agents
7
7
 
8
8
  description do
9
9
  <<-MD
10
- The Twitch Agent is able to find information about users, checking live steam, others.
10
+ The Twitch Agent is able to find information about users, checking live stream, others.
11
11
 
12
12
  `debug` is used for verbose mode.
13
13
 
@@ -49,10 +49,10 @@ module Agents
49
49
 
50
50
  def default_options
51
51
  {
52
- 'client_id' => '',
53
52
  'user_id' => '',
54
- 'client_secret' => '',
55
- 'access_token' => '',
53
+ 'client_id' => '{% credential twitch_client_id %}',
54
+ 'client_secret' => '{% credential twitch_client_secret %}',
55
+ 'access_token' => '{% credential twitch_access_token %}',
56
56
  'debug' => 'false',
57
57
  'emit_events' => 'false',
58
58
  'expected_receive_period_in_days' => '2',
@@ -118,6 +118,12 @@ module Agents
118
118
 
119
119
  private
120
120
 
121
+ def set_credential(name, value)
122
+ c = user.user_credentials.find_or_initialize_by(credential_name: name)
123
+ c.credential_value = value
124
+ c.save!
125
+ end
126
+
121
127
  def log_curl_output(code,body)
122
128
 
123
129
  log "request status : #{code}"
@@ -127,69 +133,105 @@ module Agents
127
133
  log body
128
134
  end
129
135
 
130
- if interpolated['emit_events'] == 'true'
131
- create_event payload: body
136
+ end
137
+
138
+ def token_refresh()
139
+
140
+ uri = URI.parse("https://id.twitch.tv/oauth2/token?&client_id=#{interpolated['client_id']}&client_secret=#{interpolated['client_secret']}&grant_type=client_credentials")
141
+ request = Net::HTTP::Post.new(uri)
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
+ log_curl_output(response.code,response.body)
152
+
153
+ payload = JSON.parse(response.body)
154
+ if interpolated['access_token'] != payload['access_token']
155
+ set_credential("twitch_access_token", payload['access_token'])
156
+ if interpolated['debug'] == 'true'
157
+ log "twitch_access_token credential updated"
158
+ end
132
159
  end
160
+ current_timestamp = Time.now.to_i
161
+ memory['expires_at'] = payload['expires_in'] + current_timestamp
133
162
 
134
163
  end
135
164
 
165
+ def check_token_validity()
166
+
167
+ if memory['expires_at'].nil?
168
+ token_refresh()
169
+ else
170
+ timestamp_to_compare = memory['expires_at']
171
+ current_timestamp = Time.now.to_i
172
+ difference_in_hours = (timestamp_to_compare - current_timestamp) / 3600.0
173
+ if difference_in_hours < 2
174
+ token_refresh()
175
+ else
176
+ log "refresh not needed"
177
+ end
178
+ end
179
+ end
180
+
136
181
  def active_streams()
137
182
 
183
+ check_token_validity()
138
184
  uri = URI.parse("https://api.twitch.tv/helix/streams?user_id=#{interpolated['user_id']}")
139
185
  request = Net::HTTP::Get.new(uri)
140
186
  request["Authorization"] = "Bearer #{interpolated['access_token']}"
141
187
  request["Client-Id"] = "#{interpolated['client_id']}"
142
-
188
+
143
189
  req_options = {
144
190
  use_ssl: uri.scheme == "https",
145
191
  }
146
-
192
+
147
193
  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
148
194
  http.request(request)
149
195
  end
150
196
 
151
- if interpolated['debug'] == 'true'
152
- log "response.body"
153
- log response.body
154
- end
197
+ log_curl_output(response.code,response.body)
155
198
 
156
- log "request status : #{response.code}"
157
199
  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
200
+ if payload != memory['last_status']
201
+ payload['data'].each do |stream|
202
+ found = false
203
+ if !memory['last_status'].nil? and memory['last_status'].present?
204
+ last_status = memory['last_status']
168
205
  if interpolated['debug'] == 'true'
169
206
  log "stream"
170
207
  log stream
171
208
  end
172
209
  last_status['data'].each do |streambis|
173
- if stream == streambis
210
+ if stream['started_at'] == streambis['started_at']
174
211
  found = true
175
212
  end
176
213
  if interpolated['debug'] == 'true'
214
+ log "found is #{found}!"
177
215
  log "streambis"
178
216
  log streambis
179
- log "found is #{found}!"
180
217
  end
181
218
  end
182
- if found == false
183
- log_curl_output(response.code,stream)
219
+ end
220
+ if found == false
221
+ create_event payload: stream
222
+ else
223
+ if interpolated['debug'] == 'true'
224
+ log "found is #{found}"
184
225
  end
185
226
  end
186
227
  end
187
- memory['last_status'] = payload.to_s
228
+ memory['last_status'] = payload
188
229
  end
189
230
  end
190
231
 
191
232
  def get_user_informations()
192
-
233
+
234
+ check_token_validity()
193
235
  uri = URI.parse("https://api.twitch.tv/helix/users?id=#{interpolated['user_id']}")
194
236
  request = Net::HTTP::Get.new(uri)
195
237
  request["Authorization"] = "Bearer #{interpolated['access_token']}"
@@ -198,13 +240,25 @@ module Agents
198
240
  req_options = {
199
241
  use_ssl: uri.scheme == "https",
200
242
  }
201
-
243
+
202
244
  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
203
245
  http.request(request)
204
246
  end
205
247
 
206
248
  log_curl_output(response.code,response.body)
207
249
 
250
+ payload = JSON.parse(response.body)
251
+
252
+ if payload != memory['last_status']
253
+ if interpolated['emit_events'] == 'true'
254
+ create_event payload: payload
255
+ end
256
+ memory['last_status'] = payload
257
+ else
258
+ if interpolated['debug'] == 'true'
259
+ log "no diff"
260
+ end
261
+ end
208
262
  end
209
263
 
210
264
  def trigger_action
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huginn_twitch_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Germain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-30 00:00:00.000000000 Z
11
+ date: 2023-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  requirements: []
85
- rubygems_version: 3.1.6
85
+ rubygems_version: 3.3.3
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: Write a short summary, because Rubygems requires one.