reddit_bot 1.0.1 → 1.1.0

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: 12c8ad9df61dbafab56150f521ff9714560d2146
4
- data.tar.gz: 50d1d6f7343d404c0e1febd18021d243ada03672
3
+ metadata.gz: 1f926ac62735d34c52c09633ba7ad9f837c79619
4
+ data.tar.gz: 7843372b1f19af4a445db6c5b471b93a3c02d92a
5
5
  SHA512:
6
- metadata.gz: 18c4d2ed6b45682d2ecca5bdb3d7111b5db74108a1b71d6ba4ee8ef4f5fd43ffb26838799c590904f465b35306f26cad234c22f143f42a3d18b9106987323943
7
- data.tar.gz: aad031f0c1b1f847d363b5290069472db371976ffa1e89b56b5ce085ca497a070dc0eb53d1f479ceb0676a0ac9b876fe5199e756fbf26ba8126a52303260913d
6
+ metadata.gz: d35ed1deddea26306c5e1877e793e6a8cac4454a74f10195d3a4a345233f8ebffb25997e99976942764790a9dcd996201cd79525c7b29a97d053edab5cd6a49f
7
+ data.tar.gz: 683bd66d4eb6cd408799b75a3eb619836f5e44204ce66030cd49baaf76d0ceabfa37f3df87ae75ed803d65dbc34b1036ab2c5d54cd178bb092dc0809edbf11e2
data/README.md CHANGED
@@ -17,8 +17,11 @@ Python (and so PRAW) sucks.
17
17
 
18
18
  The [examples folder](examples) includes:
19
19
 
