directlink 0.0.2.0 → 0.0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -2
- data/bin/directlink +1 -1
- data/directlink.gemspec +1 -1
- data/lib/directlink.rb +17 -9
- data/test.rb +25 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ad94a6b55149c5f3f0a26a021131eff17311d65
|
4
|
+
data.tar.gz: d564d355ef4c3447274cd535ca569c3454262411
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/lib/directlink.rb
CHANGED
@@ -80,27 +80,27 @@ module DirectLink
|
|
80
80
|
require "json"
|
81
81
|
require "nethttputils"
|
82
82
|
|
83
|
-
|
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
|
-
|
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
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
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",
|
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.
|
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
|
+
date: 2018-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nethttputils
|