reddit_bot 1.6.5 → 1.6.6
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 +4 -4
- data/README.md +1 -0
- data/lib/reddit_bot/version.rb +1 -1
- data/lib/reddit_bot.rb +22 -17
- data/reddit_bot.gemspec +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fb0c92530c0139f3abcf98680613b118ab21eeb8
|
|
4
|
+
data.tar.gz: 68ff542161f8ea0e73bbeb0ca3ecd2a3fef6d618
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6f162ba96a47e7ea121f26dc2a1e4a79a23c5448355fdf931b6c518216881758aa527880957e608f8b356a1340dd00999154440678105517b44540d893e4e134
|
|
7
|
+
data.tar.gz: 9143a8d2ba251a0871097ab726eab1fdae48449a678363ae1c58c73a98baa204cb7f12d5b325bdc4d87a561f7bbf8e2c100540b96dee32681f8ed82b7034148b
|
data/README.md
CHANGED
|
@@ -74,6 +74,7 @@ You obviously can't run these examples as is, because they use some dependencies
|
|
|
74
74
|
:client_secret: Fqo.....................AFI
|
|
75
75
|
:password: mybotpassword
|
|
76
76
|
:login: MyBotUsername
|
|
77
|
+
# :user_agent: optional_custom_useragent_to_bypass_reddit_spam_protection
|
|
77
78
|
|
|
78
79
|
To update the gem version in Gemfile.lock when using Gemfile like this: `gem "reddit_bot", "~>1.1.0"`, do the:
|
|
79
80
|
|
data/lib/reddit_bot/version.rb
CHANGED
data/lib/reddit_bot.rb
CHANGED
|
@@ -17,8 +17,7 @@ module RedditBot
|
|
|
17
17
|
# [secrets] +Hash+ with keys :client_id, :client_secret, :password: and :login
|
|
18
18
|
# [kwargs] keyword params may include :subreddit for clever methods
|
|
19
19
|
def initialize secrets, **kwargs
|
|
20
|
-
@
|
|
21
|
-
@name = secrets[:login]
|
|
20
|
+
@name, @secret_password, @user_agent, *@secret_auth = secrets.values_at *%i{ login password user_agent client_id client_secret }
|
|
22
21
|
# @ignore_captcha = true
|
|
23
22
|
# @ignore_captcha = kwargs[:ignore_captcha] if kwargs.has_key?(:ignore_captcha)
|
|
24
23
|
@subreddit = kwargs[:subreddit]
|
|
@@ -30,11 +29,12 @@ module RedditBot
|
|
|
30
29
|
def json mtd, path, _form = []
|
|
31
30
|
form = Hash[_form]
|
|
32
31
|
response = JSON.parse resp_with_token mtd, path, form.merge({api_type: "json"})
|
|
33
|
-
if response.is_a?(Hash) && response["json"] # for example, flairlist.json and {"error": 403} do not have it
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
response["json"]["errors"].
|
|
37
|
-
|
|
32
|
+
if response.is_a?(Hash) && response["json"] && # for example, flairlist.json and {"error": 403} do not have it
|
|
33
|
+
!response["json"]["errors"].empty?
|
|
34
|
+
puts "ERROR OCCURED on #{[mtd, path]}"
|
|
35
|
+
fail "unknown how to handle multiple errors" if 1 < response["json"]["errors"].size
|
|
36
|
+
puts "error: #{response["json"]["errors"]}"
|
|
37
|
+
error, description = response["json"]["errors"].first
|
|
38
38
|
case error
|
|
39
39
|
when "ALREADY_SUB" ; puts "was rejected by moderator if you didn't see in dups"
|
|
40
40
|
# when "BAD_CAPTCHA" ; update_captcha
|
|
@@ -42,9 +42,13 @@ module RedditBot
|
|
|
42
42
|
# iden: @iden_and_captcha[0],
|
|
43
43
|
# captcha: @iden_and_captcha[1],
|
|
44
44
|
# } ) unless @ignore_captcha
|
|
45
|
+
when "RATELIMIT"
|
|
46
|
+
fail error unless description[/\Ayou are doing that too much\. try again in (\d) minutes\.\z/]
|
|
47
|
+
puts "retrying in #{$1.to_i + 1} minutes"
|
|
48
|
+
sleep ($1.to_i + 1) * 60
|
|
49
|
+
return json mtd, path, _form
|
|
45
50
|
else ; fail error
|
|
46
51
|
end
|
|
47
|
-
end
|
|
48
52
|
end
|
|
49
53
|
response
|
|
50
54
|
end
|
|
@@ -163,16 +167,17 @@ module RedditBot
|
|
|
163
167
|
|
|
164
168
|
def token
|
|
165
169
|
return @token_cached if @token_cached
|
|
170
|
+
# TODO handle with nive error message if we get 403 -- it's probably because of bad user agent
|
|
166
171
|
response = JSON.parse reddit_resp :post,
|
|
167
172
|
"https://www.reddit.com/api/v1/access_token", {
|
|
168
173
|
grant_type: "password",
|
|
169
|
-
username: @
|
|
170
|
-
password: @
|
|
174
|
+
username: @name,
|
|
175
|
+
password: @secret_password,
|
|
171
176
|
}, {
|
|
172
|
-
"User-Agent" => "bot/#{@
|
|
173
|
-
},
|
|
177
|
+
"User-Agent" => "bot/#{@user_agent || @name}/#{RedditBot::VERSION} by /u/nakilon",
|
|
178
|
+
}, @secret_auth
|
|
174
179
|
unless @token_cached = response["access_token"]
|
|
175
|
-
fail "bot #{@
|
|
180
|
+
fail "bot #{@name} isn't a 'developer' of app at https://www.reddit.com/prefs/apps/" if response == {"error"=>"invalid_grant"}
|
|
176
181
|
fail response.inspect
|
|
177
182
|
end
|
|
178
183
|
puts "new token is: #{@token_cached}"
|
|
@@ -195,10 +200,10 @@ module RedditBot
|
|
|
195
200
|
begin
|
|
196
201
|
reddit_resp mtd, "https://oauth.reddit.com" + path, form, {
|
|
197
202
|
"Authorization" => "bearer #{token}",
|
|
198
|
-
"User-Agent" => "bot/#{@
|
|
203
|
+
"User-Agent" => "bot/#{@user_agent || @name}/#{RedditBot::VERSION} by /u/nakilon",
|
|
199
204
|
}
|
|
200
205
|
rescue NetHTTPUtils::Error => e
|
|
201
|
-
|
|
206
|
+
sleep 5
|
|
202
207
|
raise unless e.code == 401
|
|
203
208
|
@token_cached = nil
|
|
204
209
|
retry
|
|
@@ -206,9 +211,9 @@ module RedditBot
|
|
|
206
211
|
end
|
|
207
212
|
|
|
208
213
|
def reddit_resp *args
|
|
209
|
-
mtd, url, form, headers,
|
|
214
|
+
mtd, url, form, headers, basic_auth = *args
|
|
210
215
|
begin
|
|
211
|
-
NetHTTPUtils.request_data(url, mtd, form: form, header: headers, auth:
|
|
216
|
+
NetHTTPUtils.request_data(url, mtd, form: form, header: headers, auth: basic_auth) do |response|
|
|
212
217
|
next unless remaining = response.to_hash["x-ratelimit-remaining"]
|
|
213
218
|
if Gem::Platform.local.os == "darwin"
|
|
214
219
|
puts %w{
|
data/reddit_bot.gemspec
CHANGED
|
@@ -15,6 +15,7 @@ Gem::Specification.new do |spec|
|
|
|
15
15
|
# spec.require_paths = ["lib"]
|
|
16
16
|
|
|
17
17
|
spec.add_runtime_dependency "json"
|
|
18
|
+
spec.add_runtime_dependency "nethttputils", "~> 0.2.4.1"
|
|
18
19
|
# spec.add_development_dependency "bundler", "~> 1.11"
|
|
19
20
|
# spec.add_development_dependency "rake", "~> 10.0"
|
|
20
21
|
# spec.add_development_dependency "rspec", "~> 3.0"
|
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.6.
|
|
4
|
+
version: 1.6.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Victor Maslov
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2018-06-
|
|
11
|
+
date: 2018-06-14 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: json
|
|
@@ -24,6 +24,20 @@ dependencies:
|
|
|
24
24
|
- - ">="
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: nethttputils
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 0.2.4.1
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 0.2.4.1
|
|
27
41
|
description: better than PRAW
|
|
28
42
|
email:
|
|
29
43
|
- nakilon@gmail.com
|