switch to httparty
This commit is contained in:
@@ -31,12 +31,8 @@ module Downloads
|
|||||||
}
|
}
|
||||||
@source, headers, @data = before_download(@source, headers, @data)
|
@source, headers, @data = before_download(@source, headers, @data)
|
||||||
url = URI.parse(@source)
|
url = URI.parse(@source)
|
||||||
Net::HTTP.start(url.host, url.port, :use_ssl => url.is_a?(URI::HTTPS)) do |http|
|
res = HTTParty.head(url, timeout: 3)
|
||||||
http.read_timeout = 3
|
res.content_length
|
||||||
http.request_head(url.request_uri, headers) do |res|
|
|
||||||
return res.content_length
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def download!
|
def download!
|
||||||
@@ -94,30 +90,17 @@ module Downloads
|
|||||||
validate_local_hosts(url)
|
validate_local_hosts(url)
|
||||||
|
|
||||||
begin
|
begin
|
||||||
Net::HTTP.start(url.host, url.port, :use_ssl => url.is_a?(URI::HTTPS)) do |http|
|
res = HTTParty.get(url, timeout: 10, headers: headers)
|
||||||
http.read_timeout = 10
|
if res.success?
|
||||||
http.request_get(url.request_uri, headers) do |res|
|
if max_size
|
||||||
case res
|
len = res["Content-Length"]
|
||||||
when Net::HTTPSuccess then
|
raise Error.new("File is too large (#{len} bytes)") if len && len.to_i > max_size
|
||||||
if max_size
|
end
|
||||||
len = res["Content-Length"]
|
yield(res)
|
||||||
raise Error.new("File is too large (#{len} bytes)") if len && len.to_i > max_size
|
return [src, datums]
|
||||||
end
|
else
|
||||||
yield(res)
|
raise Error.new("HTTP error code: #{res.code} #{res.message}")
|
||||||
return [src, datums]
|
end
|
||||||
|
|
||||||
when Net::HTTPRedirection then
|
|
||||||
if limit == 0 then
|
|
||||||
raise Error.new("Too many redirects")
|
|
||||||
end
|
|
||||||
src = res["location"]
|
|
||||||
limit -= 1
|
|
||||||
|
|
||||||
else
|
|
||||||
raise Error.new("HTTP error code: #{res.code} #{res.message}")
|
|
||||||
end
|
|
||||||
end # http.request_get
|
|
||||||
end # http.start
|
|
||||||
rescue Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EIO, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, IOError => x
|
rescue Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EIO, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, IOError => x
|
||||||
tries += 1
|
tries += 1
|
||||||
if tries < 3
|
if tries < 3
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ module Downloads
|
|||||||
protected
|
protected
|
||||||
def test_original(url)
|
def test_original(url)
|
||||||
res = http_head_request(url, {})
|
res = http_head_request(url, {})
|
||||||
res.is_a?(Net::HTTPSuccess)
|
res.success?
|
||||||
end
|
end
|
||||||
|
|
||||||
def rewrite_html_url(url, headers)
|
def rewrite_html_url(url, headers)
|
||||||
|
|||||||
@@ -21,23 +21,12 @@ module Downloads
|
|||||||
|
|
||||||
protected
|
protected
|
||||||
def http_head_request(url, headers)
|
def http_head_request(url, headers)
|
||||||
uri = URI.parse(url)
|
HTTParty.head(url, headers: headers)
|
||||||
http = Net::HTTP.new(uri.host, uri.port)
|
|
||||||
if uri.scheme == "https"
|
|
||||||
http.use_ssl = true
|
|
||||||
end
|
|
||||||
http.request_head(uri.request_uri, headers) do |res|
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def http_exists?(url, headers)
|
def http_exists?(url, headers)
|
||||||
exists = false
|
|
||||||
res = http_head_request(url, headers)
|
res = http_head_request(url, headers)
|
||||||
if res.is_a?(Net::HTTPSuccess)
|
res.success?
|
||||||
exists = true
|
|
||||||
end
|
|
||||||
exists
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -16,19 +16,15 @@ class ImageProxy
|
|||||||
raise "Proxy not allowed for this site"
|
raise "Proxy not allowed for this site"
|
||||||
end
|
end
|
||||||
|
|
||||||
uri = URI.parse(url)
|
|
||||||
headers = {
|
headers = {
|
||||||
"Referer" => fake_referer_for(url),
|
"Referer" => fake_referer_for(url),
|
||||||
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
|
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
|
||||||
}
|
}
|
||||||
|
response = HTTParty.get(url, headers: headers)
|
||||||
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.is_a?(URI::HTTPS)) do |http|
|
if response.success?
|
||||||
resp = http.request_get(uri.request_uri, headers)
|
return response
|
||||||
if resp.is_a?(Net::HTTPSuccess)
|
else
|
||||||
return resp
|
raise "HTTP error code: #{response.code} #{response.message}"
|
||||||
else
|
|
||||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -28,24 +28,22 @@ module Iqdb
|
|||||||
uri = URI.parse("#{Danbooru.config.iqdbs_server}/similar")
|
uri = URI.parse("#{Danbooru.config.iqdbs_server}/similar")
|
||||||
uri.query = URI.encode_www_form(params)
|
uri.query = URI.encode_www_form(params)
|
||||||
|
|
||||||
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.is_a?(URI::HTTPS)) do |http|
|
resp = HTTParty.get(uri)
|
||||||
resp = http.request_get(uri.request_uri)
|
if resp.success?
|
||||||
if resp.is_a?(Net::HTTPSuccess)
|
json = JSON.parse(resp.body)
|
||||||
json = JSON.parse(resp.body)
|
if json.is_a?(Array)
|
||||||
if json.is_a?(Array)
|
post_ids = json.map { |match| match["post_id"] }
|
||||||
post_ids = json.map { |match| match["post_id"] }
|
posts = Post.find(post_ids)
|
||||||
posts = Post.find(post_ids)
|
|
||||||
|
|
||||||
@matches = json.map do |match|
|
@matches = json.map do |match|
|
||||||
post = posts.find { |post| post.id == match["post_id"] }
|
post = posts.find { |post| post.id == match["post_id"] }
|
||||||
match.with_indifferent_access.merge({ post: post })
|
match.with_indifferent_access.merge({ post: post })
|
||||||
end
|
|
||||||
else
|
|
||||||
@matches = []
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
@matches = []
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
raise "HTTP error code: #{resp.code} #{resp.message}"
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|||||||
@@ -17,14 +17,11 @@ class MissedSearchService
|
|||||||
def fetch_data
|
def fetch_data
|
||||||
Cache.get("ms", 1.minute) do
|
Cache.get("ms", 1.minute) do
|
||||||
url = URI.parse("#{Danbooru.config.reportbooru_server}/missed_searches")
|
url = URI.parse("#{Danbooru.config.reportbooru_server}/missed_searches")
|
||||||
response = ""
|
response = HTTParty.get(url, timeout: 6)
|
||||||
Net::HTTP.start(url.host, url.port, :use_ssl => url.is_a?(URI::HTTPS)) do |http|
|
if response.success?
|
||||||
http.read_timeout = 1
|
response = response.body
|
||||||
http.request_get(url.request_uri) do |res|
|
else
|
||||||
if res.is_a?(Net::HTTPSuccess)
|
response = ""
|
||||||
response = res.body
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
response.force_encoding("utf-8")
|
response.force_encoding("utf-8")
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -8,22 +8,18 @@ class NicoSeigaApiClient
|
|||||||
end
|
end
|
||||||
|
|
||||||
def get_illust(id)
|
def get_illust(id)
|
||||||
uri = URI.parse("#{BASE_URL}/illust/info?id=#{id}")
|
uri = "#{BASE_URL}/illust/info?id=#{id}"
|
||||||
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.is_a?(URI::HTTPS)) do |http|
|
resp = HTTParty.get(uri)
|
||||||
resp = http.request_get(uri.request_uri)
|
if resp.success?
|
||||||
if resp.is_a?(Net::HTTPSuccess)
|
parse_illust_xml_response(resp.body)
|
||||||
parse_illust_xml_response(resp.body)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_artist(id)
|
def get_artist(id)
|
||||||
uri = URI.parse("#{BASE_URL}/user/info?id=#{id}")
|
uri = "#{BASE_URL}/user/info?id=#{id}"
|
||||||
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.is_a?(URI::HTTPS)) do |http|
|
resp = HTTParty.get(uri)
|
||||||
resp = http.request_get(uri.request_uri)
|
if resp.success?
|
||||||
if resp.is_a?(Net::HTTPSuccess)
|
parse_artist_xml_response(resp.body)
|
||||||
parse_artist_xml_response(resp.body)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -137,13 +137,11 @@ class PixivApiClient
|
|||||||
url.query = URI.encode_www_form(params)
|
url.query = URI.encode_www_form(params)
|
||||||
json = nil
|
json = nil
|
||||||
|
|
||||||
Net::HTTP.start(url.host, url.port, :use_ssl => true) do |http|
|
resp = HTTParty.get(url)
|
||||||
resp = http.request_get(url.request_uri, headers)
|
if resp.success?
|
||||||
if resp.is_a?(Net::HTTPSuccess)
|
json = parse_api_json(resp.body)
|
||||||
json = parse_api_json(resp.body)
|
else
|
||||||
else
|
raise Error.new("Pixiv API call failed (status=#{resp.code} body=#{resp.body})")
|
||||||
raise Error.new("Pixiv API call failed (status=#{resp.code} body=#{resp.body})")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
WorksResponse.new(json["response"][0])
|
WorksResponse.new(json["response"][0])
|
||||||
@@ -173,17 +171,14 @@ private
|
|||||||
"client_id" => CLIENT_ID,
|
"client_id" => CLIENT_ID,
|
||||||
"client_secret" => CLIENT_SECRET
|
"client_secret" => CLIENT_SECRET
|
||||||
}
|
}
|
||||||
url = URI.parse("https://oauth.secure.pixiv.net/auth/token")
|
url = "https://oauth.secure.pixiv.net/auth/token"
|
||||||
|
|
||||||
Net::HTTP.start(url.host, url.port, :use_ssl => true) do |http|
|
resp = HTTParty.post(url, body: params, headers: headers)
|
||||||
resp = http.request_post(url.request_uri, URI.encode_www_form(params), headers)
|
if resp.success?
|
||||||
|
json = JSON.parse(resp.body)
|
||||||
if resp.is_a?(Net::HTTPSuccess)
|
access_token = json["response"]["access_token"]
|
||||||
json = JSON.parse(resp.body)
|
else
|
||||||
access_token = json["response"]["access_token"]
|
raise Error.new("Pixiv API access token call failed (status=#{resp.code} body=#{resp.body})")
|
||||||
else
|
|
||||||
raise Error.new("Pixiv API access token call failed (status=#{resp.code} body=#{resp.body})")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
access_token
|
access_token
|
||||||
|
|||||||
@@ -27,15 +27,12 @@ class PopularSearchService
|
|||||||
dates = date.strftime("%Y-%m-%d")
|
dates = date.strftime("%Y-%m-%d")
|
||||||
|
|
||||||
Cache.get("ps-#{scale}-#{dates}", 1.minute) do
|
Cache.get("ps-#{scale}-#{dates}", 1.minute) do
|
||||||
url = URI.parse("#{Danbooru.config.reportbooru_server}/hits/#{scale}?date=#{dates}")
|
url = "#{Danbooru.config.reportbooru_server}/hits/#{scale}?date=#{dates}"
|
||||||
response = ""
|
response = HTTParty.get(url, timeout: 3)
|
||||||
Net::HTTP.start(url.host, url.port, :use_ssl => url.is_a?(URI::HTTPS)) do |http|
|
if response.success?
|
||||||
http.read_timeout = 1
|
response = response.body
|
||||||
http.request_get(url.request_uri) do |res|
|
else
|
||||||
if res.is_a?(Net::HTTPSuccess)
|
response = ""
|
||||||
response = res.body
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
response
|
response
|
||||||
end.to_s.force_encoding("utf-8")
|
end.to_s.force_encoding("utf-8")
|
||||||
|
|||||||
@@ -33,13 +33,11 @@ module Reports
|
|||||||
uri = URI.parse("#{Danbooru.config.reportbooru_server}/reports/#{endpoint}")
|
uri = URI.parse("#{Danbooru.config.reportbooru_server}/reports/#{endpoint}")
|
||||||
uri.query = URI.encode_www_form(params)
|
uri.query = URI.encode_www_form(params)
|
||||||
|
|
||||||
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.is_a?(URI::HTTPS)) do |http|
|
resp = HTTParty.get(uri)
|
||||||
resp = http.request_get(uri.request_uri)
|
if resp.success?
|
||||||
if resp.is_a?(Net::HTTPSuccess)
|
resp.body
|
||||||
resp.body
|
else
|
||||||
else
|
raise "HTTP error code: #{resp.code} #{resp.message}"
|
||||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -46,25 +46,22 @@ module Sources::Strategies
|
|||||||
end
|
end
|
||||||
|
|
||||||
def get
|
def get
|
||||||
uri = URI.parse(api_url)
|
resp = HTTParty.get(api_url)
|
||||||
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.is_a?(URI::HTTPS)) do |http|
|
image_url_rewriter = Downloads::RewriteStrategies::ArtStation.new
|
||||||
resp = http.request_get(uri.request_uri)
|
if resp.success?
|
||||||
image_url_rewriter = Downloads::RewriteStrategies::ArtStation.new
|
@json = JSON.parse(resp.body)
|
||||||
if resp.is_a?(Net::HTTPSuccess)
|
@artist_name = json["user"]["username"]
|
||||||
@json = JSON.parse(resp.body)
|
@profile_url = json["user"]["permalink"]
|
||||||
@artist_name = json["user"]["username"]
|
images = json["assets"].select { |asset| asset["asset_type"] == "image" }
|
||||||
@profile_url = json["user"]["permalink"]
|
@image_urls = images.map do |x|
|
||||||
images = json["assets"].select { |asset| asset["asset_type"] == "image" }
|
y, _, _ = image_url_rewriter.rewrite(x["image_url"], nil)
|
||||||
@image_urls = images.map do |x|
|
y
|
||||||
y, _, _ = image_url_rewriter.rewrite(x["image_url"], nil)
|
|
||||||
y
|
|
||||||
end
|
|
||||||
@tags = json["tags"].map {|x| [x.downcase.tr(" ", "_"), "https://www.artstation.com/search?q=" + CGI.escape(x)]} if json["tags"]
|
|
||||||
@artist_commentary_title = json["title"]
|
|
||||||
@artist_commentary_desc = ActionView::Base.full_sanitizer.sanitize(json["description"])
|
|
||||||
else
|
|
||||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
|
||||||
end
|
end
|
||||||
|
@tags = json["tags"].map {|x| [x.downcase.tr(" ", "_"), "https://www.artstation.com/search?q=" + CGI.escape(x)]} if json["tags"]
|
||||||
|
@artist_commentary_title = json["title"]
|
||||||
|
@artist_commentary_desc = ActionView::Base.full_sanitizer.sanitize(json["description"])
|
||||||
|
else
|
||||||
|
raise "HTTP error code: #{resp.code} #{resp.message}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -22,14 +22,11 @@ class TagAliasCorrection
|
|||||||
end
|
end
|
||||||
|
|
||||||
def fill_hash!
|
def fill_hash!
|
||||||
Net::HTTP.start(hostname, 80) do |http|
|
res = HTTParty.get("http://#{hostname}/tag_aliases/#{tag_alias_id}/correction.json")
|
||||||
http.request_get("/tag_aliases/#{tag_alias_id}/correction.json") do |res|
|
if res.success?
|
||||||
if res === Net::HTTPSuccess
|
json = JSON.parse(res.body)
|
||||||
json = JSON.parse(res.body)
|
statistics_hash["antecedent_cache"] = json["antecedent_cache"]
|
||||||
statistics_hash["antecedent_cache"] = json["antecedent_cache"]
|
statistics_hash["consequent_cache"] = json["consequent_cache"]
|
||||||
statistics_hash["consequent_cache"] = json["consequent_cache"]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -19,14 +19,11 @@ class TagCorrection
|
|||||||
end
|
end
|
||||||
|
|
||||||
def fill_hash!
|
def fill_hash!
|
||||||
Net::HTTP.start(hostname, 80) do |http|
|
res = HTTParty.get("http://#{hostname}/tags/#{tag_id}/correction.json")
|
||||||
http.request_get("/tags/#{tag_id}/correction.json") do |res|
|
if res.success?
|
||||||
if res === Net::HTTPSuccess
|
json = JSON.parse(res.body)
|
||||||
json = JSON.parse(res.body)
|
statistics_hash["category_cache"] = json["category_cache"]
|
||||||
statistics_hash["category_cache"] = json["category_cache"]
|
statistics_hash["post_fast_count_cache"] = json["post_fast_count_cache"]
|
||||||
statistics_hash["post_fast_count_cache"] = json["post_fast_count_cache"]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -32,22 +32,12 @@ class TwitterService
|
|||||||
end.compact
|
end.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract_og_image_from_page(url, n = 5)
|
def extract_og_image_from_page(url)
|
||||||
raise "too many redirects" if n == 0
|
resp = HTTParty.get(url)
|
||||||
|
if resp.success?
|
||||||
Net::HTTP.start(url.host, url.port, :use_ssl => (url.normalized_scheme == "https")) do |http|
|
doc = Nokogiri::HTML(resp.body)
|
||||||
resp = http.request_get(url.request_uri)
|
images = doc.css("meta[property='og:image']")
|
||||||
if resp.is_a?(Net::HTTPMovedPermanently) && resp["Location"]
|
return images.first.attr("content").sub(":large", ":orig")
|
||||||
redirect_url = Addressable::URI.parse(resp["Location"])
|
|
||||||
redirect_url.host = url.host if redirect_url.host.nil?
|
|
||||||
redirect_url.scheme = url.scheme if redirect_url.scheme.nil?
|
|
||||||
redirect_url.port = url.port if redirect_url.port.nil?
|
|
||||||
return extract_og_image_from_page(redirect_url, n - 1)
|
|
||||||
elsif resp.is_a?(Net::HTTPSuccess)
|
|
||||||
doc = Nokogiri::HTML(resp.body)
|
|
||||||
images = doc.css("meta[property='og:image']")
|
|
||||||
return images.first.attr("content").sub(":large", ":orig")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -28,18 +28,14 @@ class SavedSearch < ApplicationRecord
|
|||||||
"queries" => queries
|
"queries" => queries
|
||||||
}.to_json
|
}.to_json
|
||||||
|
|
||||||
uri = URI.parse("#{Danbooru.config.listbooru_server}/v2/search")
|
uri = "#{Danbooru.config.listbooru_server}/v2/search"
|
||||||
|
|
||||||
body = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.is_a?(URI::HTTPS)) do |http|
|
resp = HTTParty.post(uri, body: json)
|
||||||
resp = http.request_post(uri.request_uri, json)
|
if resp.success?
|
||||||
if resp.is_a?(Net::HTTPSuccess)
|
resp.body.to_s.scan(/\d+/).map(&:to_i)
|
||||||
resp.body
|
else
|
||||||
else
|
raise "HTTP error code: #{resp.code} #{resp.message}"
|
||||||
raise "HTTP error code: #{resp.code} #{resp.message}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
body.to_s.scan(/\d+/).map(&:to_i)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -174,7 +174,7 @@ class User < ApplicationRecord
|
|||||||
def update_remote_cache
|
def update_remote_cache
|
||||||
if name_changed?
|
if name_changed?
|
||||||
Danbooru.config.other_server_hosts.each do |server|
|
Danbooru.config.other_server_hosts.each do |server|
|
||||||
Net::HTTP.delete(URI.parse("http://#{server}/users/#{id}/cache"))
|
HTTParty.delete("http://#{server}/users/#{id}/cache")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue Exception
|
rescue Exception
|
||||||
|
|||||||
Reference in New Issue
Block a user