remove old iqdb code

This commit is contained in:
Albert Yi
2016-12-02 13:21:56 -08:00
parent 8e5331d234
commit 7aab50f726
23 changed files with 13 additions and 349 deletions

View File

@@ -3,7 +3,7 @@ class IqdbQueriesController < ApplicationController
before_filter :member_only
def create
if !Danbooru.config.iqdb_hostname_and_port
if !Danbooru.config.iqdbs_server
render :nothing => true
return
end

View File

@@ -1,31 +0,0 @@
module Iqdb
class Command
attr_reader :database
def initialize(database)
@database = database
end
def process(&block)
IO.popen("iqdb command #{database}", "w+", &block)
end
def add(post)
hex = post.id.to_s(16)
process do |io|
io.puts "add 0 #{hex}:#{post.preview_file_path}"
io.puts "quit"
io.read
end
end
def remove(post_id)
hex = post_id.to_s(16)
process do |io|
io.puts "remove 0 #{hex}"
io.puts "quit"
io.read
end
end
end
end

View File

@@ -42,18 +42,7 @@ module Iqdb
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
raise NotImplementedError
end
end
end

View File

@@ -1,16 +0,0 @@
module Iqdb
class Importer
def import!
Post.find_each do |post|
IO.popen("iqdb command #{Danbooru.config.iqdb_file}", "w+") do |io|
if File.exists?(post.preview_file_path)
hex = post.id.to_s(16)
io.puts "add 0 #{hex}:#{post.preview_file_path}"
end
io.puts "quit"
io.read
end
end
end
end
end

View File

@@ -1,6 +0,0 @@
module Iqdb
module Responses
class Base
end
end
end

View File

@@ -1,27 +0,0 @@
module Iqdb
module Responses
class Collection
attr_reader :responses
include Enumerable
delegate :<=>, :each, :to => :matches
def initialize(response_string)
@responses = response_string.split(/\n/).map do |string|
::Iqdb::Responses.const_get("Response_#{string[0..2]}").new(string[4..-1])
end
end
def matches
@matches ||= responses.select {|x| x.is_a?(Iqdb::Responses::Response_200) && x.score >= 80}
end
def errored?
errors.any?
end
def errors
@errors ||= responses.select {|x| x.is_a?(Iqdb::Responses::Error)}.map {|x| x.to_s}
end
end
end
end

View File

@@ -1,6 +0,0 @@
module Iqdb
module Responses
class Error < Base
end
end
end

View File

@@ -1,8 +0,0 @@
module Iqdb
module Responses
class Response_000 < Base
def initialize(response_string)
end
end
end
end

View File

@@ -1,11 +0,0 @@
module Iqdb
module Responses
class Response_100 < Base
attr_reader :message
def initialize(response_string)
@message = response_string
end
end
end
end

View File

@@ -1,11 +0,0 @@
module Iqdb
module Responses
class Response_101 < Base
attr_reader :key, :value
def initialize(response_string)
@key, @value = response_string.split(/\=/)
end
end
end
end

View File

@@ -1,11 +0,0 @@
module Iqdb
module Responses
class Response_102 < Base
attr_reader :dbid, :filename
def initialize(response_string)
@dbid, @filename = response_string.split(/ /)
end
end
end
end

View File

@@ -1,18 +0,0 @@
module Iqdb
module Responses
class Response_200 < Base
attr_reader :imgid, :score, :width, :height
def initialize(response_string)
@imgid, @score, @width, @height = response_string.split(/ /)
@score = score.to_f
@width = width.to_i
@height = height.to_i
end
def post_id
imgid.to_i(16)
end
end
end
end

View File

@@ -1,19 +0,0 @@
module Iqdb
module Responses
class Response_201 < Base
attr_reader :dbid, :imgid, :score, :width, :height
def initialize(response_string)
@dbid, @imgid, @score, @width, @height = response_string.split(/ /)
@dbid = dbid.to_i
@score = score.to_f
@width = width.to_i
@height = height.to_i
end
def post_id
imgid.to_i(16)
end
end
end
end

View File

@@ -1,19 +0,0 @@
module Iqdb
module Responses
class Response_202 < Base
attr_reader :original_id, :stddev, :dupes
def initialize(response_string)
response_string =~ /^(\d+)=([0-9.]+)/
@original_id = $1
@stddev = $2
@dupes = response_string.scan(/(\d+):([0-9.]+)/).map {|x| [x[0].to_i(16), x[1].to_f]}
end
def original_post_id
original_id.to_i(16)
end
end
end
end

View File

@@ -1,15 +0,0 @@
module Iqdb
module Responses
class Response_300 < Error
attr_reader :message
def initialize(response_string)
@message = response_string
end
def to_s
"Error: #{message}"
end
end
end
end

View File

