From 73ff94e920e542505e9f7b97543221deb6ca433e Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Wed, 30 Nov 2016 16:38:00 -0800 Subject: [PATCH] integrate iqdbs --- app/controllers/iqdb_queries_controller.rb | 9 ++-- app/logical/iqdb/download.rb | 38 +++++++++---- script/install/iqdb_init.sh | 63 ---------------------- 3 files changed, 35 insertions(+), 75 deletions(-) delete mode 100644 script/install/iqdb_init.sh diff --git a/app/controllers/iqdb_queries_controller.rb b/app/controllers/iqdb_queries_controller.rb index 62d07d726..f5943f214 100644 --- a/app/controllers/iqdb_queries_controller.rb +++ b/app/controllers/iqdb_queries_controller.rb @@ -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 \ No newline at end of file diff --git a/app/logical/iqdb/download.rb b/app/logical/iqdb/download.rb index 1f58a57b3..c3c49b351 100644 --- a/app/logical/iqdb/download.rb +++ b/app/logical/iqdb/download.rb @@ -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 diff --git a/script/install/iqdb_init.sh b/script/install/iqdb_init.sh deleted file mode 100644 index 62ab77583..000000000 --- a/script/install/iqdb_init.sh +++ /dev/null @@ -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 \ No newline at end of file