Files
danbooru/app/logical/iqdb_proxy.rb
evazion ae521e600e iqdb: proxy iqdb searches through danbooru.
Previously the search form on the /iqdb_queries page submitted directly
to the iqdb service (karasuma.donmai.us), which redirected back to
Danbooru with the search results.

This was different than API requests, which submitted to
/iqdb_queries.json which proxied the call to iqdb through Danbooru.
Because of this, searches on the /iqdb_queries page had different
behavior than API requests. Things like filesize limits and referrer
spoofing were handled differently.

Now searches on the /iqdb_queries page submit directly to Danbooru. This
is simpler and it means that API requests and HTML requests have the
same behavior.
2019-10-14 21:16:04 -05:00

48 lines
1.4 KiB
Ruby

class IqdbProxy
def self.search(params)
raise NotImplementedError unless Danbooru.config.iqdbs_server.present?
limit = params[:limit].presence || 20
limit = limit.to_i.clamp(1, 100)
similarity = params[:similarity].to_f.clamp(0.0, 100.0)
if params[:file].present?
results = query_file(params[:file], limit)
elsif params[:url].present?
url = Sources::Strategies.find(params[:url]).image_url
results = query_url(url, limit)
elsif params[:post_id].present?
url = Post.find(params[:post_id]).preview_file_url
results = query_url(url, limit)
else
results = []
end
results = results.select { |result| result["score"] >= similarity }.take(limit)
decorate_posts(results)
end
def self.query_url(url, limit)
query = { url: url, limit: limit }
response = HTTParty.get("#{Danbooru.config.iqdbs_server}/similar", query: query, **Danbooru.config.httparty_options)
response.parsed_response
end
def self.query_file(file, limit)
body = { file: file, limit: limit }
response = HTTParty.post("#{Danbooru.config.iqdbs_server}/similar", body: body, **Danbooru.config.httparty_options)
response.parsed_response
end
def self.decorate_posts(json)
json.map do |x|
begin
x["post"] = Post.find(x["post_id"])
x
rescue ActiveRecord::RecordNotFound
nil
end
end.compact
end
end