reddit_bot 1.7.4 → 1.7.5

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: 2e081c0ab3c130fb135fa473e6af3fbbc21cfe98
4
- data.tar.gz: ef7376ff05f190b9fd8e6b054817de92e4f11b80
3
+ metadata.gz: 5dca4b66ca047773ac0de806e1281cf7a088bf8f
4
+ data.tar.gz: d53ad1a90ba44333ab2245b98a304da5e0f89974
5
5
  SHA512:
6
- metadata.gz: 8638b799d7a414b5a3016a76bc605311abfb27815baf5ec7aea9ead30e241cf523832d747bdb4f5611e82c30a60d4b66bc69463ae5d9a802e1e6c971cf03e48b
7
- data.tar.gz: fb618375d7634a1ee42ce47b3e04579a6a15ae028a4eb9523c13ab8294e5c2b7c24e7cbbcb5707119b4e42c952d31edcbcda10a7464f510e046b79bba92ce685
6
+ metadata.gz: a97e1f8905b56cafa91ce4c3509f7a779db5e5579f8f480363876f45bf4bc152cffd5c4b75ea2040607d95892b831d728981c372440145153577e04ca213e0f6
7
+ data.tar.gz: 950cbf9d106d01f4f6a68e3a8884d36cc7b0962a4488b3c8363c7d44cda637570e706581b7d07413e0c4d2dfe121f54575b0c14a8bbe3b01176f1f7284a734dc
@@ -34,7 +34,7 @@ loop do
34
34
  JSON.parse( begin
35
35
  NetHTTPUtils.request_data "https://www.reddit.com/r/#{subreddit}/comments.json", header: ["User-Agent", "ajsdjasdasd"]
36
36
  rescue NetHTTPUtils::Error => e
37
- raise unless [503, 504, 500].include? e.code
37
+ raise unless [500, 502, 503, 504].include? e.code
38
38
  sleep 60
39
39
  retry
40
40
  end )["data"]["children"].each do |comment|
@@ -2,7 +2,7 @@
2
2
 
3
3
  gem "json"
4
4
 
5
- gem "nokogiri", "~>1.10.4"
5
+ gem "nokogiri", "~>1.10.8"
6
6
 
7
7
  gem "nethttputils", git: "git@github.com:nakilon/nethttputils.git"
8
8
  gem "directlink", "~>0.0.8.5"
@@ -76,7 +76,7 @@ GEM
76
76
  mini_portile2 (2.4.0)
77
77
  multi_json (1.13.1)
78
78
  multipart-post (2.0.0)
79
- nokogiri (1.10.5)
79
+ nokogiri (1.10.8)
80
80
  mini_portile2 (~> 2.4.0)
81
81
  os (0.9.6)
82
82
  public_suffix (2.0.5)
@@ -101,7 +101,7 @@ DEPENDENCIES
101
101
  google-cloud-error_reporting
102
102
  json
103
103
  nethttputils!
104
- nokogiri (~> 1.10.4)
104
+ nokogiri (~> 1.10.8)
105
105
 
106
106
  BUNDLED WITH
107
107
  2.0.2
@@ -40,7 +40,7 @@ search_url = lambda do |url|
40
40
  JSON.load( begin
41
41
  NetHTTPUtils.request_data "https://www.reddit.com/r/largeimages/search.json", form: {q: "url:#{url}", restrict_sr: "on"}, header: ["User-Agent", "ajsdjasdasd"]
42
42
  rescue NetHTTPUtils::Error => e
43
- raise unless [503].include? e.code
43
+ raise unless [500, 503].include? e.code
44
44
  sleep 60
45
45
  retry
46
46
  end )["data"]["children"]
@@ -85,13 +85,22 @@ loop do
85
85
  ].each do |source, min_resolution, entries|
86
86
  logger.warn "#{source}.size: #{entries.size}"
87
87
  entries.each do |id, url, title, subreddit, author, permalink|
88
+ author.downcase!
88
89
  next if checked.include? id
89
90
  checked << id
90
91
  # next if Gem::Platform.local.os == "darwin" # prevent concurrent posting
