integrate iqdbs

This commit is contained in:
Albert Yi
2016-11-30 16:38:00 -08:00
parent ebedea99c1
commit 73ff94e920
3 changed files with 35 additions and 75 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -1,63 +0,0 @@
#!/bin/bash
# iqdb
# chkconfig: 345 20 80
# description: iqdb
# processname: iqdb
DAEMON_PATH=/home/albert/iqdb
PORT=4000
IQDB_FILE=/var/www/danbooru2/iqdb.db
DAEMON=iqdb
DAEMONOPTS="listen2 localhost:$PORT -r $IQDB_FILE"
NAME=iqdb
DESC=iqdb
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
case "$1" in
start)
printf "%-50s" "Starting $NAME..."
cd $DAEMON_PATH
PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
#echo "Saving PID" $PID " to " $PIDFILE
if [ -z $PID ]; then
printf "%s\n" "Fail"
else
echo $PID > $PIDFILE
printf "%s\n" "Ok"
fi
;;
status)
printf "%-50s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
printf "%s\n" "Process dead but pidfile exists"
else
echo "Running"
fi
else
printf "%s\n" "Service not running"
fi
;;
stop)
printf "%-50s" "Stopping $NAME"
PID=`cat $PIDFILE`
cd $DAEMON_PATH
if [ -f $PIDFILE ]; then
kill -SIGTERM $PID
printf "%s\n" "Ok"
rm -f $PIDFILE
else
printf "%s\n" "pidfile not found"
fi
;;
restart)
$0 start
;;
*)
echo "Usage: $0 {status|start|stop|restart}"
exit 1
esac