From 46e9f2dede1b86bb16a55fb884b891ee780a4449 Mon Sep 17 00:00:00 2001 From: lllusion3469 <31420484+lllusion3469@users.noreply.github.com> Date: Sun, 10 May 2020 19:04:24 +0200 Subject: [PATCH] deviantart: switch to Danbooru::Http httprb doesn't seem to support a base_uri parameter so use URI.join with a relative path instead --- app/logical/deviant_art_api_client.rb | 26 ++++++++----------- app/logical/sources/strategies/deviant_art.rb | 16 ++++-------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/app/logical/deviant_art_api_client.rb b/app/logical/deviant_art_api_client.rb index 07bcf50a2..769690942 100644 --- a/app/logical/deviant_art_api_client.rb +++ b/app/logical/deviant_art_api_client.rb @@ -15,22 +15,22 @@ class DeviantArtApiClient 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 = {}) - @client_id, @client_secret, @httparty_options = client_id, client_secret, httparty_options + def initialize(client_id, client_secret) + @client_id, @client_secret = client_id, client_secret end # https://www.deviantart.com/developers/http/v1/20160316/deviation_single/bcc296bdf3b5e40636825a942a514816 def deviation(uuid) - request("/deviation/#{uuid}") + request("deviation/#{uuid}") end # https://www.deviantart.com/developers/http/v1/20160316/deviation_download/bed6982b88949bdb08b52cd6763fcafd def download(uuid, mature_content: "1") - request("/deviation/download/#{uuid}", mature_content: mature_content) + request("deviation/download/#{uuid}", mature_content: mature_content) end # https://www.deviantart.com/developers/http/v1/20160316/deviation_metadata/7824fc14d6fba6acbacca1cf38c24158 @@ -43,19 +43,15 @@ class DeviantArtApiClient ext_stats: ext_stats, } - request("/deviation/metadata", **params) + request("deviation/metadata", **params) end def request(url, **params) - options = { - base_uri: BASE_URL, - params: { access_token: access_token.token, **params }, - headers: { "Accept-Encoding" => "gzip" }, - format: :plain, - } + params = { access_token: access_token.token, **params } - body, code = HTTParty.get(url, **options) - JSON.parse(Zlib.gunzip(body), symbolize_names: true) + url = URI.join(BASE_URL, url).to_s + response = Danbooru::Http.cache(1.minute).get(url, params: params) + response.parse.with_indifferent_access end def oauth diff --git a/app/logical/sources/strategies/deviant_art.rb b/app/logical/sources/strategies/deviant_art.rb index d0dda62cf..6f39eea33 100644 --- a/app/logical/sources/strategies/deviant_art.rb +++ b/app/logical/sources/strategies/deviant_art.rb @@ -246,20 +246,15 @@ module Sources def page return nil if page_url_from_image_url.blank? - options = Danbooru.config.httparty_options.deep_merge( - format: :plain, - headers: { "Accept-Encoding" => "gzip" } - ) - resp = HTTParty.get(page_url_from_image_url, **options) + resp = Danbooru::Http.cache(1.minute).get(page_url_from_image_url, follow: {max_hops: 1}) - if resp.success? - body = Zlib.gunzip(resp.body) - Nokogiri::HTML(body) + if resp.status.success? + Nokogiri::HTML(resp.body.to_s) # the work was deleted elsif resp.code == 404 nil else - raise HTTParty::ResponseError.new(resp) + raise "failed to fetch page (got code #{resp.code})" end end memoize :page @@ -280,8 +275,7 @@ module Sources def api_client api_client = DeviantArtApiClient.new( Danbooru.config.deviantart_client_id, - Danbooru.config.deviantart_client_secret, - Danbooru.config.httparty_options + Danbooru.config.deviantart_client_secret ) api_client.access_token = Cache.get("da-access-token", 11.weeks) do api_client.access_token.to_hash