reddit_bot 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f403237693827c791d79ee8904c9cfb9dc5a5a7
4
- data.tar.gz: a3804dca179ba6cb72455d65f6b50cd1cecd3939
3
+ metadata.gz: 0d2ed897b55ef286ce14857b2d4cfd82c69ed861
4
+ data.tar.gz: 40c30c1b24122c3f964ec2e6d447a08eeeb1a9cb
5
5
  SHA512:
6
- metadata.gz: 601b5f77064340aedbc9264a0f96483705c49302f913af46db8fc3d347ade2222969516804976d6af55dac7cbc3b041e46918c5170b141ebc1b6cf31d05a7d59
7
- data.tar.gz: 906036dc78a34315d0df25958c094e042b4d66ed68f8198004f530eea20df76a8013a33ee7ef67108bf8299a81602dd5ea7d774f92742ecac0b5911353a446db
6
+ metadata.gz: 667e9d3cb500e53e4c3f5eac0374ee6e71e116a02292df4517d90ba6904f642e67785f0e990df0cd3c724dd4cfb972cf8a23c7ac35a8027878c8e7e6d149570c
7
+ data.tar.gz: 8dde680bd8addba2765e8a849660465cc8460c21867ca1b48ad451b00ce51b60310097266bce7d6d155b6b15ed4d7b48a3e9924c36301b0bd152f2ffe30ae9a3
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # RedditBot
2
2
 
