directlink 0.0.4.4 → 0.0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/directlink +1 -0
- data/directlink.gemspec +2 -2
- data/lib/directlink.rb +18 -16
- data/test.rb +23 -9
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 812459ea321dc15a16ea19cea2176c5b37fa868b
|
4
|
+
data.tar.gz: b1328fb09fb38b8471eaaee2eb951ba77f87f026
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e82be782453a80cf10d38334e532edde1feb72c9b24fb9ff24c3c7baa7d12a3b30de655af9be7f1527b63d5fea9aae2fbc2e08ec80c28f0e846d6a40454e019
|
7
|
+
data.tar.gz: fa87ea7807b41ca3689455afb2dac674ee659352adf5a5fa57c519eb47d46d7d4f388ba8d9478d69bfee3d3998c3c762e3159ad1105656b7a2e27734f7f86924
|
data/bin/directlink
CHANGED
data/directlink.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "directlink"
|
3
|
-
spec.version = "0.0.4.
|
3
|
+
spec.version = "0.0.4.5"
|
4
4
|
spec.summary = "converts any kind of image hyperlink to direct link, type of image and its resolution"
|
5
5
|
|
6
6
|
spec.author = "Victor Maslov aka Nakilon"
|
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.add_dependency "fastimage", "~>2.1.3"
|
13
13
|
spec.add_dependency "nokogiri"
|
14
|
-
spec.add_dependency "reddit_bot", "~>1.6.
|
14
|
+
spec.add_dependency "reddit_bot", "~>1.6.9"
|
15
15
|
spec.add_dependency "kramdown"
|
16
16
|
spec.add_dependency "addressable"
|
17
17
|
spec.add_development_dependency "minitest"
|
data/lib/directlink.rb
CHANGED
@@ -19,7 +19,7 @@ module DirectLink
|
|
19
19
|
end
|
20
20
|
logging_error = Class.new RuntimeError do
|
21
21
|
def initialize msg
|
22
|
-
Module.nesting.first.logger.error msg
|
22
|
+
Module.nesting.first.logger.error "#{self.class}: #{msg}"
|
23
23
|
super msg
|
24
24
|
end
|
25
25
|
end
|
@@ -216,7 +216,7 @@ module DirectLink
|
|
216
216
|
end
|
217
217
|
json = if ENV["REDDIT_SECRETS"]
|
218
218
|
require "reddit_bot"
|
219
|
-
RedditBot.logger.level = Logger::
|
219
|
+
RedditBot.logger.level = Logger::ERROR
|
220
220
|
require "yaml"
|
221
221
|
self.reddit_bot ||= RedditBot::Bot.new YAML.load_file ENV["REDDIT_SECRETS"]
|
222
222
|
retry_on_json_parseerror.call{ self.reddit_bot.json :get, "/by_id/t3_#{id}" }
|
@@ -228,13 +228,12 @@ module DirectLink
|
|
228
228
|
end
|
229
229
|
data = json["data"]["children"].first["data"]
|
230
230
|
if data["media"]["reddit_video"]
|
231
|
-
|
232
|
-
raise ErrorAssert.new "our knowledge about Reddit API seems to be outdated" unless t.size == 1
|
233
|
-
return [true, t.first["source"]["url"]]
|
231
|
+
return [true, data["media"]["reddit_video"]["fallback_url"]]
|
234
232
|
else
|
235
233
|
raise ErrorAssert.new "our knowledge about Reddit API seems to be outdated" unless data["media"].keys.sort == %w{ oembed type } && data["media"]["type"] == "youtube.com"
|
236
234
|
return [true, data["media"]["oembed"]["thumbnail_url"]]
|
237
235
|
end if data["media"]
|
236
|
+
return reddit data["url"] if data["crosspost_parent"] # TODO: test that it does it
|
238
237
|
return [true, data["url"]] unless data["is_self"]
|
239
238
|
raise ErrorAssert.new "our knowledge about Reddit API seems to be outdated" if data["url"] != "https://www.reddit.com" + data["permalink"]
|
240
239
|
return [false, data["selftext"]]
|
@@ -273,7 +272,9 @@ def DirectLink link, max_redirect_resolving_retry_delay = nil, giveup = false
|
|
273
272
|
# > NetHTTPUtils.logger.level = Logger::DEBUG
|
274
273
|
# > NetHTTPUtils.request_data "http://www.aeronautica.difesa.it/organizzazione/REPARTI/divolo/PublishingImages/6%C2%B0%20Stormo/2013-decollo%20al%20tramonto%20REX%201280.jpg",
|
275
274
|
# max_read_retry_delay: 5, timeout: 5
|
276
|
-
r = NetHTTPUtils.get_response link, header: {
|
275
|
+
r = NetHTTPUtils.get_response link, header: {
|
276
|
+
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"
|
277
|
+
}, **(max_redirect_resolving_retry_delay ? {
|
277
278
|
max_timeout_retry_delay: max_redirect_resolving_retry_delay,
|
278
279
|
max_sslerror_retry_delay: max_redirect_resolving_retry_delay,
|
279
280
|
max_read_retry_delay: max_redirect_resolving_retry_delay,
|
@@ -328,7 +329,9 @@ def DirectLink link, max_redirect_resolving_retry_delay = nil, giveup = false
|
|
328
329
|
raise DirectLink::ErrorBadLink.new link if giveup # TODO: print original url in such cases if there was a recursion
|
329
330
|
f = ->_{ _.type == :a ? _.attr["href"] : _.children.flat_map(&f) }
|
330
331
|
require "kramdown"
|
331
|
-
return f[Kramdown::Document.new(u).root].map
|
332
|
+
return f[Kramdown::Document.new(u).root].map do |sublink|
|
333
|
+
DirectLink URI.join(link, sublink), max_redirect_resolving_retry_delay, giveup
|
334
|
+
end
|
332
335
|
end
|
333
336
|
return struct.new *u.values_at(*%w{ fallback_url width height }), "video" if u.is_a? Hash
|
334
337
|
link = u
|
@@ -343,17 +346,15 @@ def DirectLink link, max_redirect_resolving_retry_delay = nil, giveup = false
|
|
343
346
|
raise if giveup
|
344
347
|
require "nokogiri"
|
345
348
|
html = Nokogiri::HTML NetHTTPUtils::request_data link, header: {"User-Agent" => "Mozilla"}
|
346
|
-
h = {}
|
349
|
+
h = {} # TODO: maybe move it outside because of possible img[:src] recursion?...
|
347
350
|
l = lambda do |node, s = []|
|
348
351
|
node.element_children.flat_map do |child|
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
else
|
356
|
-
l[child, s + [child.node_name]]
|
352
|
+
next l[child, s + [child.node_name]] unless "img" == child.node_name
|
353
|
+
begin
|
354
|
+
[[s, (h[child[:src]] = h[child[:src]] || DirectLink(URI.join(link, child[:src]), nil, true))]] # ... or wait, do we giveup?
|
355
|
+
rescue => e
|
356
|
+
DirectLink.logger.error e
|
357
|
+
[]
|
357
358
|
end
|
358
359
|
end
|
359
360
|
end
|
@@ -363,6 +364,7 @@ def DirectLink link, max_redirect_resolving_retry_delay = nil, giveup = false
|
|
363
364
|
return DirectLink t[:content], nil, true
|
364
365
|
end.max_by{ |_, v| v.map{ |i| i.width * i.height }.inject(:+) / v.size }.last
|
365
366
|
else
|
367
|
+
# TODO: maybe move this to right before `rescue` line
|
366
368
|
w, h = f.size
|
367
369
|
struct.new link, w, h, f.type
|
368
370
|
end
|
data/test.rb
CHANGED
@@ -412,8 +412,11 @@ describe DirectLink do
|
|
412
412
|
tries += 1
|
413
413
|
raise JSON::ParserError
|
414
414
|
end
|
415
|
+
# this gem call should never return success -- it should experience JSON::ParserError
|
416
|
+
# so I'm not much sure why do we need this line
|
415
417
|
m.call *args
|
416
418
|
} do
|
419
|
+
# why?
|
417
420
|
t = ENV.delete "REDDIT_SECRETS"
|
418
421
|
begin
|
419
422
|
DirectLink.reddit link, 3 # do not remove `4` or test will hang
|
@@ -427,11 +430,17 @@ describe DirectLink do
|
|
427
430
|
end
|
428
431
|
it "Reddit correctly parses out id when no token provided" do
|
429
432
|
t = ENV.delete "REDDIT_SECRETS"
|
430
|
-
|
431
|
-
assert_equal "https://
|
432
|
-
|
433
|
-
|
434
|
-
|
433
|
+
FastImage.stub :new, lambda{ |link, *|
|
434
|
+
assert_equal "https://v.redd.it/2tyovczka8m11/DASH_4_8_M", link
|
435
|
+
throw :_
|
436
|
+
} do
|
437
|
+
begin
|
438
|
+
catch :_ do
|
439
|
+
DirectLink "https://www.reddit.com/r/gifs/comments/9ftc8f/low_pass_wake_vortices/?st=JM2JIKII&sh=c00fea4f"
|
440
|
+
end
|
441
|
+
ensure
|
442
|
+
ENV["REDDIT_SECRETS"] = t
|
443
|
+
end
|
435
444
|
end
|
436
445
|
end
|
437
446
|
it "it is really impossible to get dimensions from the shitty Reddit media hosting" do
|
@@ -519,10 +528,15 @@ describe DirectLink do
|
|
519
528
|
end
|
520
529
|
|
521
530
|
describe "does not shadow the internal exception" do
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
531
|
+
[
|
532
|
+
SocketError,
|
533
|
+
Errno::ECONNRESET,
|
534
|
+
].each do |exception|
|
535
|
+
it "raises #{exception} from the redirect resolving stage" do
|
536
|
+
assert_raises exception do
|
537
|
+
NetHTTPUtils.stub :get_response, ->*{ raise exception.new } do
|
538
|
+
DirectLink "http://example.com/404"
|
539
|
+
end
|
526
540
|
end
|
527
541
|
end
|
528
542
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: directlink
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.4.
|
4
|
+
version: 0.0.4.5
|
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: 2018-
|
11
|
+
date: 2018-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fastimage
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 1.6.
|
47
|
+
version: 1.6.9
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 1.6.
|
54
|
+
version: 1.6.9
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: kramdown
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|