This commit is contained in:
r888888888
2014-04-25 17:15:09 -07:00
parent ceddf792f1
commit 9e2c664a41
10 changed files with 56 additions and 16 deletions

View File

@@ -20,7 +20,7 @@
}
Danbooru.Upload.initialize_iqdb_source = function() {
$.get("/iqdb/similar_by_source", {"source": $("#normalized_url").val()}).done(function(html) {$("#iqdb-similar").html(html)});
$.post("/iqdb_queries", {"url": $("#normalized_url").val()}).done(function(html) {$("#iqdb-similar").html(html)});
}
Danbooru.Upload.initialize_enter_on_tags = function() {

View File

@@ -1,8 +0,0 @@
class IqdbController < ApplicationController
def similar_by_source
@download = Iqdb::Download.new(params[:source])
@download.download_from_source
@download.find_similar
render :layout => false
end
end

View File

@@ -0,0 +1,30 @@
class IqdbQueriesController < ApplicationController
before_filter :member_only
def create
if !Danbooru.config.iqdb_hostname_and_port
render :nothing => true
return
end
if params[:url]
create_by_url
elsif params[:post_id]
create_by_post
end
end
protected
def create_by_url
@download = Iqdb::Download.new(params[:url])
@download.download_from_source
@download.find_similar
render :layout => false, :action => "create_by_url"
end
def create_by_post
@post = Post.find(params[:post_id])
@results = Iqdb::Server.default.similar(@post.id, 3)
render :layout => false, :action => "create_by_post"
end
end

View File

@@ -4,7 +4,6 @@ module Iqdb
Post.find_each do |post|
IO.popen("iqdb command #{Danbooru.config.iqdb_file}", "w+") do |io|
if File.exists?(post.preview_file_path)
puts post.id
hex = post.id.to_s(16)
io.puts "add 0 #{hex}:#{post.preview_file_path}"
end

View File

@@ -2,6 +2,7 @@ module Iqdb
module Responses
class Collection
attr_reader :responses
delegate :each, :empty?, :any?, :to => :matches
def initialize(response_string)
@responses = response_string.split(/\n/).map do |string|
@@ -13,10 +14,6 @@ module Iqdb
@matches ||= responses.select {|x| x.is_a?(Iqdb::Responses::Response_200) && x.score >= 90}
end
def empty?
matches.empty?
end
def errored?
errors.any?
end

View File

@@ -7,6 +7,10 @@ module Iqdb
attr_reader :hostname, :port, :socket
def self.default
new(*Danbooru.config.iqdb_hostname_and_port)
end
def initialize(hostname, port)
@hostname = hostname
@port = port
@@ -45,6 +49,15 @@ module Iqdb
end
end
def similar(post_id, results, flags = FLAG_DISCARD_COMMON_COEFFS)
request do
hex_id = post_id.to_s(16)
socket.puts "sim 0 #{flags} #{results} #{hex_id}"
socket.puts "done"
responses = Responses::Collection.new(@socket.read)
end
end
def query(results, filename, flags = FLAG_DISCARD_COMMON_COEFFS)
request do
socket.puts "query 0 #{flags} #{results} #{filename}"

View File

@@ -0,0 +1,9 @@
<% if @results.any? %>
<h3>Similar</h3>
<% @results.each do |match| %>
<%= PostPresenter.preview(Post.find(match.post_id)) %>
<% end %>
<% else %>
<h3>Similar</h3>
<p>No matches found</p>
<% end %>

View File

@@ -14,7 +14,7 @@
<% if CurrentUser.is_builder? && post.has_notes? %>
<li id="copy-all-notes-list"><%= link_to "Copy all notes", "#", :id => "copy-notes" %></li>
<% end %>
<li><%= link_to "Find similar", "http://danbooru.iqdb.org/db-search.php?url=http://#{Danbooru.config.hostname}#{post.preview_file_url}" %></li>
<li><%= link_to "Find similar", iqdb_queries_path(:post_id => post.id), :method => :post, :remote => true %></li>
<% if post.is_status_locked? %>
<li><span id="status-locked-notice">Status locked</span></li>

View File

@@ -257,6 +257,7 @@ Rails.application.routes.draw do
get :diff
end
end
resources :iqdb_queries, :only => [:create]
# aliases
resources :wpages, :controller => "wiki_pages"
@@ -264,7 +265,6 @@ Rails.application.routes.draw do
resources :fposts, :controller => "forum_posts"
get "/m/posts", :controller => "mobile/posts", :action => "index"
get "/m/posts/:id", :controller => "mobile/posts", :action => "show"
get "/iqdb/similar_by_source", :controller => "iqdb", :action => "similar_by_source"
# legacy aliases
get "/artist" => redirect {|params, req| "/artists?page=#{req.params[:page]}&search[name]=#{CGI::escape(req.params[:name].to_s)}"}