* Change cutoffs on upload page to max 5 results, min. 20% similarity. * Change cutoffs on standalone /iqdb_queries page to max 20 results, min. 0% similarity. * /iqdb_queries.json: add `limit` and `similarity` params to change default cutoffs.
26 lines
698 B
Ruby
26 lines
698 B
Ruby
class IqdbProxy
|
|
def self.query(url, limit, similarity)
|
|
raise NotImplementedError unless Danbooru.config.iqdbs_server.present?
|
|
|
|
limit ||= 20
|
|
similarity ||= 0.0
|
|
query = { url: url, limit: limit }
|
|
response = HTTParty.get("#{Danbooru.config.iqdbs_server}/similar", query: query, **Danbooru.config.httparty_options)
|
|
|
|
json = decorate_posts(response.parsed_response)
|
|
json = json.select { |result| result["score"] >= similarity.to_f }.take(limit.to_i)
|
|
json
|
|
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
|