@@ -1,17 +0,0 @@
module Iqdb
module Responses
class Response_301 < Error
attr_reader :exception, :description
def initialize(response_string)
response_string =~ /^(\S+) (.+)/
@exception = $1
@description = $2
end
def to_s
"Exception: #{exception}: #{description}"
end
end
end
end

View File

@@ -1,17 +0,0 @@
module Iqdb
module Responses
class Response_302 < Error
attr_reader :exception, :description
def initialize(response_string)
response_string =~ /^(\S+) (.+)/
@exception = $1
@description = $2
end
def to_s
"Fatal Exception: #{exception}: #{description}"
end
end
end
end

View File

@@ -1,60 +0,0 @@
module Iqdb
class Server
FLAG_SKETCH = 0x01
FLAG_GRAYSCALE = 0x02
FLAG_WIDTH_AS_SET = 0x08
FLAG_DISCARD_COMMON_COEFFS = 0x10
attr_reader :hostname, :port, :socket
def self.default
new(*Danbooru.config.iqdb_hostname_and_port)
end
def initialize(hostname, port)
@hostname = hostname
@port = port
end
def open
@socket = TCPSocket.open(hostname, port)
end
def close
socket.close
end
def request
open
yield
ensure
close
end
def add(post)
request do
hex = post.id.to_s(16)
socket.puts "add 0 #{hex}:#{post.preview_file_path}"
socket.puts "done"
socket.read
end
end
def remove(post_id)
request do
hex = post_id.to_s(16)
socket.puts "remove 0 #{hex}"
socket.puts "done"
socket.read
end
end
def query(results, filename, flags = 0)
request do
socket.puts "query 0 #{flags} #{results} #{filename}"
socket.puts "done"
responses = Responses::Collection.new(socket.read)
end
end
end
end

View File

@@ -1697,42 +1697,21 @@ class Post < ActiveRecord::Base
if Danbooru.config.aws_sqs_iqdb_url
client = SqsService.new(Danbooru.config.aws_sqs_iqdb_url)
client.send_message("remove\n#{post_id}")
else
Iqdb::Server.new(*Danbooru.config.iqdb_hostname_and_port).remove(post_id)
Iqdb::Command.new(Danbooru.config.iqdb_file).remove(post_id)
end
end
end
def update_iqdb_async
if File.exists?(preview_file_path)
if Danbooru.config.aws_sqs_iqdb_url
client = SqsService.new(Danbooru.config.aws_sqs_iqdb_url)
client.send_message("update\n#{id}\n#{complete_preview_file_url}")
elsif Danbooru.config.iqdb_hostname_and_port
Danbooru.config.all_server_hosts.each do |host|
if has_tag?("ugoira")
run_at = 10.seconds.from_now
else
run_at = Time.now
end
delay(:queue => host, :run_at => run_at).update_iqdb
end
end
if File.exists?(preview_file_path) && Danbooru.config.aws_sqs_iqdb_url
client = SqsService.new(Danbooru.config.aws_sqs_iqdb_url)
client.send_message("update\n#{id}\n#{complete_preview_file_url}")
end
end
def remove_iqdb_async
if File.exists?(preview_file_path)
if Danbooru.config.aws_sqs_iqdb_url
client = SqsService.new(Danbooru.config.aws_sqs_iqdb_url)
client.send_message("remove\n#{id}")
elsif Danbooru.config.iqdb_hostname_and_port
Danbooru.config.all_server_hosts.each do |host|
Post.delay(:queue => host).remove_iqdb(id)
end
end
if File.exists?(preview_file_path) && Danbooru.config.aws_sqs_iqdb_url
client = SqsService.new(Danbooru.config.aws_sqs_iqdb_url)
client.send_message("remove\n#{id}")
end
end
@@ -1740,9 +1719,6 @@ class Post < ActiveRecord::Base
if Danbooru.config.aws_sqs_iqdb_url
client = SqsService.new(Danbooru.config.aws_sqs_iqdb_url)
client.send_message("update\n#{id}\n#{complete_preview_file_url}")
else
Iqdb::Server.new(*Danbooru.config.iqdb_hostname_and_port).add(self)
Iqdb::Command.new(Danbooru.config.iqdb_file).add(self)
end
end
end

View File

@@ -72,7 +72,7 @@
<%= button_tag "Artist", :id => "find-artist-button", :type => "button" %>
</div>
<% if Danbooru.config.iqdb_hostname_and_port %>
<% if Danbooru.config.iqdbs_server %>
<div class="input" id="iqdb-similar" style="display: none;"></div>
<% end %>

View File

@@ -88,7 +88,7 @@
</div>
<% end %>
<% if Danbooru.config.iqdb_hostname_and_port %>
<% if Danbooru.config.iqdbs_server %>
<% if params[:url] %>
<div class="input" id="iqdb-similar">
<p><em>Loading similar...</em></p>