Fix querying iqdb by image id

This commit is contained in:
Donovan Daniels 2024-10-13 03:53:55 -05:00
parent e2b84fa079
commit 217ae78374
Signed by: Donovan_DMC
GPG Key ID: 907D29CBFD6157BA

View File

@ -54,18 +54,24 @@ module Iqdb
end end
def query_file(file, score_cutoff) def query_file(file, score_cutoff)
thumb = generate_thumbnail(file.path) Tempfile.open("yiffy2-#{file.md5}") do |tempfile|
return [] unless thumb file.binmode
tempfile.binmode
tempfile.write(file.read)
tempfile.rewind
thumb = generate_thumbnail(tempfile.path)
raise(Error, "failed to generate thumb for #{file.md5}") unless thumb
response = make_request("/query", :post, get_channels_data(thumb)) response = make_request("/query", :post, get_channels_data(thumb))
return [] if response.status != 200 raise(Error, "iqdb request failed") if response.status != 200
process_iqdb_result(JSON.parse(response.body), score_cutoff) process_iqdb_result(JSON.parse(response.body), score_cutoff)
end
end end
def query_hash(hash, score_cutoff) def query_hash(hash, score_cutoff)
response = make_request("/query", :post, { hash: hash }) response = make_request("/query", :post, { hash: hash })
return [] if response.status != 200 raise(Error, "iqdb request failed") if response.status != 200
process_iqdb_result(JSON.parse(response.body), score_cutoff) process_iqdb_result(JSON.parse(response.body), score_cutoff)
end end
@ -73,7 +79,7 @@ module Iqdb
def process_iqdb_result(json, score_cutoff) def process_iqdb_result(json, score_cutoff)
raise(Error, "Server returned an error. Most likely the url is not found.") unless json.is_a?(Array) raise(Error, "Server returned an error. Most likely the url is not found.") unless json.is_a?(Array)
json.filter! { |entry| (entry["score"] || 0) >= (score_cutoff.presence || 60).to_i } json.filter! { |entry| (entry["score"] || 0) >= (score_cutoff.presence || 50).to_i }
json.map do |x| json.map do |x|
x["image"] = APIImage.find_by!(iqdb_id: x["post_id"]) x["image"] = APIImage.find_by!(iqdb_id: x["post_id"])
x x