iqdb: use preview image url for iqdb lookups.

* Fix the IQDB lookup to handle error messages returned by IQDB.

* Fix `undefined method `>=' for nil:NilClass` error when trying to upload
  ugoiras. Using the preview image for IQDB lookups should be faster and
  give the same results as the full image, plus it should work with things
  like ugoiras and videos that IQDB can't handle.

  Also add an image_url param so that API users can continue using the
  full image if needed.
This commit is contained in:
evazion
2019-10-22 12:59:00 -05:00
parent 2c6567b5d2
commit abee048de5

View File

@@ -15,7 +15,10 @@ class IqdbProxy
if params[:file].present?
results = query(file: params[:file], limit: limit)
elsif params[:url].present?
url = Sources::Strategies.find(params[:url]).image_url
url = Sources::Strategies.find(params[:url]).preview_url
results = query(url: url, limit: limit)
elsif params[:image_url].present?
url = Sources::Strategies.find(params[:image_url]).image_url
results = query(url: url, limit: limit)
elsif params[:post_id].present?
url = Post.find(params[:post_id]).preview_file_url
@@ -34,6 +37,7 @@ class IqdbProxy
def self.query(params)
response = HTTParty.post("#{Danbooru.config.iqdbs_server}/similar", body: params, **Danbooru.config.httparty_options)
raise Error, "HTTP error: #{response.code} #{response.message}" unless response.success?
raise Error, "IQDB error: #{response.parsed_response.first}" if response.parsed_response.try(:first).is_a?(String)
response.parsed_response
end