directlink 0.0.6.1 → 0.0.7.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: 1b33e244fed09c86b5d956379f5030f225a9cb59
4
- data.tar.gz: 4324a01c311800d7d28104378175c9894635dffb
3
+ metadata.gz: 82fb69a1c4a926a14a776f62c647ca40cb4ffcbb
4
+ data.tar.gz: 904eafe032fcd4947770212b0e97788102b41811
5
5
  SHA512:
6
- metadata.gz: eaa015a87590f251a650cca774af4e1dea7083eaeb73690d121fed151b700241f09dc9f509229a8eb121c43a26e7f1b6f646e99049d573d27cd3c1ab89cce9ce
7
- data.tar.gz: 23fc26919824925f0774b293f685d4fe5036633ec9b67e11f6d1f5add99d3651a59c2cb144d4246a28cccbbe2f2f394d5e6d5fff78dbb3026f8169d5a03180fd
6
+ metadata.gz: 4d9e95ecb4fec1fd2e7b633b1e7c817a187ab3078af80e2721d2f6e15f7c8da031963c35891dbc36dd1eb9d1681b999abe8421f520e8a0bbe8968dc6a8b23b7d
7
+ data.tar.gz: 244fcccee54d13bd4e2c7d219c67b92174441fe034fee6098c158947d1d278bc58fdcc6c3a53b3831dad419eb70f88fbe6ace14688535d4c23a86be028f0d905
data/README.md CHANGED
@@ -84,11 +84,21 @@ $ directlink --json https://imgur.com/a/oacI3gl https://avatars1.githubuserconte
84
84
  }
85
85
  ]
86
86
  ```
87
+ By default tries to parse the `<meta property="og:image"` tag in the "don't give up" mode but can also ignore it:
88
+ ```
89
+ $ directlink --ignore-meta https://www.kp.ru/daily/26342.7/3222103/
90
+ <= https://www.kp.ru/daily/26342.7/3222103/
91
+ => https://s11.stc.all.kpcdn.net/share/i/12/8024261/wx1080.jpg
92
+ jpeg 1080x653
93
+ => https://s13.stc.all.kpcdn.net/share/i/12/8024233/wx1080.jpg
94
+ jpeg 1080x653
95
+ ...
96
+ ```
87
97
  Downloads `master:HEAD` version of `lib/directlink.rb` from GitHub and uses it once instead of installed one:
88
98
  ```
89
99
  $ directlink --github https://imgur.com/a/oacI3gl
90
100
  ```
91
- When an image hosting with known API is recognized, it will try to use the API tokens you've provided as env vars (otherwise it will go "don't give up" mode):
101
+ When an image hosting with known API is recognized, it will try to use the API tokens you've provided as env vars (otherwise it will go the "don't give up" mode):
92
102
  ```
93
103
  $ export IMGUR_CLIENT_ID=0f99cd781...
94
104
  $ export FLICKR_API_KEY=dc2bfd348b...
data/bin/directlink CHANGED
@@ -12,15 +12,13 @@ end
12
12
 
13
13
 
14
14
  # TODO: use OptionParser?
15
- debug = json = github = nil
15
+ debug = json = github = ignore_meta = nil
16
16
  loop do
17
17
  case ARGV.first
18
- when "--debug"
19
- debug = ARGV.shift
20
- when "--json"
21
- json = ARGV.shift
22
- when "--github"
23
- github = ARGV.shift
18
+ when "--debug" ; debug = ARGV.shift
19
+ when "--json" ; json = ARGV.shift
20
+ when "--github" ; github = ARGV.shift
21
+ when "--ignore-meta" ; ignore_meta = ARGV.shift
24
22
  else
25
23
  break
26
24
  end
@@ -46,7 +44,7 @@ end
46
44
  NetHTTPUtils.logger.level = debug ? Logger::INFO : Logger::FATAL unless ENV.include? "LOGLEVEL_NetHTTPUtils"
47
45
  # TODO --timeout=
48
46
  DirectLink.silent = !debug
49
- abort "usage: directlink [--debug] [--json] [--github] <link1> <link2> <link3> ...\n#{
47
+ abort "usage: directlink [--debug] [--json] [--github] [--ignore-meta] <link1> <link2> <link3> ...\n#{
50
48
  if github
51
49
  "impossible to tell version for sure from raw.githubusercontent.com"
52
50
  else
@@ -58,13 +56,13 @@ begin
58
56
  if json
59
57
  require "json"
60
58
  t = ARGV.map do |link|
