Iqdb::Download.find_similar: simplify error handling.
Raise errors immediately to avoid deeply nested if-else statements.
This commit is contained in:
@@ -16,35 +16,31 @@ module Iqdb
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.find_similar(source)
|
def self.find_similar(source)
|
||||||
if Danbooru.config.iqdbs_server
|
raise NotImplementedError, "the IQDBs service isn't configured. Similarity searches are not available." unless enabled?
|
||||||
url, ref = get_referer(source)
|
|
||||||
params = {
|
|
||||||
"key" => Danbooru.config.iqdbs_auth_key,
|
|
||||||
"url" => url,
|
|
||||||
"ref" => ref
|
|
||||||
}
|
|
||||||
uri = URI.parse("#{Danbooru.config.iqdbs_server}/similar")
|
|
||||||
uri.query = URI.encode_www_form(params)
|
|
||||||
|
|
||||||
resp = HTTParty.get(uri, Danbooru.config.httparty_options)
|
url, ref = get_referer(source)
|
||||||
if resp.success?
|
params = {
|
||||||
json = JSON.parse(resp.body)
|
"key" => Danbooru.config.iqdbs_auth_key,
|
||||||
if json.is_a?(Array)
|
"url" => url,
|
||||||
post_ids = json.map { |match| match["post_id"] }
|
"ref" => ref
|
||||||
posts = Post.find(post_ids)
|
}
|
||||||
|
uri = URI.parse("#{Danbooru.config.iqdbs_server}/similar")
|
||||||
|
uri.query = URI.encode_www_form(params)
|
||||||
|
|
||||||
json.map do |match|
|
resp = HTTParty.get(uri, Danbooru.config.httparty_options)
|
||||||
post = posts.find { |post| post.id == match["post_id"] }
|
raise "HTTP error code: #{resp.code} #{resp.message}" unless resp.success?
|
||||||
match.with_indifferent_access.merge({ post: post })
|
|
||||||
end
|
json = JSON.parse(resp.body)
|
||||||
else
|
if json.is_a?(Array)
|
||||||
[]
|
post_ids = json.map { |match| match["post_id"] }
|
||||||
end
|
posts = Post.find(post_ids)
|
||||||
else
|
|
||||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
json.map do |match|
|
||||||
|
post = posts.find { |post| post.id == match["post_id"] }
|
||||||
|
match.with_indifferent_access.merge({ post: post })
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise NotImplementedError, "the IQDBs service isn't configured. Similarity searches are not available." unless enabled?
|
[]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user