additional fixes to support iqdbs

This commit is contained in:
Albert Yi
2016-12-01 12:22:34 -08:00
parent 73ff94e920
commit f46aaad39d
2 changed files with 21 additions and 3 deletions

View File

@@ -6,11 +6,24 @@ module Iqdb
@source = source
end
def get_referer(url)
headers = {}
datums = {}
Downloads::RewriteStrategies::Base.strategies.each do |strategy|
url, headers, datums = strategy.new(url).rewrite(url, headers, datums)
end
[url, headers["Referer"]]
end
def find_similar
if Danbooru.config.iqdbs_server
url, ref = get_referer(source)
params = {
"key" => Danbooru.config.iqdbs_auth_key,
"url" => source
"url" => url,
"ref" => ref
}
uri = URI.parse("#{Danbooru.config.iqdbs_server}/similar")
uri.query = URI.encode_www_form(params)
@@ -18,7 +31,12 @@ module Iqdb
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)
json = JSON.parse(resp.body)
if json.is_a?(Array)
json
else
[]
end
else
raise "HTTP error code: #{resp.code} #{resp.message}"
end

View File

@@ -1,7 +1,7 @@
<% if @results.any? %>
<h3>Similar</h3>
<% @results.each do |match| %>
<%= PostPresenter.preview(Post.find(match.post_id), :tags => "status:any", :size => true) %>
<%= PostPresenter.preview(Post.find(match["post_id"]), :tags => "status:any", :size => true) %>
<% end %>
<% else %>
<h3>Similar</h3>