directlink 0.0.8.8 → 0.0.9.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: '097dd2d9c1fd6437aeeda4edb35f26ba8cbac99c'
4
- data.tar.gz: 191d639d9b83f310d2497f735257d34868694da9
3
+ metadata.gz: 2fd631d28a9163381634ef669e093e592f930ded
4
+ data.tar.gz: 0b50f85bbfc1650fd3c600a087088bce1d804c7b
5
5
  SHA512:
6
- metadata.gz: 7be490bc74cb4dd909cfc866735170c26bdf498047aa61ad8c6f22f7d3131f8b6ae532f228a07174dabf763248d99c60b02a16b7f4b464b58308c2d2359cc833
7
- data.tar.gz: b92394db6e24b26553b93d87af54ceecc9ee4b079de6c2d2823ce65a995022e870006045b4b4b4f24aedba53822499a4ac9b3a6402c90e61bcad703b86caf0a0
6
+ metadata.gz: 0f37f81c0cc925df667eddca3ce2bf2c801438712deb22547acd2bc96f314728a377e1efbfc00c35304ed308b9bbf2f481a07a65edaf0626baf2e37b51044c84
7
+ data.tar.gz: fddbc5f906b2b9b49434dd7d0e2bb9fbffd5119defd5494b5b849ff8eb7ff201275b2e2b271db6f80506641bb3dddbce046f482db387671753b4fc53acc9f23f
@@ -8,6 +8,8 @@ os:
8
8
  - osx
9
9
  rvm:
10
10
  - ruby-head
11
+ - 2.7
12
+ - 2.6
11
13
  - 2.5
12
14
  - 2.4
13
15
  - 2.3
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "directlink"
3
- spec.version = "0.0.8.8"
3
+ spec.version = "0.0.9.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"
@@ -137,7 +137,7 @@ module DirectLink
137
137
  end
138
138
 
139
139
  def self._500px link