91
- logger.debug "image url for #{id}: #{url}"
92
- next logger.warn "skipped a post by /u/sjhill" if author == "sjhill" # opt-out
93
- next logger.warn "skipped a post by /u/redisforever" if author == "redisforever" # opt-out
94
- next logger.warn "skipped a post by /u/bekalaki" if author == "bekalaki" # 9 ways to divide a karmawhore
92
+ logger.info "image url for #{id}: #{url}"
93
+ next logger.warn "skipped a post by /u/sjhill" if author == "sjhill" # opt-out
94
+ next logger.warn "skipped a post by /u/redisforever" if author == "redisforever" # opt-out
95
+ next logger.warn "skipped a post by /u/bekalaki" if author == "bekalaki" # 9 ways to divide a karmawhore
96
+ next logger.warn "skipped a post by /u/cherryblackeyes" if author == "cherryblackeyes" # he's not nice
97
+ next logger.warn "skipped a post by /u/abel_a_kay" if author == "abel_a_kay" # posting very similar images of the same thing for the history
98
+ next logger.warn "skipped gifv" if ( begin
99
+ URI url
100
+ rescue URI::InvalidURIError
101
+ require "addressable"
102
+ URI Addressable::URI.escape url
103
+ end ).host.split(?.) == %w{ v redd it }
95
104
 
96
105
  t = begin
97
106
  DirectLink url, 60
@@ -105,11 +114,11 @@ loop do
105
114
  DirectLink::ErrorBadLink => e
106
115
  next logger.error "skipped (#{e}) #{url} from http://redd.it/#{id}"
107
116
  end
108
- logger.info "DirectLink: #{t.inspect}"
117
+ logger.debug "DirectLink: #{t.inspect}"
109
118
  tt = t.is_a?(Array) ? t : [t]
110
119
  next logger.error "probably crosspost of a self post: http://redd.it/#{id}" if tt.empty?
111
120
  unless min_resolution <= tt.first.width * tt.first.height
112
- next logger.debug "skipped low resolution #{source}"
121
+ next logger.info "skipped low resolution #{source}"
113
122
  end
114
123
  # puts "https://www.reddit.com/r/LargeImages/search.json?q=url%3A#{CGI.escape url}&restrict_sr=on"
115
124
  resolution = "[#{tt.first.width}x#{tt.first.height}]"
@@ -37,11 +37,11 @@ tweet2titleNtext = lambda do |tweet|
37
37
  [CGI::unescapeHTML(tweet["full_text"]).sub(/( https:\/\/t\.co\/[0-9a-zA-Z]{10})*\z/, ""), text, contains_media]
38
38
  end
