reddit_bot 1.8.0 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/reddit_bot.rb +27 -18
  3. data/reddit_bot.gemspec +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56b982ca68fe28189b57f07c597c68d0db80541f
4
- data.tar.gz: f25148fd86d9cc67f93651b16298d1d902bb0567
3
+ metadata.gz: 99ebebd30c2e4bfa221cc6b78c6521f4eb2e5c7b
4
+ data.tar.gz: 79b8d91f910d5ca649f8350f1009bb6c21afd702
5
5
  SHA512:
6
- metadata.gz: fbdd04f798cc039e66a32639bf4949887de5b9875c57ff5f63291730ca903afb2437b91440e7eb8449374589249d3dd4808cbc29385ffbdb65c4faa509d01b65
7
- data.tar.gz: 1125779a8c6a4aa55ba98ecb50f6b54ebfd3c4496b983adc09a575aba247d98c6c4583208c5668b58aa86014bf5064e2f24afc150bb4befd8e8b4dd8ca8c48a9
6
+ metadata.gz: 9049548db4c55006a9fd9dd7fef394cb3b973ea3df7047a25528dcd2495e230acaa8217fd17ffa8128e9f23784bd292875d6e01015c785d465c3acc7c51db0b9
7
+ data.tar.gz: 6e9849c6d4d3c7ed2eacc8b404f256764e0756e773d567ab7004d7f0a041bbdb5b722106d4fbba281b1ef0f8b6898d551b7742b19a232b942058523e8235a669
data/lib/reddit_bot.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  STDOUT.sync = true
2
- # require "pp"
3
2
 
4
3
  require "openssl"
5
4
  require "json"
@@ -15,12 +14,8 @@ module RedditBot
15
14
  self.logger = Logger.new STDOUT
16
15
 
17
16
  class Bot
18
-
19
- # bot's Reddit username; set via constructor parameter secrets[:login]
20
17
  attr_reader :name
21
18
 
22
- # [secrets] +Hash+ with keys :client_id, :client_secret, :password: and :login
23
- # [kwargs] keyword params may include :subreddit for clever methods
24
19
  def initialize secrets, **kwargs
25
20
  @name, @secret_password, @user_agent, *@secret_auth = secrets.values_at *%i{ login password user_agent client_id client_secret }
26
21
  # @ignore_captcha = true
@@ -28,12 +23,14 @@ module RedditBot
28
23
  @subreddit = kwargs[:subreddit]
29
24
  end
30
25
 
31
- # [mtd] +Symbol+ :get or :post
32
- # [path] +String+ an API method
33
- # [_form] +Array+ or +Hash+ API method params
34
26
  def json mtd, path, _form = []
35
27
  form = Hash[_form]
36
- response = JSON.load resp_with_token mtd, path, form.merge({api_type: "json"})
28
+ response = begin
29
+ JSON.load resp_with_token mtd, path, form.merge({api_type: "json"})
30
+ rescue JSON::ParserError
31
+ $!.message.slice! 1000..-1
32
+ raise
33
+ end
37
34
  if response.is_a?(Hash) && response["json"] && # for example, flairlist.json and {"error": 403} do not have it
38
35
  !response["json"]["errors"].empty?
39
36
  Module.nesting[1].logger.error "ERROR OCCURED on #{[mtd, path]}"
@@ -70,8 +67,6 @@ module RedditBot
70
67
  # # ["previous", result["data"]["children"].last["id"]],
71
68
  # end
72
69
 
73
- # [reason] :nodoc:
74
- # [thing_id] +String+ fullname of a "link, commenr or message"
75
70
  def report reason, thing_id
76
71
  Module.nesting[1].logger.warn "reporting '#{thing_id}'"
77
72
  json :post, "/api/report",
@@ -80,9 +75,6 @@ module RedditBot
80
75
  thing_id: thing_id
81
76
  end
82
77
 
83
- # [post] JSON object of a post of self.post
84
- # [link_flair_css_class] :nodoc:
85
- # [link_flair_text] :nodoc:
86
78
  def set_post_flair post, link_flair_css_class, link_flair_text
87
79
  Module.nesting[1].logger.warn "setting flair '#{link_flair_css_class}' with text '#{link_flair_text}' to post '#{post["name"]}'"
88
80
  if {"error"=>403} == @flairselector_choices ||= json(:post, "/r/#{@subreddit}/api/flairselector", link: post["name"])
@@ -97,8 +89,6 @@ module RedditBot
97
89
  }["flair_template_id"]
98
90
  end
99
91
 
100
- # [thing_id] +String+ fullname of a post (or self.post?), comment (and private message?)
101
- # [text] :nodoc:
102
92
  def leave_a_comment thing_id, text
103
93
  Module.nesting[1].logger.warn "leaving a comment on '#{thing_id}'"
104
94
  json(:post, "/api/comment",
@@ -164,7 +154,6 @@ module RedditBot
164
154
  end
165
155
  end
166
156
 
167
- # :yields: JSON objects: ["data"] part of post or self.post, top level comment (["children"] element)
168
157
  def each_new_post_with_top_level_comments
169
158
  # TODO add keys assertion like in method above?
170
159
  json(:get, "/r/#{@subreddit}/new")["data"]["children"].each do |post|
@@ -178,7 +167,6 @@ module RedditBot
178
167
  end
179
168
  end
180
169
 
181
- # [article] +String+ ID36 of a post or self.post
182
170
  def each_comment_of_the_post_thread article
183
171
  Enumerator.new do |e|
184
172
  f = lambda do |smth|
@@ -194,6 +182,27 @@ module RedditBot
194
182
  end
195
183
  end
196
184
 
185
+ def subreddit_iterate what, **kwargs
186
+ Enumerator.new do |e|
187
+ after = {}
188
+ loop do
189
+ break unless marker = json(:get, "/r/#{@subreddit}/#{what}", {limit: 100}.merge(after).merge(kwargs)).tap do |result|
190
+ fail if %w{ kind data } != result.keys
191
+ fail if "Listing" != result["kind"]
192
+ fail result["data"].keys.inspect unless result["data"].keys == %w{ after dist modhash whitelist_status children before } ||
193
+ result["data"].keys == %w{ modhash dist children after before }
194
+ result["data"]["children"].each do |post|
195
+ fail "unknown type post['kind']: #{post["kind"]}" unless post["kind"] == "t3"
196
+ e << ( post["data"].tap do |data|
197
+ data["url"] = "https://www.reddit.com" + data["url"] if /\A\/r\/[0-9a-zA-Z_]+\/comments\/[0-9a-z]{5,6}\// =~ data["url"] if data["crosspost_parent"]
198
+ end )
199
+ end
200
+ end["data"]["after"]
201
+ after = {after: marker}
202
+ end
203
+ end
204
+ end
205
+
197
206
  private
198
207
 
199
208
  def token
data/reddit_bot.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "reddit_bot"
3
- spec.version = "1.8.0"
3
+ spec.version = "1.9.0"
4
4
  spec.summary = "Simple library for Reddit bots"
5
5
 
6
6
  spec.author = "Victor Maslov aka Nakilon"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reddit_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov aka Nakilon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-02 00:00:00.000000000 Z
11
+ date: 2021-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json_pure