61
- t = DirectLink link, 30
59
+ t = DirectLink link, 30, ignore_meta: ignore_meta
62
60
  t.is_a?(Array) ? t.map(&:to_h) : t.to_h
63
61
  end
64
62
  puts JSON.pretty_generate t.size == 1 ? t.first : t
65
63
  else
66
64
  ARGV.each do |link|
67
- t = DirectLink link, 30
65
+ t = DirectLink link, 30, ignore_meta: ignore_meta
68
66
  puts "<= #{link}"
69
67
  (t.is_a?(Array) ? t : [t]).each{ |s| puts "=> #{s.url}\n #{s.type} #{s.width}x#{s.height}" }
70
68
  end
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.6.1"
3
+ spec.version = "0.0.7.0"
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"
@@ -16,6 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.add_dependency "kramdown"
17
17
  spec.add_dependency "addressable"
18
18
  spec.add_development_dependency "minitest"
19
+ spec.add_development_dependency "byebug"
19
20
 
20
21
  spec.require_path = "lib"
21
22
  spec.bindir = "bin"
data/lib/directlink.rb CHANGED
@@ -247,7 +247,7 @@ end
247
247
 
248
248
  require "fastimage"
249
249
 
250
- def DirectLink link, max_redirect_resolving_retry_delay = nil, giveup = false
250
+ def DirectLink link, max_redirect_resolving_retry_delay = nil, giveup: false, ignore_meta: false
251
251
  ArgumentError.new("link should be a <String>, not <#{link.class}>") unless link.is_a? String
252
252
  begin
253
253
  URI link
@@ -340,7 +340,7 @@ def DirectLink link, max_redirect_resolving_retry_delay = nil, giveup = false
340
340
  f = ->_{ _.type == :a ? _.attr["href"] : _.children.flat_map(&f) }
341
341
  require "kramdown"
342
342
  return f[Kramdown::Document.new(u).root].map do |sublink|
343
- DirectLink URI.join(link, sublink).to_s, max_redirect_resolving_retry_delay, giveup
343
+ DirectLink URI.join(link, sublink).to_s, max_redirect_resolving_retry_delay, giveup: giveup
344
344
  end
345
345
  end
346
346
  return struct.new *u.values_at(*%w{ fallback_url width height }), "video" if u.is_a? Hash
@@ -363,14 +363,14 @@ def DirectLink link, max_redirect_resolving_retry_delay = nil, giveup = false
363
363
  end
364
364
  html = Nokogiri::HTML NetHTTPUtils.request_data link, header: {"User-Agent" => "Mozilla"}
365
365
  if t = html.at_css("meta[@property='og:image']")
366
- return DirectLink t[:content], nil, true
367
- end
366
+ return DirectLink t[:content], nil, giveup: true
367
+ end unless ignore_meta
368
368
  h = {} # TODO: maybe move it outside because of possible img[:src] recursion?...
369
369
  l = lambda do |node, s = []|
370
370
  node.element_children.flat_map do |child|
371
371
  next l[child, s + [child.node_name]] unless "img" == child.node_name
372
372
  begin
373
- [[s, (h[child[:src]] = h[child[:src]] || DirectLink(URI.join(link, child[:src]).to_s, nil, true))]] # ... or wait, do we giveup?
373
+ [[s, (h[child[:src]] = h[child[:src]] || DirectLink(URI.join(link, child[:src]).to_s, nil, giveup: true))]] # ... or wait, do we giveup?
374
374
  rescue => e
375
375
  DirectLink.logger.error "#{e} (from no giveup)"
376
376
  []
data/test.rb CHANGED
@@ -362,17 +362,17 @@ describe DirectLink do
362
362
  # and maaaaybe move some tests from here to the context about giveup