39
39
  [
40
- [905764294687633408, true, "The Polish government & military high command is now evacuating Warsaw for Brest, 120 miles east: German armies are too close to the capital", "* [Image 1](https://pbs.twimg.com/media/DJHq71BXYAA6KJ0.jpg)\n\n" "^- ^WW2 ^Tweets ^from ^1941 [^\\(@#{TWITTER}\\)](https://twitter.com/#{TWITTER}) ^| [^""September ^7, ^2017](https://twitter.com/#{TWITTER}/status/905764294687633408)"],
41
- [915534673471733760, true, "In east Poland (now Soviet Ukraine) industry & farms to be collectivised, political parties banned, aristocrats & capitalists \"re-educated\".", "* [Image 1](https://pbs.twimg.com/media/DLSh2J9W4AACcOG.jpg)\n\n* [Image 2](https://pbs.twimg.com/media/DLSh4sKX0AEBaXq.jpg)\n\n^- ^WW2 ^Tweets ^from ^1941 [^\\(@#{TWITTER}\\)](https://twitter.com/#{TWITTER}) ^| [^" "October ^4, ^2017](https://twitter.com/#{TWITTER}/status/915534673471733760)"],
42
- [915208866408824832, true, "For 1st time, RAF planes dropping propaganda leaflets on Berlin itself, entitled \"Germans: these are your leaders!\"", "* [Image 1](https://pbs.twimg.com/media/DLN5jJ-XkAEUz9M.jpg)\n\n* [Link 1](https://www.psywar.org/product_1939EH158.php)\n\n" "^- ^WW2 ^Tweets ^from ^1941 [^\\(@#{TWITTER}\\)](https://twitter.com/#{TWITTER}) ^| [^" "October ^3, ^2017](https://twitter.com/#{TWITTER}/status/915208866408824832)"],
43
- [914577848891006978, true, "\"In Poland, Russia pursued a cold policy of selfinterest. But clearly necessary for Russia… against Nazi menace.\"", "* [Link 1](https://www.youtube.com/watch?v=ygmP5A3n2JA)\n\n" "^- ^WW2 ^Tweets ^from ^1941 [^\\(@#{TWITTER}\\)](https://twitter.com/#{TWITTER}) ^| [^" "October ^1, ^2017](https://twitter.com/#{TWITTER}/status/914577848891006978)"],
44
- [926581977372942336, false, "Finland rejects Soviet demand to surrender land near Leningrad & give Red Navy base in Hanko; Soviets now claim Finns' manner \"warlike\".", "^- ^WW2 ^Tweets ^from ^1941 [^\\(@#{TWITTER}\\)](https://twitter.com/#{TWITTER}) ^| [^" "November ^3, ^2017](https://twitter.com/#{TWITTER}/status/926581977372942336)"],
40
+ [905764294687633408, true, "The Polish government & military high command is now evacuating Warsaw for Brest, 120 miles east: German armies are too close to the capital", "* [Image 1](https://pbs.twimg.com/media/DJHq71BXYAA6KJ0.jpg)\n\n" "^- ^WW2 ^Tweets ^from ^1942 [^\\(@#{TWITTER}\\)](https://twitter.com/#{TWITTER}) ^| [^""September ^7, ^2017](https://twitter.com/#{TWITTER}/status/905764294687633408)"],
41
+ [915534673471733760, true, "In east Poland (now Soviet Ukraine) industry & farms to be collectivised, political parties banned, aristocrats & capitalists \"re-educated\".", "* [Image 1](https://pbs.twimg.com/media/DLSh2J9W4AACcOG.jpg)\n\n* [Image 2](https://pbs.twimg.com/media/DLSh4sKX0AEBaXq.jpg)\n\n^- ^WW2 ^Tweets ^from ^1942 [^\\(@#{TWITTER}\\)](https://twitter.com/#{TWITTER}) ^| [^" "October ^4, ^2017](https://twitter.com/#{TWITTER}/status/915534673471733760)"],
42
+ [915208866408824832, true, "For 1st time, RAF planes dropping propaganda leaflets on Berlin itself, entitled \"Germans: these are your leaders!\"", "* [Image 1](https://pbs.twimg.com/media/DLN5jJ-XkAEUz9M.jpg)\n\n* [Link 1](https://www.psywar.org/product_1939EH158.php)\n\n" "^- ^WW2 ^Tweets ^from ^1942 [^\\(@#{TWITTER}\\)](https://twitter.com/#{TWITTER}) ^| [^" "October ^3, ^2017](https://twitter.com/#{TWITTER}/status/915208866408824832)"],
43
+ [914577848891006978, true, "\"In Poland, Russia pursued a cold policy of selfinterest. But clearly necessary for Russia… against Nazi menace.\"", "* [Link 1](https://www.youtube.com/watch?v=ygmP5A3n2JA)\n\n" "^- ^WW2 ^Tweets ^from ^1942 [^\\(@#{TWITTER}\\)](https://twitter.com/#{TWITTER}) ^| [^" "October ^1, ^2017](https://twitter.com/#{TWITTER}/status/914577848891006978)"],
44
+ [926581977372942336, false, "Finland rejects Soviet demand to surrender land near Leningrad & give Red Navy base in Hanko; Soviets now claim Finns' manner \"warlike\".", "^- ^WW2 ^Tweets ^from ^1942 [^\\(@#{TWITTER}\\)](https://twitter.com/#{TWITTER}) ^| [^" "November ^3, ^2017](https://twitter.com/#{TWITTER}/status/926581977372942336)"],
45
45
  ].each do |id, contains_media_, title_, text_|