20
- * iostroubleshooting -- (currently active) bot that applies flairs to posts
21
- * mlgtv -- (currently active) bot that updates sidebar with currently streaming twitch channels
20
+ * iostroubleshooting -- bot that applies flairs to posts
21
+ * mlgtv -- bot that updates sidebar with currently streaming twitch channels
22
+ * pokemon_trading -- bot that sets flair to user according to request submitted via PM
23
+ * oneplus -- bot that removes and modmails about links to 1080x1920 images
24
+ * yayornay (not active yet) -- bot that flairs posts according to voting in top level comments
22
25
  * largeimages -- useful script for [subreddit /r/largeimages](https://reddit.com/r/largeimages/top)
23
26
  It calculates quality of x-posts from different subreddits based on mods activity (remove/approve).
24
27
  For example, this shows that it would be ok to ignore /r/pics from now:
@@ -54,7 +57,7 @@ probably Gemfile:
54
57
  source "https://rubygems.org"
55
58
  gem "reddit_bot"
56
59
 
57
- TODO: Write usage instructions here
60
+ TODO: Write usage instructions here
58
61
  TODO: manual on how to create bots with Reddit web interface and run via bash console
59
62
 
60
63
  #### Contributing and License
@@ -2,4 +2,4 @@
2
2
 
3
3
  # ruby "2.0.0"
4
4
  gem "json"#, "1.7.7"
5
- gem "reddit_bot"
5
+ gem "reddit_bot", "0.1.3"
@@ -2,14 +2,15 @@ GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
4
  json (1.8.3)
5
- reddit_bot (0.1.0)
5
+ reddit_bot (0.1.3)
6
+ json
6
7
 
7
8
  PLATFORMS
8
9
  ruby
9
10
 
10
11
  DEPENDENCIES
11
12
  json
12
- reddit_bot
13
+ reddit_bot (= 0.1.3)
13
14
 
14
15
  BUNDLED WITH
15
16
  1.11.2
@@ -2,36 +2,50 @@
2
2
 
3
3
  BOT = RedditBot::Bot.new YAML.load(File.read "secrets.yaml"), ignore_captcha: true
4
4
 
5
- require_relative "../../../../dimensioner/get_dimensions"
5
+ require_relative "#{"../" if ENV["LOGNAME"] == "nakilon"}../../../dimensioner/get_dimensions"
6
6
 
7
- SUBREDDIT = "test___________"
8
- FLAIR_CLASS = "redflair"
7
+ SUBREDDIT = "oneplus"
8
+ # FLAIR_CLASS = "redflair"
9
9
 
10
10
  checked = []
11
11
  loop do
12
12
  AWSStatus::touch
13
13
  puts "LOOP #{Time.now}"
14
14
 
15
- _ = DownloadWithRetry::download_with_retry("https://www.reddit.com/r/#{SUBREDDIT}.json?sort=new&restrict_sr=on&t=hour") and
16
- JSON.parse(_)["data"]["children"].each do |item|
15
+ BOT.json(:get, "/r/#{SUBREDDIT}/new")["data"]["children"].each do |item|
17
16
  id, url, title, subreddit = item["data"].values_at(*%w{ id url title subreddit })
18
17
  next if checked.include? id # WTF?
19
18
  checked << id
20
- next puts "skipped #{url} from https://redd.it/#{id}" if :skipped == _ = GetDimensions::get_dimensions(url)
21
- next puts "unable #{url} from https://redd.it/#{id}" unless _
19
+ redd_it = "https://redd.it/#{id}"
20
+ next puts "skipped #{url} from #{redd_it}" if :skipped == _ = GetDimensions::get_dimensions(url)
21
+ next puts "unable #{url} from #{redd_it}" unless _
22
22
  width, height, * = _
23
23
  result = ([1080, 1920] != resolution = [width, height])
24
24
  puts "#{result} #{id} [#{resolution}] #{title} #{url}"
25
25
  next if result
26
- {"json"=>{"errors"=>[]}} == _ = BOT.json(:post,
27
- "/api/selectflair",
28
- flair_template_id: BOT.json(:post,
29
- "/api/flairselector",
30
- link: "t3_#{id}",
31
- )["choices"].find{ |i| i["flair_css_class"] == FLAIR_CLASS }["flair_template_id"],
32
- link: "t3_#{id}",
33
- ) or fail _.inspect
34
- BOT.report "1080x1920", "t3_#{id}"
26
+
27
+ ### flair
28
+ # {"json"=>{"errors"=>[]}} == _ = BOT.json(:post,
29
+ # "/api/selectflair",
30
+ # flair_template_id: BOT.json(:post,
31
+ # "/api/flairselector",
32
+ # link: "t3_#{id}",
33
+ # )["choices"].find{ |i| i["flair_css_class"] == FLAIR_CLASS }["flair_template_id"],
34
+ # link: "t3_#{id}",
35
+ # ) or fail _.inspect
36
+ ### report
37
+ # BOT.report "1080x1920", "t3_#{id}"
38
+
39
+ ### delete
40
+ BOT.json :post, "/api/remove",
41
+ id: "t3_#{id}",
42
+ spam: false
43
+ ### modmail
44
+ BOT.json :post, "/api/compose",
45
+ subject: "possible screenshot detected",
46
+ text: "please, investigate: #{redd_it}",
47
+ to: "/r/#{SUBREDDIT}"
48
+
35
49
  end or puts "/r/#{SUBREDDIT} seems to be 403-ed"
36
50
 
37
51
  puts "END LOOP #{Time.now}"
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ if ENV["LOGNAME"] == "nakilon"
4
+ gem "reddit_bot", path: "../.."
5
+ else
6
+ gem "reddit_bot", "~>1.1.0"
7
+ end
@@ -0,0 +1,33 @@
1
+ require_relative "../boilerplate"
2
+
3
+ SUBREDDIT = "test___________"
4
+ # SUBREDDIT = "yayornay"
5
+
6
+ BOT = RedditBot::Bot.new YAML.load(File.read "secrets.yaml"), ignore_captcha: true, subreddit: SUBREDDIT
7
+
8
+
9
+ loop do
10
+ AWSStatus::touch
11
+ puts "LOOP #{Time.now}"
12
+
13
+ BOT.each_new_post_with_top_level_comments do |post, comments|
14
+ yay = []
15
+ nay = []
16
+ comments.each do |comment|
17
+ yay |= [comment["author"]] if comment["body"][/yay/i]
18
+ nay |= [comment["author"]] if comment["body"][/nay/i]
19
+ end
20
+ yay, nay = [(yay - nay).size, (nay - yay).size]
21
+ next if 0 == total = yay + nay
22
+ proper_class = yay > nay ? "yay" : yay < nay ? "nay" : "none"
23
+ proper_text = "#{(100 * yay / total).round}% Yay"
24
+ next if [proper_class, proper_text] == [post["link_flair_css_class"], post["link_flair_text"]]
25
+ puts "setting #{[proper_class, proper_text]} to #{post["name"]}"
26
+ {"json"=>{"errors"=>[]}} == _ = BOT.set_post_flair(
27
+ post, proper_class, proper_text
28
+ ) or fail _.inspect
29
+ end
30
+
31
+ puts "END LOOP #{Time.now}"
32
+ sleep 300
33
+ end
data/lib/reddit_bot.rb CHANGED
@@ -8,13 +8,15 @@ require "json"
8
8
 
9
9
 
10
10
  module RedditBot
11
- VERSION = "1.0.1"
11
+ VERSION = "1.1.0"
12
12
 
13
13
  class Bot
14
14
 
15
15
  def initialize secrets, **kwargs
16
16
  @secrets = secrets.values_at *%i{ client_id client_secret password login }
17
- @ignore_captcha = kwargs[:ignore_captcha]
17
+ @ignore_captcha = true
18
+ @ignore_captcha = kwargs[:ignore_captcha] if kwargs.has_key?(:ignore_captcha)
19
+ @subreddit = kwargs[:subreddit]
18
20
  end
19
21
 
20
22
  # attr_accessor :token_cached
@@ -26,7 +28,7 @@ module RedditBot
26
28
  def json mtd, url, _form = []
27
29
  form = Hash[_form]
28
30
  response = JSON.parse resp_with_token mtd, url, form.merge({api_type: "json"})
29
- if response.is_a?(Hash) && response["json"] # for example, flairlist.json and {"error": 403} do not have it
31
+ if response.is_a?(Hash) && response["json"] # for example, flairlist.json and {"error": 403} do not have it
30
32
  puts "ERROR OCCURED on #{[mtd, url]}" unless response["json"]["errors"].empty?
31
33
  # pp response["json"]
32
34
  response["json"]["errors"].each do |error, description|
@@ -60,6 +62,38 @@ module RedditBot
60
62
  thing_id: thing_id
61
63
  end
62
64
 
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
72
+
73
+ def each_new_post_with_top_level_comments
74
+ json(:get, "/r/#{@subreddit}/new")["data"]["children"].each do |post|
75
+ 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
80
+ fail "smth weird about /comments/<id> response" unless t.size == 2
81
+ yield post["data"], t[1]["data"]["children"].map{ |child|
82
+ fail "unknown type child['kind']: #{child["kind"]}" unless child["kind"] == "t1"
83
+ child["data"]
84
+ }.to_enum
85
+ end
86
+ end
87
+
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 }["flair_template_id"]
95
+ end
96
+
63
97
  private
64
98
 
65
99
  def token
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.0.1
4
+ version: 1.1.0
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-02-24 00:00:00.000000000 Z
11
+ date: 2016-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -51,6 +51,8 @@ files:
51
51
  - examples/pokemon_trading/Gemfile
52
52
  - examples/pokemon_trading/Gemfile.lock
53
53
  - examples/pokemon_trading/main.rb
54
+ - examples/yayornay/Gemfile
55
+ - examples/yayornay/main.rb
54
56
  - lib/reddit_bot.rb
55
57
  - reddit_bot.gemspec
56
58
  homepage: https://github.com/Nakilon/reddit_bot