363
363
  [
364
364
  [ :_500px, [
365
- ["https://500px.com/photo/264092015/morning-rider-by-tiger-seo", [1200, 800, "https://drscdn.500px.org/photo/264092015/m%3D1200/v2?webp=true&sig=49c6f8346ba8453ccb17208d4653b9e11bc3e1bb8c21c161047e2842716f3649", "jpeg"]],
365
+ ["https://500px.com/photo/264092015/morning-rider-by-tiger-seo", [1200, 800, "https://drscdn.500px.org/photo/264092015/m%3D900/v2?sig=68a9206477f573d8e2838faa6a929e7267f22dc5f9e98f1771f7a8a63efa2ed7", "jpeg"]],
366
366
  ] ],
367
367
  [ :flickr, [
368
368
  ["https://www.flickr.com/photos/tomas-/17220613278/", DirectLink::ErrorNotFound],
369
369
  ["https://www.flickr.com/photos/16936123@N07/18835195572", DirectLink::ErrorNotFound],
370
370
  ["https://www.flickr.com/photos/44133687@N00/17380073505/", [3000, 2000, "https://live.staticflickr.com/7757/17380073505_ed5178cc6a_o.jpg"]], # trailing slash
371
- ["https://www.flickr.com/photos/jacob_schmidt/18414267018/in/album-72157654235845651/", DirectLink::ErrorNotFound], # username in-album
372
- ["https://www.flickr.com/photos/tommygi/5291099420/in/dateposted-public/", [1600, 1062, "https://live.staticflickr.com/5249/5291099420_3bf8f43326_o.jpg"]], # username in-public
371
+ ["https://www.flickr.com/photos/jacob_schmidt/18414267018/in/album-72157654235845651/", DirectLink::ErrorNotFound], # username in-album
372
+ ["https://www.flickr.com/photos/tommygi/5291099420/in/dateposted-public/", [1600, 1062, "https://live.staticflickr.com/5249/5291099420_29fae96e38_h.jpg"]], # username in-public
373
373
  ["https://www.flickr.com/photos/132249412@N02/18593786659/in/album-72157654521569061/", DirectLink::ErrorNotFound],
374
374
  ["https://www.flickr.com/photos/130019700@N03/18848891351/in/dateposted-public/", [4621, 3081, "https://live.staticflickr.com/3796/18848891351_f751b35aeb_o.jpg"]], # userid in-public
375
- ["https://www.flickr.com/photos/frank3/3778768209/in/photolist-6KVb92-eCDTCr-ur8K-7qbL5z-c71afh-c6YvXW-7mHG2L-c71ak9-c71aTq-c71azf-c71aq5-ur8Q-6F6YkR-eCDZsD-eCEakg-eCE6DK-4ymYku-7ubEt-51rUuc-buujQE-ur8x-9fuNu7-6uVeiK-qrmcC6-ur8D-eCEbei-eCDY9P-eCEhCk-eCE5a2-eCH457-eCHrcq-eCEdZ4-eCH6Sd-c71b5o-c71auE-eCHa8m-eCDSbz-eCH1dC-eCEg3v-7JZ4rh-9KwxYL-6KV9yR-9tUSbU-p4UKp7-eCHfwS-6KVbAH-5FrdbP-eeQ39v-eeQ1UR-4jHAGN", [1024, 681, "https://live.staticflickr.com/2499/3778768209_280f82abab_b.jpg"]],
375
+ ["https://www.flickr.com/photos/frank3/3778768209/in/photolist-6KVb92-eCDTCr-ur8K-7qbL5z-c71afh-c6YvXW-7mHG2L-c71ak9-c71aTq-c71azf-c71aq5-ur8Q-6F6YkR-eCDZsD-eCEakg-eCE6DK-4ymYku-7ubEt-51rUuc-buujQE-ur8x-9fuNu7-6uVeiK-qrmcC6-ur8D-eCEbei-eCDY9P-eCEhCk-eCE5a2-eCH457-eCHrcq-eCEdZ4-eCH6Sd-c71b5o-c71auE-eCHa8m-eCDSbz-eCH1dC-eCEg3v-7JZ4rh-9KwxYL-6KV9yR-9tUSbU-p4UKp7-eCHfwS-6KVbAH-5FrdbP-eeQ39v-eeQ1UR-4jHAGN", [2048, 1361, "https://live.staticflickr.com/2499/3778768209_7e92a433a8_k.jpg"]],
376
376
  ["https://www.flickr.com/photos/patricksloan/18230541413/sizes/l", [2048, 491, "https://live.staticflickr.com/5572/18230541413_fec4783d79_k.jpg"]],
377
377
  ["https://flic.kr/p/vPvCWJ", [2048, 1365, "https://live.staticflickr.com/507/19572004110_d44d1b4ead_k.jpg"]],
378
378
  ] ],
@@ -389,7 +389,7 @@ describe DirectLink do
389
389
  ["https://i.redd.it/c8rk0kjywhy01.jpg", [true, "https://i.redd.it/c8rk0kjywhy01.jpg"]],
390
390
  ["https://i.redd.it/si758zk7r5xz.jpg", [true, "https://i.redd.it/si758zk7r5xz.jpg"]], # it is 404 but `.reddit` does not care -- it just returns the url
391
391
  ["https://reddit.com/123456", [true, "https://i.ytimg.com/vi/b9upM4RbIeU/hqdefault.jpg"]],
392
- ["https://www.reddit.com/r/travel/988889", [true, "https://i.redd.it/3h5xls6ehrg11.jpg"]],
392
+ # ["https://www.reddit.com/r/travel/988889", [true, "https://i.redd.it/3h5xls6ehrg11.jpg"]],
393
393
  ["http://redd.it/988889", [true, "https://i.redd.it/3h5xls6ehrg11.jpg"]],
394
394
  ["https://www.reddit.com/r/CatsStandingUp/duplicates/abn0ua/cat/", [true, "https://v.redd.it/s9b86afb6w721/DASH_2_4_M?source=fallback"]],
395
395
  ["https://www.reddit.com/r/hangers/comments/97you5/tara_radovic/", [true, "https://i.imgur.com/rbLqgOu.jpg"]], # "crossport" from Imgur
@@ -442,7 +442,7 @@ describe DirectLink do
442
442
  "https://www.reddit.com/r/cacography/comments/32tq0i/c/",
443
443
  ["http://redd.it/32tq0i", "https://www.reddit.com/comments/32tq0i"],
444
444
  ["https://reddit.com/123456", "https://www.reddit.com/r/funny/comments/123456/im_thinking_about_getting_a_dog_and_youtubed_ways/"],
445
- ["https://www.reddit.com/r/travel/988889", "https://www.reddit.com/r/travel/comments/988889/playa_miramar_in_guaymas_sonora/"],
445
+ # ["https://www.reddit.com/r/travel/988889", "https://www.reddit.com/r/travel/comments/988889/playa_miramar_in_guaymas_sonora/"],
446
446
  "https://www.reddit.com/r/KsyushaEgorova/comments/beuqs2/a_little_shy/", # NSFW causes redirect to /over_18? if the special cookie not provided
447
447
  ],
