sources: replace HTTParty with Danbooru::Http in http_exists?.

This commit is contained in:
evazion
2020-06-21 13:10:16 -05:00
parent 3ad8c708c5
commit 7e471fe223
5 changed files with 8 additions and 24 deletions

View File

@@ -147,7 +147,7 @@ module Sources::Strategies
urls = urls.reverse urls = urls.reverse
end end
chosen_url = urls.find { |url| http_exists?(url, headers) } chosen_url = urls.find { |url| http_exists?(url) }
chosen_url || url chosen_url || url
end end
end end

View File

@@ -288,9 +288,8 @@ module Sources
to_h.to_json to_h.to_json
end end
def http_exists?(url, headers) def http_exists?(url, headers = {})
res = HTTParty.head(url, Danbooru.config.httparty_options.deep_merge(headers: headers)) http.headers(headers).head(url).status.success?
res.success?
end end
# Convert commentary to dtext by stripping html tags. Sites can override # Convert commentary to dtext by stripping html tags. Sites can override

View File

@@ -168,7 +168,7 @@ module Sources::Strategies
end end
candidates.find do |candidate| candidates.find do |candidate|
http_exists?(candidate, headers) http_exists?(candidate)
end end
end end

View File

@@ -16,19 +16,4 @@ module DownloadTestHelper
def assert_not_rewritten(source, referer = nil) def assert_not_rewritten(source, referer = nil)
assert_rewritten(source, source, referer) assert_rewritten(source, source, referer)
end end
def assert_http_exists(url, headers: {})
res = HTTParty.head(url, Danbooru.config.httparty_options.deep_merge(headers: headers))
assert_equal(true, res.success?)
end
def assert_http_status(code, url, headers: {})
res = HTTParty.head(url, Danbooru.config.httparty_options.deep_merge(headers: headers))
assert_equal(code, res.code)
end
def assert_http_size(size, url, headers: {})
res = HTTParty.head(url, Danbooru.config.httparty_options.deep_merge(headers: headers))
assert_equal(size, res.content_length)
end
end end

View File

@@ -43,7 +43,7 @@ module Sources
should "get the image url" do should "get the image url" do
assert_equal("https://pic.nijie.net/03/nijie_picture/728995_20170505014820_0.jpg", @site.image_url) assert_equal("https://pic.nijie.net/03/nijie_picture/728995_20170505014820_0.jpg", @site.image_url)
assert_http_size(132_555, @site.image_url) assert_downloaded(132_555, @site.image_url)
end end
should "get the canonical url" do should "get the canonical url" do
@@ -53,7 +53,7 @@ module Sources
should "get the preview url" do should "get the preview url" do
assert_equal("https://pic.nijie.net/03/__rs_l170x170/nijie_picture/728995_20170505014820_0.jpg", @site.preview_url) assert_equal("https://pic.nijie.net/03/__rs_l170x170/nijie_picture/728995_20170505014820_0.jpg", @site.preview_url)
assert_equal([@site.preview_url], @site.preview_urls) assert_equal([@site.preview_url], @site.preview_urls)
assert_http_exists(@site.preview_url) assert_downloaded(132_555, @site.preview_url)
end end
should "get the profile" do should "get the profile" do
@@ -205,8 +205,8 @@ module Sources
assert_equal("https://nijie.info/members.php?id=236014", site.profile_url) assert_equal("https://nijie.info/members.php?id=236014", site.profile_url)
assert_nothing_raised { site.to_h } assert_nothing_raised { site.to_h }
assert_http_size(3619, site.image_url) assert_downloaded(3619, site.image_url)
assert_http_exists(site.preview_url) assert_downloaded(3619, site.preview_url)
end end
end end