140
- raise ErrorBadLink.new link unless %r{\Ahttps://500px\.com/photo/(?<id>[^/]+)/[^/]+\z} =~ link
140
+ raise ErrorBadLink.new link unless %r{\Ahttps://500px\.com/photo/(?<id>[^/]+)/[-[a-zA-Z]]+\/?\z} =~ link
141
141
  require "nokogiri"
142
142
  resp = NetHTTPUtils.request_data link
143
143
  f = lambda do |form|
@@ -236,7 +236,7 @@ module DirectLink
236
236
 
237
237
  def self.vk link
238
238
  id, mtd, field, f = case link
239
- when %r{\Ahttps://vk\.com/id(?<user_id>\d+)\?z=photo(?<id>\k<user_id>_\d+)(%2F(album\k<user_id>_0|photos\k<user_id>))\z},
239
+ when %r{\Ahttps://vk\.com/id(?<user_id>\d+)\?z=photo(?<id>\k<user_id>_\d+)(%2F(album\k<user_id>_0|photos\k<user_id>))?\z},
240
240
  %r{\Ahttps://vk\.com/[a-z_]+\?z=photo(?<_>)(?<id>(?<user_id>\d+)_\d+)%2Fphotos\k<user_id>\z},
241
241
  %r{\Ahttps://vk\.com/photo(?<_>)(?<id>-?\d+_\d+)(\?all=1)?\z},
242
242
  %r{\Ahttps://vk\.com/feed\?section=likes&z=photo(?<_>)(?<id>-(?<user_id>\d+)_\d+)%2F(liked\d+|album\k<user_id>_0)\z},
@@ -244,15 +244,22 @@ module DirectLink
244
244
  %r{\Ahttps://vk\.com/wall(?<user_id>-\d+)_\d+\?z=photo(?<id>\k<user_id>_\d+)%2F(wall\k<user_id>_\d+|album\k<user_id>_00%2Frev|\d+)\z}
245
245
  [$2, :photos, :photos, lambda do |t|
246
246
  raise ErrorAssert.new "our knowledge about VK API seems to be outdated" unless 1 == t.size
247
- t.first
247
+ t
248
248
  end ]
249
- when %r{\Ahttps://vk\.com/wall(?<id>-\d+_\d+)\z}
249
+ when %r{\Ahttps://vk\.com/wall(?<id>-?\d+_\d+)\z},
250
+ %r{\Ahttps://vk\.com/[a-z\.]+\?w=wall(?<id>\d+_\d+)\z}
250
251
  [$1, :wall, :posts, lambda do |t|
251
- t.first.fetch("attachments").tap do |t|
252
- raise ErrorAssert.new "our knowledge about VK API seems to be outdated" unless 1 == t.size
253
- end.first.tap do |t|
254
- raise ErrorAssert.new "our knowledge about VK API seems to be outdated" unless %w{ type photo } == t.keys
255
- end.fetch("photo")
252
+ t.first.fetch("attachments").select do |item|
253
+ case item.keys
254
+ when %w{ type photo }
255
+ raise ErrorAssert.new "our knowledge about VK API seems to be outdated" unless item["type"] == "photo"
256
+ next true
257
+ when %w{ type audio }
258
+ raise ErrorAssert.new "our knowledge about VK API seems to be outdated" unless item["type"] == "audio"
259
+ else
260
+ raise ErrorAssert.new "our knowledge about VK API seems to be outdated"
261
+ end
262
+ end.map{ |i| i.fetch "photo" }
256
263
  end ]
257
264
  else
258
265
  raise ErrorBadLink.new link
@@ -261,12 +268,14 @@ module DirectLink
261
268
  sleep 0.25 # "error_msg"=>"Too many requests per second"
262
269
  f.call( JSON.load( NetHTTPUtils.request_data "https://api.vk.com/method/#{mtd}.getById",
263
270
  :POST, form: { field => id, :access_token => ENV["VK_ACCESS_TOKEN"], :client_secret => ENV["VK_CLIENT_SECRET"], :v => "5.101" }
264
- ).fetch("response") ).fetch("sizes").map do |s|
265
- s.values_at("width", "height", "url").tap do |whu|
266
- w, h, u = whu
267
- whu[0, 2] = FastImage.new(u, raise_on_failure: true).size if [w, h].include? 0
268
- end
269
- end.max_by{ |w, h, u| w * h }
271
+ ).fetch("response") ).map do |photos|
272
+ photos.fetch("sizes").map do |size|
273
+ size.values_at("width", "height", "url").tap do |whu|
274
+ w, h, u = whu
275
+ whu[0, 2] = FastImage.new(u, raise_on_failure: true).size if [w, h].include? 0
276
+ end
277
+ end.max_by{ |w, h, u| w * h }
278
+ end
270
279
  end
271
280
 
272
281
  class_variable_set :@@directlink, Struct.new(:url, :width, :height, :type)
@@ -381,8 +390,9 @@ def DirectLink link, timeout = nil, giveup: false, ignore_meta: false
381
390
  %w{ redd it } == URI(link).host.split(?.)
382
391
 
383
392
  begin
384
- w, h, u = DirectLink.vk(link)
385
- return struct.new u, w, h
393
+ return DirectLink.vk(link).map do |w, h, u|
394
+ struct.new u, w, h
395
+ end
386
396
  rescue DirectLink::ErrorMissingEnvVar
387
397
  end if %w{ vk com } == URI(link).host.split(?.)
388
398
 
@@ -419,9 +429,10 @@ def DirectLink link, timeout = nil, giveup: false, ignore_meta: false
419
429
  end
420
430
  end
421
431
  end
422
- l[html].group_by(&:first).map{ |k, v| [k.join(?>), v.map(&:last)] }.tap do |results|
423
- raise if results.empty?
424
- end.max_by{ |_, v| v.map{ |i| i.width * i.height }.inject(:+) / v.size }.last
432
+ l[html].
433
+ tap{ |results| raise if results.empty? }.
434
+ group_by(&:first).map{ |k, v| [k.join(?>), v.map(&:last)] }.
435
+ max_by{ |_, v| v.map{ |i| i.width * i.height }.inject(:+) }.last
425
436
  else
426
437
  # TODO: maybe move this to right before `rescue` line
427
438
  w, h = f.size
data/test.rb CHANGED
@@ -363,6 +363,7 @@ describe DirectLink do
363
363
  [
364
364
  [ :_500px, [
365
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
+ ["https://500px.com/photo/1017579834/-poppies-flowers-by-David-Dubnitskiy/", [1819, 2500, "https://drscdn.500px.org/photo/1017579834/m%3D900/v2?sig=022e3e9dd836ffd8c1d31ae26c83735e4e42b4c8733d0c4380d8270aebbca44e", "jpeg"]],
366
367
  ] ],
367
368
  [ :flickr, [
368
369
  ["https://www.flickr.com/photos/tomas-/17220613278/", DirectLink::ErrorNotFound],
@@ -395,16 +396,18 @@ describe DirectLink do
395
396
  ["https://www.reddit.com/r/hangers/comments/97you5/tara_radovic/", [true, "https://i.imgur.com/rbLqgOu.jpg"]], # "crossport" from Imgur
396
397
  ] ],
397
398
  [ :vk, [
398
- ["https://vk.com/wall-105984091_7806", [960, 1280, "https://sun9-41.userapi.com/c855224/v855224900/a72f1/7OZ8ux9Wcwo.jpg"]],
399
- ["https://vk.com/id57030827?z=photo57030827_456241143%2Falbum57030827_0", [1920, 1440, "https://sun9-66.userapi.com/c845322/v845322944/167836/bP9z41BybhI.jpg"]],
400
- ["https://vk.com/wall-185182611_454?z=photo-185182611_457239340%2Fwall-185182611_454", [1280, 960, "https://sun9-46.userapi.com/c851028/v851028578/1a62f6/VB4SdR1O6Tg.jpg"]],
401
- ["https://vk.com/wall-105984091_7946?z=photo-105984091_457243312%2Falbum-105984091_00%2Frev", [1280, 875, "https://sun9-37.userapi.com/c852020/v852020134/1b6b36/0IsDFb-Hda4.jpg"]],
402
- ["https://vk.com/photo533531776_456239427?all=1", [750, 938, "https://sun9-25.userapi.com/c849416/v849416600/14b949/V01Ch1gYjhc.jpg"]],
403
- ["https://vk.com/photo-155488973_456242404", [1486, 1000, "https://sun9-7.userapi.com/c852132/v852132877/8578e/m6AJWiskiKE.jpg"]],
404
- ["https://vk.com/id2272074?z=photo2272074_264578776%2Fphotos2272074", [604, 484, "https://sun9-10.userapi.com/c10472/u2272074/-7/x_407b2ba2.jpg"]],
405
- ["https://vk.com/feed?section=likes&z=photo-117564754_456261460%2Fliked3902406", [1024, 1335, "https://sun9-72.userapi.com/c854028/v854028353/895b6/izQJresLdf0.jpg"]],
406
- ["https://vk.com/likizimy?z=photo-42543351_456239941%2Fwall-42543351_1908", [1179, 1731, "https://sun9-47.userapi.com/c855036/v855036571/60f7b/ryCPJIMyMkI.jpg"]],
407
- ["https://vk.com/e_rod?z=photo298742340_457247118%2Fphotos298742340", [1728, 2160, "https://sun9-53.userapi.com/c858320/v858320596/c7714/oImGe4o1ZJI.jpg"]],
399
+ ["https://vk.com/wall-105984091_7806", [[960, 1280, "https://sun9-41.userapi.com/c855224/v855224900/a72f1/7OZ8ux9Wcwo.jpg"]]],
400
+ ["https://vk.com/wall298742340_4715", [[1080, 1080, "https://sun9-24.userapi.com/c857136/v857136625/15e38b/CsCqsJD174A.jpg"]]],
401
+ ["https://vk.com/id57030827?z=photo57030827_456241143%2Falbum57030827_0", [[1920, 1440, "https://sun9-66.userapi.com/c845322/v845322944/167836/bP9z41BybhI.jpg"]]],
402
+ ["https://vk.com/id57030827?z=photo57030827_456241143", [[1920, 1440, "https://sun9-66.userapi.com/c845322/v845322944/167836/bP9z41BybhI.jpg"]]],
403
+ ["https://vk.com/wall-185182611_454?z=photo-185182611_457239340%2Fwall-185182611_454", [[1280, 960, "https://sun9-46.userapi.com/c851028/v851028578/1a62f6/VB4SdR1O6Tg.jpg"]]],
404
+ ["https://vk.com/wall-105984091_7946?z=photo-105984091_457243312%2Falbum-105984091_00%2Frev", [[1280, 875, "https://sun9-37.userapi.com/c852020/v852020134/1b6b36/0IsDFb-Hda4.jpg"]]],
405
+ ["https://vk.com/photo533531776_456239427?all=1", [[750, 938, "https://sun9-25.userapi.com/c849416/v849416600/14b949/V01Ch1gYjhc.jpg"]]],
406
+ ["https://vk.com/photo-155488973_456242404", [[1486, 1000, "https://sun9-7.userapi.com/c852132/v852132877/8578e/m6AJWiskiKE.jpg"]]],
407
+ ["https://vk.com/id2272074?z=photo2272074_264578776%2Fphotos2272074", [[604, 484, "https://sun9-10.userapi.com/c10472/u2272074/-7/x_407b2ba2.jpg"]]],
408
+ ["https://vk.com/feed?section=likes&z=photo-117564754_456261460%2Fliked3902406", [[1024, 1335, "https://sun9-72.userapi.com/c854028/v854028353/895b6/izQJresLdf0.jpg"]]],
409
+ ["https://vk.com/likizimy?z=photo-42543351_456239941%2Fwall-42543351_1908", [[1179, 1731, "https://sun9-47.userapi.com/c855036/v855036571/60f7b/ryCPJIMyMkI.jpg"]]],
410
+ ["https://vk.com/e_rod?z=photo298742340_457247118%2Fphotos298742340", [[1728, 2160, "https://sun9-53.userapi.com/c858320/v858320596/c7714/oImGe4o1ZJI.jpg"]]],
408
411
  ] ],
409
412
  ].each do |method, tests|
410
413
  next if method == :vk && ENV.include?("TRAVIS")
@@ -441,7 +444,7 @@ describe DirectLink do
441
444
  ["https://goo.gl/ySqUb5", "https://i.imgur.com/QpOBvRY.png"],
442
445
  ],
443
446
  _500px: [
444
- %w{ https://500px.com/photo/112134597/milky-way-by-tom-hall https://web.500px.com/photo/112134597/milky-way-by-tom-hall? },
447
+ %w{ https://500px.com/photo/112134597/milky-way-by-tom-hall https://500px.com/photo/112134597/milky-way-by-tom-hall },
445
448
  ],
446
449
  flickr: [
447
450
  "https://www.flickr.com/photos/59880970@N07/15773941043/in/dateposted-public/",
@@ -648,7 +651,7 @@ describe DirectLink do
648
651
 
649
652
  describe "other domains tests" do
650
653
  [
651
- ["http://www.aeronautica.difesa.it/organizzazione/REPARTI/divolo/PublishingImages/6%C2%B0%20Stormo/2013-decollo%20al%20tramonto%20REX%201280.jpg", ["http://www.aeronautica.difesa.it/organizzazione/REPARTI/divolo/PublishingImages/6%C2%B0%20Stormo/2013-decollo%20al%20tramonto%20REX%201280.jpg", 1280, 853, :jpeg], nil, 1],
654
+ # ["http://www.aeronautica.difesa.it/organizzazione/REPARTI/divolo/PublishingImages/6%C2%B0%20Stormo/2013-decollo%20al%20tramonto%20REX%201280.jpg", ["http://www.aeronautica.difesa.it/organizzazione/REPARTI/divolo/PublishingImages/6%C2%B0%20Stormo/2013-decollo%20al%20tramonto%20REX%201280.jpg", 1280, 853, :jpeg], nil, 1], # website is dead?
652
655
  # ["http://minus.com/lkP3hgRJd9npi", SocketError, /nodename nor servname provided, or not known|No address associated with hostname/, 0],
653
656
  ["http://www.cutehalloweencostumeideas.org/wp-content/uploads/2017/10/Niagara-Falls_04.jpg", SocketError, /nodename nor servname provided, or not known|Name or service not known/, 0],
654
657
  ].each_with_index do |(input, expectation, message_string_or_regex, max_redirect_resolving_retry_delay), i|
@@ -674,12 +677,12 @@ describe DirectLink do
674
677
  describe "giving up" do
675
678
  [
676
679
  ["http://example.com", FastImage::UnknownImageType],
677
- ["https://www.tic.com/index.html", FastImage::UnknownImageType, true],
678
- ["https://www.tic.com/index.html", 2],
680
+ # ["https://www.tic.com/index.html", FastImage::UnknownImageType, true], # needs new test or stub
681
+ # ["https://www.tic.com/index.html", 2], # needs new test or stub
679
682
  ["http://imgur.com/HQHBBBD", FastImage::UnknownImageType, true],
680
683
  ["http://imgur.com/HQHBBBD", "https://i.imgur.com/HQHBBBD.jpg?fb"], # .at_css("meta[@property='og:image']")
681
684
  ["https://www.deviantart.com/nadyasonika/art/Asuka-Langley-Beach-Time-590134861", FastImage::UnknownImageType, true],
682
- ["https://www.deviantart.com/nadyasonika/art/Asuka-Langley-Beach-Time-590134861", "https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/943f66cb-78ad-40f2-a086-44420b98b431/d9rcmz1-5cbc5670-0193-485b-ac14-755ddb9562f4.jpg/v1/fill/w_1024,h_732,q_75,strp/asuka_langley_beach_time_by_nadyasonika_d9rcmz1-fullview.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7ImhlaWdodCI6Ijw9NzMyIiwicGF0aCI6IlwvZlwvOTQzZjY2Y2ItNzhhZC00MGYyLWEwODYtNDQ0MjBiOThiNDMxXC9kOXJjbXoxLTVjYmM1NjcwLTAxOTMtNDg1Yi1hYzE0LTc1NWRkYjk1NjJmNC5qcGciLCJ3aWR0aCI6Ijw9MTAyNCJ9XV0sImF1ZCI6WyJ1cm46c2VydmljZTppbWFnZS5vcGVyYXRpb25zIl19.M-a_heYtVPwnR4eC9KIPk2mIYeNzEaTg4b4jqr_GwyI"],
685
+ ["https://www.deviantart.com/nadyasonika/art/Asuka-Langley-Beach-Time-590134861", "https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/943f66cb-78ad-40f2-a086-44420b98b431/d9rcmz1-5cbc5670-0193-485b-ac14-755ddb9562f4.jpg/v1/fill/w_1024,h_732,q_75,strp/asuka_langley_beach_time_by_nadyasonika_d9rcmz1-fullview.jpg?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOiIsImlzcyI6InVybjphcHA6Iiwib2JqIjpbW3siaGVpZ2h0IjoiPD03MzIiLCJwYXRoIjoiXC9mXC85NDNmNjZjYi03OGFkLTQwZjItYTA4Ni00NDQyMGI5OGI0MzFcL2Q5cmNtejEtNWNiYzU2NzAtMDE5My00ODViLWFjMTQtNzU1ZGRiOTU2MmY0LmpwZyIsIndpZHRoIjoiPD0xMDI0In1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmltYWdlLm9wZXJhdGlvbnMiXX0.L6OhXuQZ_9ovKOdjjuQbvxpD0mG8M_KiqV4ljEDfW3Q"],
683
686
  ["https://calgary.skyrisecities.com/news/2019/11/blue-morning-light", "https://cdn.skyrisecities.com/sites/default/files/images/articles/2019/11/39834/39834-132071.jpg"], # og:image without scheme
684
687
  ["https://www.reddit.com/r/darksouls3/comments/e59djh/hand_it_over_that_thing_your_wallpaper/", DirectLink::ErrorBadLink, true],
685
688
  ["https://www.reddit.com/r/darksouls3/comments/e59djh/hand_it_over_that_thing_your_wallpaper/", 6],
@@ -691,14 +694,14 @@ describe DirectLink do
691
694
  case expectation
692
695
  when Class
693
696
  e = assert_raises expectation, "for #{input} (giveup = #{giveup})" do
694
- DirectLink input, nil, giveup: giveup
697
+ DirectLink input, 10, giveup: giveup
695
698
  end
696
699
  assert_equal expectation.to_s, e.class.to_s, "for #{input} (giveup = #{giveup})"
697
700
  when String
698
- result = DirectLink input, nil, giveup: giveup
701
+ result = DirectLink input, 10, giveup: giveup
699
702
  assert_equal expectation, result.url, "for #{input} (giveup = #{giveup})"
700
703
  else
701
- result = DirectLink input, nil, giveup: giveup
704
+ result = DirectLink input, 10, giveup: giveup
702
705
  result = [result] unless result.is_a? Array # we can't do `Array(<Struct>)` because it splats by elements
703
706
  assert_equal expectation, result.size, ->{
704
707
  "for #{input} (giveup = #{giveup}): #{result.map &:url}"
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.8.8
4
+ version: 0.0.9.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: 2020-02-26 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: fastimage