448
448
  }.each do |method, tests|
@@ -563,7 +563,7 @@ describe DirectLink do
563
563
  } do
564
564
  t = ENV.delete "REDDIT_SECRETS"
565
565
  begin
566
- p DirectLink.reddit "https://www.reddit.com/123456"
566
+ DirectLink.reddit "https://www.reddit.com/123456"
567
567
  ensure
568
568
  ENV["REDDIT_SECRETS"] = t
569
569
  end
@@ -671,14 +671,14 @@ describe DirectLink do
671
671
  case expectation
672
672
  when Class
673
673
  e = assert_raises expectation, "for #{input} (giveup = #{giveup})" do
674
- DirectLink input, nil, giveup
674
+ DirectLink input, nil, giveup: giveup
675
675
  end
676
676
  assert_equal expectation.to_s, e.class.to_s, "for #{input} (giveup = #{giveup})"
677
677
  when String
678
- result = DirectLink input, nil, giveup
678
+ result = DirectLink input, nil, giveup: giveup
679
679
  assert_equal expectation, result.url, "for #{input} (giveup = #{giveup})"
680
680
  else
681
- result = DirectLink input, nil, giveup
681
+ result = DirectLink input, nil, giveup: giveup
682
682
  result = [result] unless result.is_a? Array # we can't do `Array(<Struct>)` because it splats by elements
