From 44f826d8fa82ab35e7e574be6b2b1ebb04995759 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 24 Jun 2020 22:16:48 -0500 Subject: [PATCH] nicoseiga: optimize image_url method. The image_url method makes a request to `https://seiga.nicovideo.jp/images/source/:image_id` to see where this URL redirects to. Before we did a GET request, which caused it to download the full image. This could fail with a timeout error if the download took too long. We also cached the request, which caused the full image to be cached, even though we only need the headers. Change it to a HEAD request so we don't have to download the entire image just to check the URL. --- app/logical/nico_seiga_api_client.rb | 8 ++++++-- app/logical/sources/strategies/nico_seiga.rb | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/logical/nico_seiga_api_client.rb b/app/logical/nico_seiga_api_client.rb index d471836d6..4cdb75c9c 100644 --- a/app/logical/nico_seiga_api_client.rb +++ b/app/logical/nico_seiga_api_client.rb @@ -78,7 +78,7 @@ class NicoSeigaApiClient Hash.from_xml(resp.to_s)["response"]["user"] end - def get(url) + def login form = { mail_tel: Danbooru.config.nico_seiga_login, password: Danbooru.config.nico_seiga_password @@ -88,7 +88,11 @@ class NicoSeigaApiClient resp = http.cache(1.hour).post("https://account.nicovideo.jp/login/redirector?site=seiga", form: form) raise RuntimeError, "NicoSeiga login failed (status=#{resp.status} url=#{url})" if resp.status != 200 - resp = http.cache(1.minute).get(url) + http + end + + def get(url) + resp = login.cache(1.minute).get(url) #raise RuntimeError, "NicoSeiga get failed (status=#{resp.status} url=#{url})" if resp.status != 200 resp diff --git a/app/logical/sources/strategies/nico_seiga.rb b/app/logical/sources/strategies/nico_seiga.rb index d4af63084..93a2d995c 100644 --- a/app/logical/sources/strategies/nico_seiga.rb +++ b/app/logical/sources/strategies/nico_seiga.rb @@ -81,7 +81,7 @@ module Sources else image_urls.first end - resp = api_client.get(img) + resp = api_client.login.head(img) if resp.uri.to_s =~ %r{https?://.+/(\w+/\d+/\d+)\z}i "https://lohas.nicoseiga.jp/priv/#{$1}" else