46
46
  title, text, contains_media = tweet2titleNtext[ JSON.load NetHTTPUtils.request_data(
47
47
  "https://api.twitter.com/1.1/statuses/show.json",
@@ -101,7 +101,7 @@ loop do
101
101
  sleep t
102
102
  end
103
103
  rescue NetHTTPUtils::Error => e
104
- fail if e.code != 503
104
+ fail unless [500, 503].include? e.code
105
105
  sleep timeout
106
106
  timeout *= 2
107
107
  retry
@@ -1,25 +1,26 @@
1
1
  GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
- addressable (2.5.2)
5
- public_suffix (>= 2.0.2, < 4.0)
6
- directlink (0.0.4.7)
4
+ addressable (2.7.0)
5
+ public_suffix (>= 2.0.2, < 5.0)
6
+ directlink (0.0.8.7)
7
7
  addressable
8
8
  fastimage (~> 2.1.3)
9
9
  kramdown
10
+ nethttputils (~> 0.3.3.0)
10
11
  nokogiri
11
- reddit_bot (~> 1.6.13)
12
- fastimage (2.1.5)
13
- json (2.1.0)
14
- kramdown (1.17.0)
12
+ reddit_bot (~> 1.7.0)
13
+ fastimage (2.1.7)
14
+ json (2.3.0)
15
+ kramdown (2.1.0)
15
16
  mini_portile2 (2.4.0)
16
- nethttputils (0.2.5.1)
17
+ nethttputils (0.3.3.0)
17
18
  nokogiri (1.10.4)
18
19
  mini_portile2 (~> 2.4.0)
19
20
  public_suffix (2.0.5)
20
- reddit_bot (1.6.13)
21
+ reddit_bot (1.7.3)
21
22
  json
22
- nethttputils (~> 0.2.5.1)
23
+ nethttputils (~> 0.3.3.0)
23
24
 
24
25
  PLATFORMS
25
26
  ruby
@@ -3,6 +3,7 @@ STDOUT.sync = true
3
3
 
4
4
  require "openssl"
5
5
  require "json"
6
+ require "yaml"
6
7
 
7
8
  require "nethttputils"
8
9
 
@@ -234,10 +235,10 @@ module RedditBot
234
235
  "User-Agent" => "bot/#{@user_agent || @name}/#{RedditBot::VERSION} by /u/nakilon",
235
236
  }
236
237
  rescue NetHTTPUtils::Error => e
238
+ raise unless e.code == 401
237
239
  sleep timeout
238
240
  Module.nesting[1].logger.info "sleeping #{timeout} seconds because of #{e.code}"
239
241
  timeout *= 2
240
- raise unless e.code == 401
241
242
  @token_cached = nil
242
243
  retry
243
244
  end
@@ -247,21 +248,7 @@ module RedditBot
247
248
  mtd, url, form, headers, basic_auth = *args
248
249
  headers["Cookie:"] = "over18=1"
249
250
  begin
250
- NetHTTPUtils.request_data(url, mtd, form: form, header: headers, auth: basic_auth) do |response|
251
- next unless remaining = response.to_hash["x-ratelimit-remaining"]
252
- if Gem::Platform.local.os == "darwin"
253
- Module.nesting[1].logger.debug %w{
254
- x-ratelimit-remaining
255
- x-ratelimit-used
256
- x-ratelimit-reset
257
- }.map{ |key| "#{key}=#{response.to_hash[key]}" }.join ", "
258
- end
259
- fail remaining[0] if remaining[0].size < 4
260
- next if remaining[0].size > 4
261
- t = (response.to_hash["x-ratelimit-reset"][0].to_f + 1) / [remaining[0].to_f - 10, 1].max + 1
262
- Module.nesting[1].logger.info "sleeping #{t} seconds because of x-ratelimit"
263
- sleep t
264
- end
251
+ NetHTTPUtils.request_data url, mtd, form: form, header: headers, auth: basic_auth
265
252
  rescue NetHTTPUtils::Error => e
266
253
  sleep 5
267
254
  raise unless e.code.to_s.start_with? "50"
@@ -271,4 +258,66 @@ module RedditBot
271
258
  end
272
259
 
273
260
  end
261
+
262
+ module Twitter
263
+ require "json"
264
+
265
+ def self.init_twitter twitter
266
+ const_set :TWITTER_ACCOUNT, twitter
267
+ const_set :TWITTER_ACCESS_TOKEN, JSON.load(
268
+ NetHTTPUtils.request_data "https://api.twitter.com/oauth2/token", :post,
269
+ auth: File.read("twitter.token").split,
270
+ form: {grant_type: :client_credentials}
271
+ )["access_token"]
272
+ end
273
+
274
+ require "cgi"
275
+ def self.tweet2titleNtext tweet
276
+ text = ""
277
+ contains_media = false
278
+ up = ->s{ s.split.map{ |w| "^#{w}" }.join " " }
279
+ if tweet["extended_entities"] && !tweet["extended_entities"]["media"].empty?
280
+ contains_media = true
281
+ tweet["extended_entities"]["media"].each_with_index do |media, i|
282
+ text.concat "* [Image #{i + 1}](#{media["media_url_https"]})\n\n"
283
+ end
284
+ end
285
+ if !tweet["entities"]["urls"].empty?
286
+ contains_media = true
287
+ tweet["entities"]["urls"].each_with_index do |url, i|
288
+ text.concat "* [Link #{i + 1}](#{url["expanded_url"]})\n\n"
289
+ end
290
+ end
291
+ text.concat "^- #{
292
+ up[tweet["user"]["name"]]
293
+ } [^\\(@#{TWITTER_ACCOUNT}\\)](https://twitter.com/#{TWITTER_ACCOUNT}) ^| [#{
294
+ up[Date.parse(tweet["created_at"]).strftime "%B %-d, %Y"]
295
+ }](https://twitter.com/#{TWITTER_ACCOUNT}/status/#{tweet["id"]})"
296
+ [CGI::unescapeHTML(tweet["full_text"]).sub(/( https:\/\/t\.co\/[0-9a-zA-Z]{10})*\z/, ""), text, contains_media]
297
+ end
298
+
299
+ def self.user_timeline
300
+ timeout = 1
301
+ JSON.load begin
302
+ NetHTTPUtils.request_data(
303
+ "https://api.twitter.com/1.1/statuses/user_timeline.json",
304
+ form: { screen_name: TWITTER_ACCOUNT, count: 200, tweet_mode: "extended" },
305
+ header: { Authorization: "Bearer #{TWITTER_ACCESS_TOKEN}" }
306
+ ) do |res|
307
+ next unless res.key? "x-rate-limit-remaining"
308
+ remaining = res.fetch("x-rate-limit-remaining").to_i
309
+ next if 100 < remaining
310
+ t = (res.fetch("x-rate-limit-reset").to_i - Time.now.to_i + 1).fdiv remaining
311
+ puts "sleep #{t}"
312
+ sleep t
313
+ end
314
+ rescue NetHTTPUtils::Error => e
315
+ fail unless [500, 503].include? e.code
316
+ sleep timeout
317
+ timeout *= 2
318
+ retry
319
+ end
320
+ end
321
+ end
322
+
274
323
  end
@@ -1,8 +1,6 @@
1
- require_relative "lib/reddit_bot/version"
2
-
3
1
  Gem::Specification.new do |spec|
4
2
  spec.name = "reddit_bot"
5
- spec.version = RedditBot::VERSION
3
+ spec.version = "1.7.5"
6
4
  spec.authors = ["Victor Maslov"]
7
5
  spec.email = ["nakilon@gmail.com"]
8
6
 
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.7.4
4
+ version: 1.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-04 00:00:00.000000000 Z
11
+ date: 2020-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -108,7 +108,6 @@ files:
108
108
  - examples/yayornay/Gemfile.lock
109
109
  - examples/yayornay/main.rb
110
110
  - lib/reddit_bot.rb
111
- - lib/reddit_bot/version.rb
112
111
  - reddit_bot.gemspec
113
112
  homepage: https://github.com/Nakilon/reddit_bot
114
113
  licenses:
@@ -1,3 +0,0 @@
1
- module RedditBot
2
- VERSION = "1.7.4" # :nodoc:
3
- end