101 lines
2.8 KiB
Ruby
101 lines
2.8 KiB
Ruby
class Scraper::ClientFactory
|
|
@http_clients = Concurrent::ThreadLocalVar.new() { {} }
|
|
@gallery_dl_clients = Concurrent::ThreadLocalVar.new(nil)
|
|
|
|
# for testing only
|
|
def self.http_client_mock=(mock)
|
|
raise unless Rails.env.test?
|
|
@http_client_mock = mock
|
|
end
|
|
|
|
def self.gallery_dl_client_mock=(mock)
|
|
raise unless Rails.env.test?
|
|
@gallery_dl_client_mock = mock
|
|
end
|
|
|
|
# public API
|
|
def self.get_gallery_dl_client
|
|
# TODO - can probably allow development to hit real websites
|
|
if Rails.env.test? || Rails.env.development?
|
|
@gallery_dl_client_mock || raise("no gallery dl mock set")
|
|
else
|
|
_gallery_dl_client_impl
|
|
end
|
|
end
|
|
|
|
def self.get_twitter_http_client
|
|
if Rails.env.test? || Rails.env.development?
|
|
@http_client_mock || raise("no http client mock set")
|
|
else
|
|
_http_client_impl(:twitter, Scraper::TwitterHttpClientConfig)
|
|
end
|
|
end
|
|
|
|
def self.get_fa_http_client
|
|
if Rails.env.test? || Rails.env.development?
|
|
@http_client_mock || raise("no http client mock set")
|
|
else
|
|
_http_client_impl(:fa, Scraper::FaHttpClientConfig)
|
|
end
|
|
end
|
|
|
|
def self.get_e621_http_client
|
|
if Rails.env.test? || Rails.env.development?
|
|
@http_client_mock || raise("no http client mock set")
|
|
else
|
|
_http_client_impl(:e621, Scraper::E621HttpClientConfig)
|
|
end
|
|
end
|
|
|
|
def self.get_inkbunny_http_client
|
|
if Rails.env.test?
|
|
@http_client_mock || raise("no http client mock set")
|
|
else
|
|
client = _http_client_impl(:inkbunny, Scraper::InkbunnyHttpClientConfig)
|
|
sid = _get_ib_client_sid(client)
|
|
client
|
|
end
|
|
end
|
|
|
|
def self._gallery_dl_client_impl
|
|
@gallery_dl_clients.value ||=
|
|
begin
|
|
proxy_config =
|
|
Rails.application.config.x.proxy || raise("no proxy config")
|
|
if proxy_config[:gallery_dl].blank?
|
|
raise(
|
|
"no gallery_dl host defined for proxy config #{proxy_config[:name]}"
|
|
)
|
|
end
|
|
Scraper::GalleryDlClient.new(
|
|
proxy_config[:name],
|
|
proxy_config[:gallery_dl]
|
|
)
|
|
end
|
|
end
|
|
|
|
def self._http_client_impl(key, config_klass)
|
|
@http_clients.value[key] ||= begin
|
|
# proxy_config = Rails.application.config.x.proxy || raise("no proxy config")
|
|
# performer = Scraper::HttpPerformer.new(proxy_config[:name], proxy_config[:http])
|
|
performer = Scraper::CurlHttpPerformer.new
|
|
Scraper::HttpClient.new(config_klass.new, performer)
|
|
end
|
|
end
|
|
|
|
def self._get_ib_client_sid(client)
|
|
sid_key = "sid-#{Rails.application.config.x.proxy}"
|
|
sid = Domain::Inkbunny::GlobalState.find_or_create_by!(key: sid_key)
|
|
sid.with_lock("FOR UPDATE") do
|
|
if sid.value.blank?
|
|
sid.value = _ib_login(client)
|
|
sid.save!
|
|
end
|
|
end
|
|
sid.value
|
|
end
|
|
|
|
def self._ib_login(client)
|
|
end
|
|
end
|