deviantart: switch to Danbooru::Http

httprb doesn't seem to support a base_uri parameter so use URI.join with
a relative path instead
This commit is contained in:
lllusion3469
2020-05-10 19:04:24 +02:00
parent 2794cd254d
commit 46e9f2dede
2 changed files with 16 additions and 26 deletions

View File

@@ -15,22 +15,22 @@
class DeviantArtApiClient class DeviantArtApiClient
class Error < StandardError; end class Error < StandardError; end
BASE_URL = "https://www.deviantart.com/api/v1/oauth2" BASE_URL = "https://www.deviantart.com/api/v1/oauth2/"
attr_reader :client_id, :client_secret, :httparty_options attr_reader :client_id, :client_secret
def initialize(client_id, client_secret, httparty_options = {}) def initialize(client_id, client_secret)
@client_id, @client_secret, @httparty_options = client_id, client_secret, httparty_options @client_id, @client_secret = client_id, client_secret
end end
# https://www.deviantart.com/developers/http/v1/20160316/deviation_single/bcc296bdf3b5e40636825a942a514816 # https://www.deviantart.com/developers/http/v1/20160316/deviation_single/bcc296bdf3b5e40636825a942a514816
def deviation(uuid) def deviation(uuid)
request("/deviation/#{uuid}") request("deviation/#{uuid}")
end end
# https://www.deviantart.com/developers/http/v1/20160316/deviation_download/bed6982b88949bdb08b52cd6763fcafd # https://www.deviantart.com/developers/http/v1/20160316/deviation_download/bed6982b88949bdb08b52cd6763fcafd
def download(uuid, mature_content: "1") def download(uuid, mature_content: "1")
request("/deviation/download/#{uuid}", mature_content: mature_content) request("deviation/download/#{uuid}", mature_content: mature_content)
end end
# https://www.deviantart.com/developers/http/v1/20160316/deviation_metadata/7824fc14d6fba6acbacca1cf38c24158 # https://www.deviantart.com/developers/http/v1/20160316/deviation_metadata/7824fc14d6fba6acbacca1cf38c24158
@@ -43,19 +43,15 @@ class DeviantArtApiClient
ext_stats: ext_stats, ext_stats: ext_stats,
} }
request("/deviation/metadata", **params) request("deviation/metadata", **params)
end end
def request(url, **params) def request(url, **params)
options = { params = { access_token: access_token.token, **params }
base_uri: BASE_URL,
params: { access_token: access_token.token, **params },
headers: { "Accept-Encoding" => "gzip" },
format: :plain,
}
body, code = HTTParty.get(url, **options) url = URI.join(BASE_URL, url).to_s
JSON.parse(Zlib.gunzip(body), symbolize_names: true) response = Danbooru::Http.cache(1.minute).get(url, params: params)
response.parse.with_indifferent_access
end end
def oauth def oauth

View File

@@ -246,20 +246,15 @@ module Sources
def page def page
return nil if page_url_from_image_url.blank? return nil if page_url_from_image_url.blank?
options = Danbooru.config.httparty_options.deep_merge( resp = Danbooru::Http.cache(1.minute).get(page_url_from_image_url, follow: {max_hops: 1})
format: :plain,
headers: { "Accept-Encoding" => "gzip" }
)
resp = HTTParty.get(page_url_from_image_url, **options)
if resp.success? if resp.status.success?
body = Zlib.gunzip(resp.body) Nokogiri::HTML(resp.body.to_s)
Nokogiri::HTML(body)
# the work was deleted # the work was deleted
elsif resp.code == 404 elsif resp.code == 404
nil nil
else else
raise HTTParty::ResponseError.new(resp) raise "failed to fetch page (got code #{resp.code})"
end end
end end
memoize :page memoize :page
@@ -280,8 +275,7 @@ module Sources
def api_client def api_client
api_client = DeviantArtApiClient.new( api_client = DeviantArtApiClient.new(
Danbooru.config.deviantart_client_id, Danbooru.config.deviantart_client_id,
Danbooru.config.deviantart_client_secret, Danbooru.config.deviantart_client_secret
Danbooru.config.httparty_options
) )
api_client.access_token = Cache.get("da-access-token", 11.weeks) do api_client.access_token = Cache.get("da-access-token", 11.weeks) do
api_client.access_token.to_hash api_client.access_token.to_hash