integrate iqdbs
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
# todo: move this to iqdbs
|
||||
class IqdbQueriesController < ApplicationController
|
||||
before_filter :member_only
|
||||
|
||||
@@ -19,14 +20,16 @@ class IqdbQueriesController < ApplicationController
|
||||
protected
|
||||
def create_by_url
|
||||
@download = Iqdb::Download.new(params[:url])
|
||||
@download.download_and_find_similar
|
||||
@download.find_similar
|
||||
@results = @download.matches
|
||||
render :layout => false, :action => "create_by_url"
|
||||
end
|
||||
|
||||
def create_by_post
|
||||
@post = Post.find(params[:post_id])
|
||||
@results = Iqdb::Server.default.query(3, @post.preview_file_path)
|
||||
render :layout => false, :action => "create_by_post"
|
||||
@download = Iqdb::Download.new(@post.complete_preview_file_url)
|
||||
@download.find_similar
|
||||
@results = @download.matches
|
||||
render :layout => false, :action => "create_by_url"
|
||||
end
|
||||
end
|
||||
@@ -6,17 +6,37 @@ module Iqdb
|
||||
@source = source
|
||||
end
|
||||
|
||||
def download_and_find_similar
|
||||
tempfile = Tempfile.new("iqdb-#{$PROCESS_ID}")
|
||||
@download = Downloads::File.new(source, tempfile.path, :get_thumbnail => true)
|
||||
@download.download!
|
||||
def find_similar
|
||||
if Danbooru.config.iqdbs_server
|
||||
params = {
|
||||
"key" => Danbooru.config.iqdbs_auth_key,
|
||||
"url" => source
|
||||
}
|
||||
uri = URI.parse("#{Danbooru.config.iqdbs_server}/similar")
|
||||
uri.query = URI.encode_www_form(params)
|
||||
|
||||
if Danbooru.config.iqdb_hostname_and_port
|
||||
@matches = Iqdb::Server.default.query(3, @download.file_path).matches
|
||||
Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
resp = http.request_get(uri.request_uri)
|
||||
if resp.is_a?(Net::HTTPSuccess)
|
||||
JSON.parse(resp.body)
|
||||
else
|
||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
||||
end
|
||||
end
|
||||
else
|
||||
begin
|
||||
tempfile = Tempfile.new("iqdb-#{$PROCESS_ID}")
|
||||
@download = Downloads::File.new(source, tempfile.path, :get_thumbnail => true)
|
||||
@download.download!
|
||||
|
||||
if Danbooru.config.iqdb_hostname_and_port
|
||||
@matches = Iqdb::Server.default.query(3, @download.file_path).matches
|
||||
end
|
||||
ensure
|
||||
tempfile.close
|
||||
tempfile.unlink
|
||||
end
|
||||
end
|
||||
ensure
|
||||
tempfile.close
|
||||
tempfile.unlink
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user