directlink 0.0.2.0 → 0.0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e3122bab4547d8bdb684ff41906edddb54030ea
4
- data.tar.gz: 1a14b888b2ce1ee4e5c0f1493cbdfedaca5a49da
3
+ metadata.gz: 4ad94a6b55149c5f3f0a26a021131eff17311d65
4
+ data.tar.gz: d564d355ef4c3447274cd535ca569c3454262411
5
5
  SHA512:
6
- metadata.gz: e2d49ca7a20bfe00eb33128328156f25e8e8703cb2e2ecfad3af876e3e52d8eaa12a64781482acce3767dcc1d114c40462b6f2465b851a4f0f257e971ccbaec3
7
- data.tar.gz: 662c63eaf3d635e9a1b4f642b0787f29045a19ca52c309a383e20aa912804de64a4acd47b36f4cf259647293cfd0a49ec5996195da1ce28d2138a0a5c937c441
6
+ metadata.gz: d310ca90312eca7a4bbfbabe7773887f8cf6649c69fa31dd10bc154137aebd7eedb49413a4b4018783c8fbe8505f293cc696bbeb5dbd0025ae77f4a835d9fad9
7
+ data.tar.gz: 6e5b998c2c7d8006cd9762c4c80a3ce4f6e84887a537ad6712790a11f05b0e808f8e86b20a8f092d1ba8868d8fed390ec9d5be8493e93ecece1229a090ff2008
data/README.md CHANGED
@@ -85,7 +85,6 @@ When an image hosting with known API is recognized, the API will be used and you
85
85
  ```
86
86
  $ export IMGUR_CLIENT_ID=0f99cd781...
87
87
  $ export FLICKR_API_KEY=dc2bfd348b...
88
- $ export _500PX_CONSUMER_KEY=ESkHT...
89
88
  ```
90
89
 
91
90
  ### As a library
@@ -139,5 +138,4 @@ SocketError: Failed to open TCP connection to minus.com:80 (getaddrinfo: nodenam
139
138
  * `module DirectLink` public methods return different sets of properties -- `DirectLink()` unites them
140
139
  * the `DirectLink::ErrorAssert` should never happen and you might report it if it does
141
140
  * style: `@@` and lambdas are used to keep things private
142
- * resolution of 500px.com photo is that you get via Pro membership, not that URL really provides
143
141
  * this gem is a 2 or 3 libraries merged so don't expect tests to be full and consistent
data/bin/directlink CHANGED
@@ -42,7 +42,7 @@ if github
42
42
  else
43
43
  require "directlink"
44
44
  end
45
- NetHTTPUtils.logger.level = Logger::FATAL
45
+ NetHTTPUtils.logger.level = debug ? Logger::INFO : Logger::FATAL
46
46
  # TODO --timeout=
47
47
  DirectLink.silent = !debug
48
48
  abort "usage: directlink [--debug] [--json] [--github] <link1> <link2> <link3> ...\n#{
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.2.0"
3
+ spec.version = "0.0.2.1"
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"
data/lib/directlink.rb CHANGED
@@ -80,27 +80,27 @@ module DirectLink
80
80
  require "json"
81
81
  require "nethttputils"
82
82
 
83
- def self.imgur link
83
+ # TODO make the timeout handling respect the way the Directlink method works with timeouts
84
+ def self.imgur link, timeout = 1000
84
85
  raise ErrorMissingEnvVar.new "define IMGUR_CLIENT_ID env var" unless ENV["IMGUR_CLIENT_ID"]
85
86
 
86
87
  case link
87
88
  when /\Ahttps?:\/\/(?:(?:i|m|www)\.)?imgur\.com\/(a|gallery)\/([a-zA-Z0-9]{5}(?:[a-zA-Z0-9]{2})?)\z/,
88
89
  /\Ahttps?:\/\/imgur\.com\/(gallery)\/([a-zA-Z0-9]{5}(?:[a-zA-Z0-9]{2})?)\/new\z/
89
- timeout = 1
90
+ t = 1
90
91
  json = begin
91
92
  NetHTTPUtils.request_data "https://api.imgur.com/3/#{
92
93
  $1 == "gallery" ? "gallery" : "album"
93
94
  }/#{$2}/0.json", header: { Authorization: "Client-ID #{ENV["IMGUR_CLIENT_ID"]}" }
94
95
  rescue NetHTTPUtils::Error => e
95
- case e.code
96
- when 500
97
- logger.error "retrying in #{timeout} seconds because of Imgur HTTP ERROR 500"
98
- sleep timeout
99
- timeout *= 2
96
+ if 500 == e.code && t < timeout
97
+ logger.error "retrying in #{t} seconds because of Imgur HTTP ERROR 500"
98
+ sleep t
99
+ t *= 2
100
100
  retry
101
- when 404 ; raise ErrorNotFound.new link.inspect
102
- else ; raise ErrorAssert.new "unexpected http error for #{link}"
103
101
  end
102
+ raise ErrorNotFound.new link.inspect if 404 == e.code
103
+ raise ErrorAssert.new "unexpected http error for #{link}"
104
104
  end
105
105
  data = JSON.load(json)["data"]
106
106
  if data["error"]
@@ -122,10 +122,17 @@ module DirectLink
122
122
  /\Ahttps?:\/\/imgur\.com\/([a-zA-Z0-9]{5}(?:[a-zA-Z0-9]{2})?)\z/,
123
123
  /\Ahttps?:\/\/imgur\.com\/([a-zA-Z0-9]{7})(?:\?\S+)?\z/,
124
124
  /\Ahttps?:\/\/imgur\.com\/r\/[0-9_a-z]+\/([a-zA-Z0-9]{7})\z/
