iqdb_queries_controller.rb: simplify show / check actions.
* Combine create_by_post + create_by_url. Rename to find_similar. * Move iqdb enabled check to find_similar.
This commit is contained in:
@@ -3,31 +3,16 @@ class IqdbQueriesController < ApplicationController
|
||||
respond_to :html, :json, :xml
|
||||
|
||||
def show
|
||||
server_check
|
||||
if params[:url]
|
||||
create_by_url
|
||||
respond_with(@results) do |fmt|
|
||||
fmt.html { render :layout => false, :action => "create_by_url" }
|
||||
end
|
||||
elsif params[:post_id]
|
||||
create_by_post
|
||||
respond_with(@results) do |fmt|
|
||||
fmt.js { render :layout => false, :action => "create_by_post" }
|
||||
end
|
||||
else
|
||||
render :nothing => true, :status => 422
|
||||
@results = find_similar
|
||||
|
||||
respond_with(@results) do |fmt|
|
||||
fmt.html { render :layout => false, :action => "create_by_url" }
|
||||
fmt.js { render :layout => false, :action => "create_by_post" }
|
||||
end
|
||||
end
|
||||
|
||||
def check
|
||||
server_check
|
||||
if params[:url].present?
|
||||
create_by_url
|
||||
elsif params[:post_id].present?
|
||||
create_by_post
|
||||
else
|
||||
@results = []
|
||||
end
|
||||
@results = find_similar
|
||||
respond_with(@results)
|
||||
end
|
||||
|
||||
@@ -35,22 +20,10 @@ class IqdbQueriesController < ApplicationController
|
||||
alias_method :create, :show
|
||||
|
||||
protected
|
||||
def server_check
|
||||
if !Danbooru.config.iqdbs_server
|
||||
raise NotImplementedError.new("the IQDBs service isn't configured. Similarity searches are not available.")
|
||||
end
|
||||
end
|
||||
def find_similar
|
||||
return [] if params[:url].blank? && params[:post_id].blank?
|
||||
|
||||
def create_by_url
|
||||
@download = Iqdb::Download.new(params[:url])
|
||||
@download.find_similar
|
||||
@results = @download.matches
|
||||
end
|
||||
|
||||
def create_by_post
|
||||
@post = Post.find(params[:post_id])
|
||||
@download = Iqdb::Download.new(@post.preview_file_url)
|
||||
@download.find_similar
|
||||
@results = @download.matches
|
||||
params[:url] = Post.find(params[:post_id]).preview_file_url if params[:post_id].present?
|
||||
Iqdb::Download.find_similar(params[:url])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
module Iqdb
|
||||
class Download
|
||||
attr_reader :source, :download, :matches
|
||||
|
||||
def initialize(source)
|
||||
@source = source
|
||||
def self.enabled?
|
||||
Danbooru.config.iqdbs_server.present? && Danbooru.config.iqdbs_auth_key.present?
|
||||
end
|
||||
|
||||
def get_referer(url)
|
||||
def self.get_referer(url)
|
||||
headers = {}
|
||||
datums = {}
|
||||
|
||||
@@ -17,7 +15,7 @@ module Iqdb
|
||||
[url, headers["Referer"]]
|
||||
end
|
||||
|
||||
def find_similar
|
||||
def self.find_similar(source)
|
||||
if Danbooru.config.iqdbs_server
|
||||
url, ref = get_referer(source)
|
||||
params = {
|
||||
@@ -35,18 +33,18 @@ module Iqdb
|
||||
post_ids = json.map { |match| match["post_id"] }
|
||||
posts = Post.find(post_ids)
|
||||
|
||||
@matches = json.map do |match|
|
||||
json.map do |match|
|
||||
post = posts.find { |post| post.id == match["post_id"] }
|
||||
match.with_indifferent_access.merge({ post: post })
|
||||
end
|
||||
else
|
||||
@matches = []
|
||||
[]
|
||||
end
|
||||
else
|
||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
||||
end
|
||||
else
|
||||
raise NotImplementedError
|
||||
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