683
683
  assert_equal expectation, result.size, ->{
684
684
  "for #{input} (giveup = #{giveup}): #{result.map &:url}"
@@ -699,11 +699,11 @@ describe DirectLink do
699
699
 
700
700
  describe "shows usage help if misused" do
701
701
  [
702
- [/\Ausage: directlink \[--debug\] \[--json\] \[--github\] <link1> <link2> <link3> \.\.\.\ndirectlink version \d\.\d\.\d\.\d\d? \(https:\/\/github\.com\/nakilon\/directlink\)\n\z/, nil],
703
- [/\Ausage: directlink \[--debug\] \[--json\] \[--github\] <link1> <link2> <link3> \.\.\.\ndirectlink version \d\.\d\.\d\.\d\d? \(https:\/\/github\.com\/nakilon\/directlink\)\n\z/, "-h"],
704
- [/\Ausage: directlink \[--debug\] \[--json\] \[--github\] <link1> <link2> <link3> \.\.\.\ndirectlink version \d\.\d\.\d\.\d\d? \(https:\/\/github\.com\/nakilon\/directlink\)\n\z/, "--help"],
705
- [/\Ausage: directlink \[--debug\] \[--json\] \[--github\] <link1> <link2> <link3> \.\.\.\ndirectlink version \d\.\d\.\d\.\d\d? \(https:\/\/github\.com\/nakilon\/directlink\)\n\z/, "-v"],
706
- [/\Ausage: directlink \[--debug\] \[--json\] \[--github\] <link1> <link2> <link3> \.\.\.\ndirectlink version \d\.\d\.\d\.\d\d? \(https:\/\/github\.com\/nakilon\/directlink\)\n\z/, "--version"],
702
+ [/\Ausage: directlink \[--debug\] \[--json\] \[--github\] \[--ignore-meta\] <link1> <link2> <link3> \.\.\.\ndirectlink version \d\.\d\.\d\.\d\d? \(https:\/\/github\.com\/nakilon\/directlink\)\n\z/, nil],
703
+ [/\Ausage: directlink \[--debug\] \[--json\] \[--github\] \[--ignore-meta\] <link1> <link2> <link3> \.\.\.\ndirectlink version \d\.\d\.\d\.\d\d? \(https:\/\/github\.com\/nakilon\/directlink\)\n\z/, "-h"],
704
+ [/\Ausage: directlink \[--debug\] \[--json\] \[--github\] \[--ignore-meta\] <link1> <link2> <link3> \.\.\.\ndirectlink version \d\.\d\.\d\.\d\d? \(https:\/\/github\.com\/nakilon\/directlink\)\n\z/, "--help"],
705
+ [/\Ausage: directlink \[--debug\] \[--json\] \[--github\] \[--ignore-meta\] <link1> <link2> <link3> \.\.\.\ndirectlink version \d\.\d\.\d\.\d\d? \(https:\/\/github\.com\/nakilon\/directlink\)\n\z/, "-v"],
706
+ [/\Ausage: directlink \[--debug\] \[--json\] \[--github\] \[--ignore-meta\] <link1> <link2> <link3> \.\.\.\ndirectlink version \d\.\d\.\d\.\d\d? \(https:\/\/github\.com\/nakilon\/directlink\)\n\z/, "--version"],
707
707
  ["DirectLink::ErrorBadLink: \"--\"\n", "--"],
708
708
  ["DirectLink::ErrorBadLink: \"-\"\n", "-"],
709
709
  ["DirectLink::ErrorBadLink: \"-\"\n", "- -"],
@@ -792,6 +792,16 @@ describe DirectLink do
792
792
  assert_equal "<= http://redd.it/997he7\n=> https://i.imgur.com/QpOBvRY.png\n image/png 460x460\n", string
793
793
  end
794
794
 
795
+ # TODO: test about --json
796
+ it "uses <meta> tag" do
797
+ string, status = Open3.capture2e "RUBYOPT='-rbundler/setup' ./bin/directlink --json https://www.kp.ru/daily/26342.7/3222103/"
798
+ assert_equal [0, "https://s9.stc.all.kpcdn.net/share/i/12/8054352/cr-1200-630.wm-asnpmfru-100-tr-0-0.t-13-3222103-ttps-47-8-0083CD-1010-l-85-b-41.t-13-3222103-ttps-47-8-FFF-1010-l-85-b-42.t-207-5-asb-37-10-FFF-788-l-370-t-68.m2018-03-14x02-10-20.jpg"], [status.exitstatus, JSON.load(string).fetch("url")]
799
+ end
800
+ it "ignores <meta> tag" do
801
+ string, status = Open3.capture2e "RUBYOPT='-rbundler/setup' ./bin/directlink --json --ignore-meta https://www.kp.ru/daily/26342.7/3222103/"
802
+ assert_equal [0, 21, "https://s11.stc.all.kpcdn.net/share/i/12/8024261/wx1080.jpg"], [status.exitstatus, JSON.load(string).size, JSON.load(string).first.fetch("url")]
803
+ end
804
+
795
805
  end
796
806
 
797
807
  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.6.1
4
+ version: 0.0.7.0
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: 2019-05-02 00:00:00.000000000 Z
11
+ date: 2019-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fastimage
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description:
112
126
  email: nakilon@gmail.com
113
127
  executables:
@@ -151,7 +165,7 @@ requirements:
151
165
  - 'you may need to create apps and provide their API tokens:'
152
166
  - IMGUR_CLIENT_ID, FLICKR_API_KEY
153
167
  rubyforge_project:
154
- rubygems_version: 2.5.2
168
+ rubygems_version: 2.6.14.4
155
169
  signing_key:
156
170
  specification_version: 4
157
171
  summary: converts any kind of image hyperlink to direct link, type of image and its