125
+ t = 1
125
126
  json = begin
126
127
  NetHTTPUtils.request_data "https://api.imgur.com/3/image/#{$1}/0.json", header: { Authorization: "Client-ID #{ENV["IMGUR_CLIENT_ID"]}" }
127
128
  rescue NetHTTPUtils::Error => e
128
129
  raise ErrorNotFound.new link.inspect if e.code == 404
130
+ if e.code == 400 && t < timeout
131
+ logger.error "retrying in #{t} seconds because of Imgur HTTP ERROR 400"
132
+ sleep t
133
+ t *= 2
134
+ retry
135
+ end
129
136
  raise ErrorAssert.new "unexpected http error for #{link}"
130
137
  end
131
138
  [ JSON.load(json)["data"] ]
@@ -143,6 +150,7 @@ module DirectLink
143
150
 
144
151
  def self._500px link
145
152
  raise ErrorBadLink.new link unless %r{\Ahttps://500px\.com/photo/(?<id>[^/]+)/[^/]+\z} =~ link
153
+ raise ErrorNotFound.new link # 500px.com has deprecated their API
146
154
  raise ErrorMissingEnvVar.new "define _500PX_CONSUMER_KEY env var !!! WARNING: the 500px.com deprecates their API in summer of 2018 !!!" unless ENV["_500PX_CONSUMER_KEY"]
147
155
  JSON.load( NetHTTPUtils.request_data "https://api.500px.com/v1/photos/#{id}", form: {
148
156
  image_size: 2048,
data/test.rb CHANGED
@@ -139,10 +139,11 @@ describe DirectLink do
139
139
  end
140
140
  end
141
141
 
142
- it "ErrorNotFound" do
143
- assert_raises DirectLink::ErrorNotFound do
142
+ it "ErrorNotFound when album is empty" do
143
+ e = assert_raises DirectLink::ErrorNotFound do
144
144
  DirectLink.imgur "https://imgur.com/a/wPi63mj"
145
145
  end
146
+ assert_nil e.cause if Exception.instance_methods.include? :cause # Ruby 2.1
146
147
  end
147
148
 
148
149
  valid_imgur_image_url = "https://i.imgur.com/BLCesav.jpg"
@@ -150,21 +151,34 @@ describe DirectLink do
150
151
  assert_equal [["https://i.imgur.com/BLCesav.jpg", 1000, 1500, "image/jpeg"]],
151
152
  DirectLink.imgur(valid_imgur_image_url)
152
153
  end
153
- it 404 do
154
- assert_raises DirectLink::ErrorNotFound do
155
- NetHTTPUtils.stub :request_data, ->*{ raise NetHTTPUtils::Error.new "", 404 } do
156
- DirectLink.imgur valid_imgur_image_url
157
- end
158
- end
159
- end
160
154
  it 400 do
161
155
  e = assert_raises DirectLink::ErrorAssert do
162
156
  NetHTTPUtils.stub :request_data, ->*{ raise NetHTTPUtils::Error.new "", 400 } do
163
- DirectLink.imgur valid_imgur_image_url
157
+ DirectLink.imgur valid_imgur_image_url, 4 # do not remove `4` or test may hang
164
158
  end
165
159
  end
166
160
  assert_equal 400, e.cause.code if Exception.instance_methods.include? :cause # Ruby 2.1
167
161
  end
162
+ it "does not 400 after a successfull retry" do
163
+ f = false
164
+ m = NetHTTPUtils.method :request_data
165
+ NetHTTPUtils.stub :request_data, lambda{ |*args|
166
+ f ^= true
167
+ raise NetHTTPUtils::Error.new "", 400 if f
168
+ m.call *args
169
+ } do
170
+ assert_equal [["https://i.imgur.com/BLCesav.jpg", 1000, 1500, "image/jpeg"]],
171
+ DirectLink.imgur(valid_imgur_image_url, 4) # do not remove `4` or test may hang
172
+ end
173
+ end
174
+ it 404 do
175
+ e = assert_raises DirectLink::ErrorNotFound do
176
+ NetHTTPUtils.stub :request_data, ->*{ raise NetHTTPUtils::Error.new "", 404 } do
177
+ DirectLink.imgur valid_imgur_image_url
178
+ end
179
+ end
180
+ assert_equal 404, e.cause.code if Exception.instance_methods.include? :cause # Ruby 2.1
181
+ end
168
182
 
169
183
  [ # TODO: move these end line comments to test names; and do the same in other tests
170
184
  ["https://imgur.com/a/Aoh6l", "https://i.imgur.com/BLCesav.jpg", 1000, 1500, "image/jpeg"],
@@ -228,7 +242,7 @@ describe DirectLink do
228
242
 
229
243
  [
230
244
  [ :_500px, [
231
- ["https://500px.com/photo/112134597/milky-way-by-tom-hall", [4928, 2888, "https://drscdn.500px.org/photo/112134597/m%3D2048_k%3D1_a%3D1/v2?client_application_id=48351&webp=true&sig=6b9f6ac5aed6d051aa66911555657ef57262e7d998f406e40f50e6f61515808f", "jpeg"]],
245
+ ["https://500px.com/photo/112134597/milky-way-by-tom-hall", DirectLink::ErrorNotFound],
232
246
  ] ],
233
247
  [ :flickr, [
234
248
  ["https://www.flickr.com/photos/tomas-/17220613278/", DirectLink::ErrorNotFound],
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.2.0
4
+ version: 0.0.2.1
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-06-11 00:00:00.000000000 Z
11
+ date: 2018-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nethttputils