iqdb: switch to Danbooru::Http.
This commit is contained in:
@@ -5,7 +5,7 @@ class IqdbQueriesController < ApplicationController
|
||||
# XXX allow bare search params for backwards compatibility.
|
||||
search_params.merge!(params.slice(:url, :image_url, :file_url, :post_id, :limit, :similarity, :high_similarity).permit!)
|
||||
|
||||
@high_similarity_matches, @low_similarity_matches, @matches = IqdbProxy.search(search_params)
|
||||
@high_similarity_matches, @low_similarity_matches, @matches = IqdbProxy.new.search(search_params)
|
||||
respond_with(@matches, template: "iqdb_queries/show")
|
||||
end
|
||||
|
||||
|
||||
@@ -1,19 +1,23 @@
|
||||
class IqdbProxy
|
||||
class Error < StandardError; end
|
||||
attr_reader :http, :iqdbs_server
|
||||
|
||||
def self.enabled?
|
||||
Danbooru.config.iqdbs_server.present?
|
||||
def initialize(http: Danbooru::Http.new, iqdbs_server: Danbooru.config.iqdbs_server)
|
||||
@iqdbs_server = iqdbs_server
|
||||
@http = http
|
||||
end
|
||||
|
||||
def self.download(url, type)
|
||||
def enabled?
|
||||
iqdbs_server.present?
|
||||
end
|
||||
|
||||
def download(url, type)
|
||||
download = Downloads::File.new(url)
|
||||
file, strategy = download.download!(url: download.send(type))
|
||||
file
|
||||
end
|
||||
|
||||
def self.search(params)
|
||||
raise NotImplementedError, "the IQDBs service isn't configured" unless enabled?
|
||||
|
||||
def search(params)
|
||||
limit = params[:limit]&.to_i&.clamp(1, 1000) || 20
|
||||
similarity = params[:similarity]&.to_f&.clamp(0.0, 100.0) || 0.0
|
||||
high_similarity = params[:high_similarity]&.to_f&.clamp(0.0, 100.0) || 65.0
|
||||
@@ -46,15 +50,18 @@ class IqdbProxy
|
||||
file.try(:close)
|
||||
end
|
||||
|
||||
def self.query(params)
|
||||
response = HTTParty.post("#{Danbooru.config.iqdbs_server}/similar", body: params, **Danbooru.config.httparty_options)
|
||||
raise Error, "IQDB error: #{response.code} #{response.message}" unless response.success?
|
||||
raise Error, "IQDB error: #{response.parsed_response["error"]}" if response.parsed_response.is_a?(Hash)
|
||||
raise Error, "IQDB error: #{response.parsed_response.first}" if response.parsed_response.try(:first).is_a?(String)
|
||||
response.parsed_response
|
||||
def query(params)
|
||||
raise NotImplementedError, "the IQDBs service isn't configured" unless enabled?
|
||||
response = http.post("#{iqdbs_server}/similar", body: params)
|
||||
|
||||
raise Error, "IQDB error: #{response.status}" if response.status != 200
|
||||
raise Error, "IQDB error: #{response.parse["error"]}" if response.parse.is_a?(Hash)
|
||||
raise Error, "IQDB error: #{response.parse.first}" if response.parse.try(:first).is_a?(String)
|
||||
|
||||
response.parse
|
||||
end
|
||||
|
||||
def self.decorate_posts(json)
|
||||
def decorate_posts(json)
|
||||
post_ids = json.map { |match| match["post_id"] }
|
||||
posts = Post.where(id: post_ids).group_by(&:id).transform_values(&:first)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user