Split requests made by Danbooru::Http into either internal or external requests. Internal requests are API calls to internal services run by Danbooru. External requests are requests to external websites, for example fetching sources or downloading files. External requests may use a HTTP proxy if one is configured. Internal requests don't. Fixes a few source extractors not using the HTTP proxy for certain API calls.
31 lines
720 B
Ruby
31 lines
720 B
Ruby
# frozen_string_literal: true
|
|
|
|
class DeepDanbooruClient
|
|
attr_reader :http
|
|
|
|
def initialize(http: Danbooru::Http.internal)
|
|
@http = http
|
|
end
|
|
|
|
def tags!(file)
|
|
html = post!("/upload", form: {
|
|
file: HTTP::FormData::File.new(file)
|
|
}).parse
|
|
|
|
tags = html.css("tbody tr").map do |row|
|
|
tag_name = row.css("td:first-child").text
|
|
confidence = row.css("td:last-child").text
|
|
|
|
# If tag_name is "rating:safe", then make a mock tag.
|
|
tag = Tag.find_by_name_or_alias(tag_name) || Tag.new(name: tag_name).freeze
|
|
[tag, confidence.to_f]
|
|
end.to_h
|
|
|
|
tags
|
|
end
|
|
|
|
def post!(url, **options)
|
|
http.post!("http://dev.kanotype.net:8003/deepdanbooru/#{url}", **options)
|
|
end
|
|
end
|