huginn_reddit_agent 0.1.11
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 +7 -0
- data/LICENSE.txt +7 -0
- data/lib/huginn_reddit_agent/reddit_agent.rb +311 -0
- data/lib/huginn_reddit_agent.rb +4 -0
- data/spec/reddit_agent_spec.rb +13 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 725e1a3245b9ee68ffa38a10e7f56dc97b3d9ec9f23bb1c27191ac60cbe58e3f
|
4
|
+
data.tar.gz: e4fef1c4e0a3800c05865a6dda9472ac3add9a5077b2598f131819c450f1d1b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4d33330f9f0864f786c4ddb78cd8c16528a8c98707073c68c8e46d321765f8eed97b99fc7f6007eb6aeffa74885b6066cdebec8cacc7c7ed531f42acb7f0bf03
|
7
|
+
data.tar.gz: dfe69966507362cae6ee79265432a447f536acacd4fec4f01275f1e155c358505081a501a3633d627cfcd3ffa5f520b6d587bd563cc2cd347435ddef8d8767c7
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2023 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,311 @@
|
|
1
|
+
module Agents
|
2
|
+
class RedditAgent < Agent
|
3
|
+
include FormConfigurable
|
4
|
+
can_dry_run!
|
5
|
+
no_bulk_receive!
|
6
|
+
default_schedule 'every_1h'
|
7
|
+
|
8
|
+
description do
|
9
|
+
<<-MD
|
10
|
+
The Reddit Agent interacts with Reddit API.
|
11
|
+
|
12
|
+
`debug` is used for verbose mode.
|
13
|
+
|
14
|
+
`subreddit` is for hottest post (use space to separate subreddits).
|
15
|
+
|
16
|
+
`username` is mandatory for auth endpoints.
|
17
|
+
|
18
|
+
`password` is mandatory for auth endpoints.
|
19
|
+
|
20
|
+
`bearer_token` is mandatory for authentication (to avoid issue when refresh is needed, continue to use the default value for this field).
|
21
|
+
|
22
|
+
`client_id` is mandatory for auth endpoints.
|
23
|
+
|
24
|
+
`secret_key` is mandatory for auth endpoints.
|
25
|
+
|
26
|
+
`type` is for the wanted action like read_unreadmessage.
|
27
|
+
|
28
|
+
`expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
|
29
|
+
that you anticipate passing without this Agent receiving an incoming Event.
|
30
|
+
MD
|
31
|
+
end
|
32
|
+
|
33
|
+
event_description <<-MD
|
34
|
+
Events look like this:
|
35
|
+
|
36
|
+
{
|
37
|
+
"kind": "t4",
|
38
|
+
"data": {
|
39
|
+
"first_message": XXXXXXXXXX,
|
40
|
+
"first_message_name": "XXXXXXXXXX",
|
41
|
+
"subreddit": "huginn",
|
42
|
+
"likes": null,
|
43
|
+
"replies": "",
|
44
|
+
"author_fullname": null,
|
45
|
+
"id": "XXXXXXX",
|
46
|
+
"subject": "re: [join] I would like to join huginn",
|
47
|
+
"associated_awarding_id": null,
|
48
|
+
"score": 0,
|
49
|
+
"author": null,
|
50
|
+
"num_comments": null,
|
51
|
+
"parent_id": "XXXXXXXXXX",
|
52
|
+
"subreddit_name_prefixed": "r/huginn",
|
53
|
+
"new": true,
|
54
|
+
"type": "unknown",
|
55
|
+
"body": "You're approved - welcome to the sub!",
|
56
|
+
"dest": "hihouhou",
|
57
|
+
"was_comment": false,
|
58
|
+
"body_html": "<!-- SC_OFF --><div class=\"md\"><p>You&#39;re approved - welcome to the sub!</p>\n</div><!-- SC_ON -->",
|
59
|
+
"name": "t4_1vxz7ox",
|
60
|
+
"created": 1686931931,
|
61
|
+
"created_utc": 1686931931,
|
62
|
+
"context": "",
|
63
|
+
"distinguished": "moderator"
|
64
|
+
}
|
65
|
+
}
|
66
|
+
MD
|
67
|
+
|
68
|
+
def default_options
|
69
|
+
{
|
70
|
+
'type' => 'read_unreadmessage',
|
71
|
+
'debug' => 'false',
|
72
|
+
'password' => '',
|
73
|
+
'username' => '',
|
74
|
+
'client_id' => '',
|
75
|
+
'secret_key' => '',
|
76
|
+
'bearer_token' => '{% credential reddit_bearer_token %}',
|
77
|
+
'subreddit' => '',
|
78
|
+
'limit' => '',
|
79
|
+
'emit_events' => 'true',
|
80
|
+
'expected_receive_period_in_days' => '2',
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
form_configurable :type, type: :array, values: ['read_unreadmessage', 'hottest_post_subreddit']
|
85
|
+
form_configurable :username, type: :string
|
86
|
+
form_configurable :password, type: :string
|
87
|
+
form_configurable :client_id, type: :string
|
88
|
+
form_configurable :secret_key, type: :string
|
89
|
+
form_configurable :bearer_token, type: :string
|
90
|
+
form_configurable :limit, type: :string
|
91
|
+
form_configurable :subreddit, type: :string
|
92
|
+
form_configurable :debug, type: :boolean
|
93
|
+
form_configurable :emit_events, type: :boolean
|
94
|
+
form_configurable :expected_receive_period_in_days, type: :string
|
95
|
+
def validate_options
|
96
|
+
errors.add(:base, "type has invalid value: should be 'read_unreadmessage' 'hottest_post_subreddit'") if interpolated['type'].present? && !%w(read_unreadmessage hottest_post_subreddit).include?(interpolated['type'])
|
97
|
+
|
98
|
+
unless options['subreddit'].present? || !['hottest_post_subreddit'].include?(options['type'])
|
99
|
+
errors.add(:base, "subreddit is a required field")
|
100
|
+
end
|
101
|
+
|
102
|
+
unless options['password'].present? || !['read_unreadmessage', 'hottest_post_subreddit'].include?(options['type'])
|
103
|
+
errors.add(:base, "password is a required field")
|
104
|
+
end
|
105
|
+
|
106
|
+
unless options['username'].present? || !['read_unreadmessage', 'hottest_post_subreddit'].include?(options['type'])
|
107
|
+
errors.add(:base, "username is a required field")
|
108
|
+
end
|
109
|
+
|
110
|
+
unless options['client_id'].present? || !['read_unreadmessage', 'hottest_post_subreddit'].include?(options['type'])
|
111
|
+
errors.add(:base, "client_id is a required field")
|
112
|
+
end
|
113
|
+
|
114
|
+
unless options['secret_key'].present? || !['read_unreadmessage', 'hottest_post_subreddit'].include?(options['type'])
|
115
|
+
errors.add(:base, "secret_key is a required field")
|
116
|
+
end
|
117
|
+
|
118
|
+
unless options['bearer_token'].present?
|
119
|
+
errors.add(:base, "bearer_token is a required field")
|
120
|
+
end
|
121
|
+
|
122
|
+
unless options['limit'].present? || !['hottest_post_subreddit'].include?(options['type'])
|
123
|
+
errors.add(:base, "limit is a required field")
|
124
|
+
end
|
125
|
+
|
126
|
+
if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
|
127
|
+
errors.add(:base, "if provided, emit_events must be true or false")
|
128
|
+
end
|
129
|
+
|
130
|
+
if options.has_key?('details') && boolify(options['details']).nil?
|
131
|
+
errors.add(:base, "if provided, details must be true or false")
|
132
|
+
end
|
133
|
+
|
134
|
+
if options.has_key?('debug') && boolify(options['debug']).nil?
|
135
|
+
errors.add(:base, "if provided, debug must be true or false")
|
136
|
+
end
|
137
|
+
|
138
|
+
unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
|
139
|
+
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")
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def working?
|
144
|
+
event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
|
145
|
+
end
|
146
|
+
|
147
|
+
def receive(incoming_events)
|
148
|
+
incoming_events.each do |event|
|
149
|
+
interpolate_with(event) do
|
150
|
+
log event
|
151
|
+
trigger_action
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def check
|
157
|
+
trigger_action
|
158
|
+
end
|
159
|
+
|
160
|
+
private
|
161
|
+
|
162
|
+
def set_credential(name, value)
|
163
|
+
c = user.user_credentials.find_or_initialize_by(credential_name: name)
|
164
|
+
c.credential_value = value
|
165
|
+
c.save!
|
166
|
+
end
|
167
|
+
|
168
|
+
def log_curl_output(code,body)
|
169
|
+
|
170
|
+
log "request status : #{code}"
|
171
|
+
|
172
|
+
if interpolated['debug'] == 'true'
|
173
|
+
log "body"
|
174
|
+
log body
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
def check_token_validity()
|
180
|
+
|
181
|
+
if memory['expires_at'].nil?
|
182
|
+
token_refresh()
|
183
|
+
else
|
184
|
+
timestamp_to_compare = memory['expires_at']
|
185
|
+
current_timestamp = Time.now.to_i
|
186
|
+
difference_in_hours = (timestamp_to_compare - current_timestamp) / 3600.0
|
187
|
+
if difference_in_hours < 2
|
188
|
+
token_refresh()
|
189
|
+
else
|
190
|
+
log "refresh not needed"
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def token_refresh()
|
196
|
+
url = URI('https://ssl.reddit.com/api/v1/access_token')
|
197
|
+
http = Net::HTTP.new(url.host, url.port)
|
198
|
+
http.use_ssl = true
|
199
|
+
|
200
|
+
request = Net::HTTP::Post.new(url)
|
201
|
+
request.basic_auth(interpolated['client_id'], interpolated['secret_key'])
|
202
|
+
request.set_form_data(
|
203
|
+
'grant_type' => 'password',
|
204
|
+
'username' => interpolated['username'],
|
205
|
+
'password' => interpolated['password']
|
206
|
+
)
|
207
|
+
|
208
|
+
response = http.request(request)
|
209
|
+
log_curl_output(response.code,response.body)
|
210
|
+
|
211
|
+
if response.is_a?(Net::HTTPSuccess)
|
212
|
+
payload = JSON.parse(response.body)
|
213
|
+
if interpolated['bearer_token'] != payload['access_token']
|
214
|
+
set_credential("reddit_bearer_token", payload['access_token'])
|
215
|
+
if interpolated['debug'] == 'true'
|
216
|
+
log "reddit_bearer_token credential updated"
|
217
|
+
end
|
218
|
+
end
|
219
|
+
memory['expires_at'] = payload['expires_in'] + Time.now.to_i
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
def read_all_messages(base_url,token)
|
224
|
+
|
225
|
+
check_token_validity()
|
226
|
+
uri = URI.parse("#{base_url}/api/read_all_messages")
|
227
|
+
request = Net::HTTP::Post.new(uri)
|
228
|
+
request["Authorization"] = "Bearer #{interpolated['bearer_token']}"
|
229
|
+
request["User-Agent"] = "huginn/1"
|
230
|
+
|
231
|
+
req_options = {
|
232
|
+
use_ssl: uri.scheme == "https",
|
233
|
+
}
|
234
|
+
|
235
|
+
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
236
|
+
http.request(request)
|
237
|
+
end
|
238
|
+
|
239
|
+
log_curl_output(response.code,response.body)
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
def fetch_hottest_post(base_url,subreddit)
|
244
|
+
url = URI("https://www.reddit.com/r/#{subreddit}/hot.json?limit=#{interpolated['limit']}")
|
245
|
+
req = Net::HTTP::Get.new(url)
|
246
|
+
req["User-Agent"] = "huginn/1"
|
247
|
+
|
248
|
+
res = Net::HTTP.start(url.hostname, url.port, use_ssl: true) do |http|
|
249
|
+
http.request(req)
|
250
|
+
end
|
251
|
+
|
252
|
+
log_curl_output(res.code,res.body)
|
253
|
+
|
254
|
+
payload = JSON.parse(res.body)
|
255
|
+
payload['data']['children'].each do |message|
|
256
|
+
create_event payload: message
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
260
|
+
|
261
|
+
def check_hottest_post_subreddit(base_url)
|
262
|
+
subreddit_list = interpolated['subreddit'].split(" ")
|
263
|
+
subreddit_list.each do |wanted_subreddit|
|
264
|
+
fetch_hottest_post(base_url,wanted_subreddit)
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|
268
|
+
|
269
|
+
def check_unreadmessage(base_url)
|
270
|
+
|
271
|
+
check_token_validity()
|
272
|
+
uri = URI.parse("#{base_url}/message/unread")
|
273
|
+
request = Net::HTTP::Get.new(uri)
|
274
|
+
request["Authorization"] = "Bearer #{interpolated['bearer_token']}"
|
275
|
+
request["User-Agent"] = "huginn/1"
|
276
|
+
|
277
|
+
req_options = {
|
278
|
+
use_ssl: uri.scheme == "https",
|
279
|
+
}
|
280
|
+
|
281
|
+
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
282
|
+
http.request(request)
|
283
|
+
end
|
284
|
+
|
285
|
+
log_curl_output(response.code,response.body)
|
286
|
+
|
287
|
+
payload = JSON.parse(response.body)
|
288
|
+
payload['data']['children'].each do |message|
|
289
|
+
if interpolated['emit_events'] == 'true'
|
290
|
+
create_event payload: message
|
291
|
+
end
|
292
|
+
end
|
293
|
+
if !payload['data']['children'].empty?
|
294
|
+
read_all_messages(base_url,token)
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
def trigger_action
|
299
|
+
|
300
|
+
base_url = 'https://oauth.reddit.com'
|
301
|
+
case interpolated['type']
|
302
|
+
when "read_unreadmessage"
|
303
|
+
check_unreadmessage(base_url)
|
304
|
+
when "hottest_post_subreddit"
|
305
|
+
check_hottest_post_subreddit(base_url)
|
306
|
+
else
|
307
|
+
log "Error: type has an invalid value (#{interpolated['type']})"
|
308
|
+
end
|
309
|
+
end
|
310
|
+
end
|
311
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
require 'huginn_agent/spec_helper'
|
3
|
+
|
4
|
+
describe Agents::RedditAgent do
|
5
|
+
before(:each) do
|
6
|
+
@valid_options = Agents::RedditAgent.new.default_options
|
7
|
+
@checker = Agents::RedditAgent.new(:name => "RedditAgent", :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_reddit_agent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.11
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicolas Germain
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-01-22 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_reddit_agent.rb
|
64
|
+
- lib/huginn_reddit_agent/reddit_agent.rb
|
65
|
+
- spec/reddit_agent_spec.rb
|
66
|
+
homepage: https://github.com/hihouhou/huginn_reddit_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.3.3
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Write a short summary, because Rubygems requires one.
|
89
|
+
test_files:
|
90
|
+
- spec/reddit_agent_spec.rb
|