3
- [![Join the chat at https://gitter.im/Nakilon/reddit_bot](https://badges.gitter.im/Nakilon/reddit_bot.svg)](https://gitter.im/Nakilon/reddit_bot?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4
-
5
- [![Gem Version](https://badge.fury.io/rb/reddit_bot.svg)](http://badge.fury.io/rb/mll)
3
+ [![Join the chat at https://gitter.im/Nakilon/reddit_bot](https://badges.gitter.im/Nakilon/reddit_bot.svg)](https://gitter.im/Nakilon/reddit_bot)
4
+ [![Gem Version](https://badge.fury.io/rb/reddit_bot.svg)](http://badge.fury.io/rb/reddit_bot)
6
5
 
7
6
  #### What
8
7
 
@@ -26,17 +25,17 @@ The [examples folder](examples) includes:
26
25
  It calculates quality of x-posts from different subreddits based on mods activity (remove/approve).
27
26
  For example, this shows that it would be ok to ignore /r/pics from now:
28
27
 
29
- pics Total: 98 Quality: 19%
30
- wallpapers Total: 69 Quality: 52%
31
- wallpaper Total: 45 Quality: 51%
32
- woahdude Total: 30 Quality: 66%
33
- CityPorn Total: 17 Quality: 82%
34
- FoodPorn Total: 13 Quality: 7%
35
- MapPorn Total: 13 Quality: 46%
36
- SkyPorn Total: 11 Quality: 45%
37
- carporn Total: 11 Quality: 45%
38
- InfrastructurePorn Total: 9 Quality: 77%
39
-
28
+ pics Total: 98 Quality: 19%
29
+ wallpapers Total: 69 Quality: 52%
30
+ wallpaper Total: 45 Quality: 51%
31
+ woahdude Total: 30 Quality: 66%
32
+ CityPorn Total: 17 Quality: 82%
33
+ FoodPorn Total: 13 Quality: 7%
34
+ MapPorn Total: 13 Quality: 46%
35
+ SkyPorn Total: 11 Quality: 45%
36
+ carporn Total: 11 Quality: 45%
37
+ InfrastructurePorn Total: 9 Quality: 77%
38
+
40
39
  You obviously can't run these examples as is, because they have some dependencies that are not in this repo. Like `secrets.yaml` file for authorization of the following format:
41
40
 
42
41
  :client_id: Kb9.......6wBw
@@ -0,0 +1,19 @@
1
+ PATH
2
+ remote: ../..
3
+ specs:
4
+ reddit_bot (1.1.2)
5
+ json
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ json (1.8.3)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ reddit_bot!
17
+
18
+ BUNDLED WITH
19
+ 1.11.2
data/lib/reddit_bot.rb CHANGED
@@ -8,35 +8,38 @@ require "json"
8
8
 
9
9
 
10
10
  module RedditBot
11
- VERSION = "1.1.2"
11
+ VERSION = "1.1.3" # :nodoc:
12
12
 
13
13
  class Bot
14
14
 
15
+ # bot's Reddit username; set via constructor parameter secrets[:login]
16
+ attr_reader :name
17
+
18
+ # [secrets] +Hash+ with keys :client_id, :client_secret, :password: and :login
19
+ # [kwargs] keyword params may include :ignore_captcha that is true by default and :subreddit for clever methods
15
20
  def initialize secrets, **kwargs
16
21
  @secrets = secrets.values_at *%i{ client_id client_secret password login }
22
+ @name = secrets[:login]
17
23
  @ignore_captcha = true
18
24
  @ignore_captcha = kwargs[:ignore_captcha] if kwargs.has_key?(:ignore_captcha)
19
25
  @subreddit = kwargs[:subreddit]
20
26
  end
21
27
 
22
- # attr_accessor :token_cached
23
- # attr_accessor :username
24
- # attr_accessor :iden_and_captcha
25
- # attr_accessor :ignore_captcha
26
- # attr_accessor :secrets
27
-
28
- def json mtd, url, _form = []
28
+ # [mtd] +Symbol+ :get or :post
29
+ # [path] +String+ an API method
30
+ # [_form] +Array+ or +Hash+ API method params
31
+ def json mtd, path, _form = []
29
32
  form = Hash[_form]
30
- response = JSON.parse resp_with_token mtd, url, form.merge({api_type: "json"})
33
+ response = JSON.parse resp_with_token mtd, path, form.merge({api_type: "json"})
31
34
  if response.is_a?(Hash) && response["json"] # for example, flairlist.json and {"error": 403} do not have it
32
- puts "ERROR OCCURED on #{[mtd, url]}" unless response["json"]["errors"].empty?
35
+ puts "ERROR OCCURED on #{[mtd, path]}" unless response["json"]["errors"].empty?
33
36
  # pp response["json"]
34
37
  response["json"]["errors"].each do |error, description|
35
38
  puts "error: #{[error, description]}"
36
39
  case error
37
40
  when "ALREADY_SUB" ; puts "was rejected by moderator if you didn't see in dups"
38
41
  when "BAD_CAPTCHA" ; update_captcha
39
- json mtd, url, form.merger( {
42
+ json mtd, path, form.merger( {
40
43
  iden: @iden_and_captcha[0],
41
44
  captcha: @iden_and_captcha[1],
42
45
  } ) unless @ignore_captcha
@@ -47,7 +50,11 @@ module RedditBot
47
50
  response
48
51
  end
49
52
 
53
+ # [subreddit] +String+ subreddit name without "/r" prefix
54
+ # [page] +String+ page name without "/wiki/" prefix
55
+ # [text] :nodoc:
50
56
  def wiki_edit subreddit, page, text
57
+ puts "editing wiki page '/r/#{subreddit}/wiki/#{page}'"
51
58
  json :post,
52
59
  "/r/#{subreddit}/api/wiki/edit",
53
60
  page: page,
@@ -55,28 +62,48 @@ module RedditBot
55
62
  # ["previous", result["data"]["children"].last["id"]],
56
63
  end
57
64
 
65
+ # [reason] :nodoc:
66
+ # [thing_id] +String+ fullname of a "link, commenr or message"
58
67
  def report reason, thing_id
68
+ puts "reporting '#{thing_id}'"
59
69
  json :post, "/api/report",
60
70
  reason: "other",
61
71
  other_reason: reason,
62
72
  thing_id: thing_id
63
73
  end
64
74
 
65
- # def each_new_post &block
66
- # # Enumerator.new do |e|
67
- # json(:get, "/r/#{@subreddit}/new")["data"]["children"].each do |post|
68
- # fail "unknown type post['kind']: #{post["kind"]}" unless post["kind"] == "t3"
69
- # block.call post["data"]
70
- # end
71
- # end
75
+ # [post] JSON object of a post of self.post
76
+ # [link_flair_css_class] :nodoc:
77
+ # [link_flair_text] :nodoc:
78
+ def set_post_flair post, link_flair_css_class, link_flair_text
79
+ puts "setting flair '#{link_flair_css_class}' with text '#{link_flair_text}' to post '#{post["name"]}'"
80
+ json :post, "/api/selectflair",
81
+ link: post["name"],
82
+ text: link_flair_text,
83
+ flair_template_id: (@flairselector_choices ||=
84
+ json :post, "/r/#{@subreddit}/api/flairselector", link: post["name"]
85
+ )["choices"].find{ |i| i["flair_css_class"] == link_flair_css_class }.tap{ |flair|
86
+ fail "can't find '#{link_flair_css_class}' flair class at https://www.reddit.com/r/#{@subreddit}/about/flair/#link_templates" unless flair
87
+ }["flair_template_id"]
88
+ end
72
89
 
90
+ # [thing_id] +String+ fullname of a post (or self.post?), comment (and private message?)
91
+ # [text] :nodoc:
92
+ def leave_a_comment thing_id, text
93
+ puts "leaving a comment on '#{thing_id}'"
94
+ json(:post, "/api/comment",
95
+ thing_id: thing_id,
96
+ text: text,
97
+ ).tap do |result|
98
+ fail result["json"]["errors"].to_s unless result["json"]["errors"].empty?
99
+ end
100
+ end
101
+
102
+ # :yields: JSON objects: ["data"] part of post or self.post, top level comment (["children"] element)
73
103
  def each_new_post_with_top_level_comments
74
104
  json(:get, "/r/#{@subreddit}/new")["data"]["children"].each do |post|
75
105
  fail "unknown type post['kind']: #{post["kind"]}" unless post["kind"] == "t3"
76
- t = BOT.json :get, "/comments/#{post["data"]["id"]}",
77
- # sort: "top",
78
- depth: 1,
79
- limit: 100500
106
+ t = json :get, "/comments/#{post["data"]["id"]}", depth: 1, limit: 100500#, sort: "top"
80
107
  fail "smth weird about /comments/<id> response" unless t.size == 2
81
108
  yield post["data"], t[1]["data"]["children"].map{ |child|
82
109
  fail "unknown type child['kind']: #{child["kind"]}" unless child["kind"] == "t1"
@@ -85,15 +112,20 @@ module RedditBot
85
112
  end
86
113
  end
87
114
 
88
- def set_post_flair post, link_flair_css_class, link_flair_text
89
- json :post, "/api/selectflair",
90
- link: post["name"],
91
- text: link_flair_text,
92
- flair_template_id: (@flairselector_choices ||=
93
- json :post, "/r/#{@subreddit}/api/flairselector", link: post["name"]
94
- )["choices"].find{ |i| i["flair_css_class"] == link_flair_css_class }.tap{ |flair|
95
- fail "can't find '#{link_flair_css_class}' flair class at https://www.reddit.com/r/#{@subreddit}/about/flair/#link_templates" unless flair
96
- }["flair_template_id"]
115
+ # [article] +String+ ID36 of a post or self.post
116
+ def each_comment_of_the_post_thread article
117
+ Enumerator.new do |e|
118
+ f = lambda do |smth|
119
+ smth["data"]["children"].each do |child|
120
+ f[child["data"]["replies"]] if child["data"]["replies"].is_a? Hash
121
+ fail "unknown type child['kind']: #{child["kind"]}" unless child["kind"] == "t1"
122
+ e << [child["data"]["name"], child["data"]]
123
+ end
124
+ end
125
+ f[ json(:get, "/comments/#{article}", depth: 100500, limit: 100500).tap do |t|
126
+ fail "smth weird about /comments/<id> response" unless t.size == 2
127
+ end[1] ]
128
+ end
97
129
  end
98
130
 
99
131
  private
@@ -125,9 +157,9 @@ module RedditBot
125
157
  @iden_and_captcha = [iden, gets.strip]
126
158
  end
127
159
 
128
- def resp_with_token mtd, url, form
160
+ def resp_with_token mtd, path, form
129
161
  nil until _ = catch(:"401") do
130
- reddit_resp mtd, "https://oauth.reddit.com" + url, form, [
162
+ reddit_resp mtd, "https://oauth.reddit.com" + path, form, [
131
163
  ["Authorization", "bearer #{token}"],
132
164
  ["User-Agent", "bot/#{@username}/0.0.0 by /u/nakilon"],
133
165
  ], nil # base auth
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.1.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-12 00:00:00.000000000 Z
11
+ date: 2016-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -52,6 +52,7 @@ files:
52
52
  - examples/pokemon_trading/Gemfile.lock
53
53
  - examples/pokemon_trading/main.rb
54
54
  - examples/yayornay/Gemfile
55
+ - examples/yayornay/Gemfile.lock
55
56
  - examples/yayornay/main.rb
56
57
  - lib/reddit_bot.rb
57
58
  - reddit_bot.gemspec