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
|
||||
|
||||
def self.find_similar(source)
|
||||
if Danbooru.config.iqdbs_server
|
||||
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)
|
||||
raise NotImplementedError, "the IQDBs service isn't configured. Similarity searches are not available." unless enabled?
|
||||
|
||||
resp = HTTParty.get(uri, Danbooru.config.httparty_options)
|
||||
if resp.success?
|
||||
json = JSON.parse(resp.body)
|
||||
if json.is_a?(Array)
|
||||
post_ids = json.map { |match| match["post_id"] }
|
||||
posts = Post.find(post_ids)
|
||||
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)
|
||||
|
||||
json.map do |match|
|
||||
post = posts.find { |post| post.id == match["post_id"] }
|
||||
match.with_indifferent_access.merge({ post: post })
|
||||
end
|
||||
else
|
||||
[]
|
||||
end
|
||||
else
|
||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
||||
resp = HTTParty.get(uri, Danbooru.config.httparty_options)
|
||||
raise "HTTP error code: #{resp.code} #{resp.message}" unless resp.success?
|
||||
|
||||
json = JSON.parse(resp.body)
|
||||
if json.is_a?(Array)
|
||||
post_ids = json.map { |match| match["post_id"] }
|
||||
posts = Post.find(post_ids)
|
||||
|
||||
json.map do |match|
|
||||
post = posts.find { |post| post.id == match["post_id"] }
|
||||
match.with_indifferent_access.merge({ post: post })
|
||||
end
|
||||
else
|
||||
raise NotImplementedError, "the IQDBs service isn't configured. Similarity searches are not available." unless enabled?
|
||||
[]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user