nicoseiga: fix regression with http redirects.

3cdf67920 changed it so that Danbooru::Http follows redirects by
default. This broke some things in the Nico Seiga strategy, so disable
following redirects in the Nico Seiga API client for now.

Also change it so that Danbooru::Http follows redirects after a POST
request (by setting `strict: false`). Nico Seiga needs this because it
sends a redirect after we POST the login form.
This commit is contained in:
evazion
2020-06-15 22:07:41 -05:00
parent 962e60f4f1
commit 7868e5045e
2 changed files with 16 additions and 8 deletions

View File

@@ -6,7 +6,7 @@ module Danbooru
attr_writer :cache, :http attr_writer :cache, :http
class << self class << self
delegate :get, :put, :post, :delete, :cache, :timeout, :auth, :basic_auth, :headers, to: :new delegate :get, :put, :post, :delete, :cache, :follow, :timeout, :auth, :basic_auth, :headers, to: :new
end end
def get(url, **options) def get(url, **options)
@@ -29,6 +29,10 @@ module Danbooru
dup.tap { |o| o.cache = expiry.to_i } dup.tap { |o| o.cache = expiry.to_i }
end end
def follow(*args)
dup.tap { |o| o.http = o.http.follow(*args) }
end
def timeout(*args) def timeout(*args)
dup.tap { |o| o.http = o.http.timeout(*args) } dup.tap { |o| o.http = o.http.timeout(*args) }
end end
@@ -77,7 +81,7 @@ module Danbooru
def http def http
@http ||= ::HTTP. @http ||= ::HTTP.
follow(max_hops: MAX_REDIRECTS). follow(strict: false, max_hops: MAX_REDIRECTS).
timeout(DEFAULT_TIMEOUT). timeout(DEFAULT_TIMEOUT).
use(:auto_inflate). use(:auto_inflate).
headers(Danbooru.config.http_headers). headers(Danbooru.config.http_headers).

View File

@@ -2,9 +2,13 @@ class NicoSeigaApiClient
extend Memoist extend Memoist
XML_API = "https://seiga.nicovideo.jp/api" XML_API = "https://seiga.nicovideo.jp/api"
def initialize(work_id:, type:) attr_reader :http
# XXX temp disable following redirects.
def initialize(work_id:, type:, http: Danbooru::Http.follow(nil))
@work_id = work_id @work_id = work_id
@work_type = type @work_type = type
@http = http
end end
def image_ids def image_ids
@@ -51,7 +55,7 @@ class NicoSeigaApiClient
api_response = JSON.parse(resp)["target_image"] api_response = JSON.parse(resp)["target_image"]
elsif @work_type == "manga" elsif @work_type == "manga"
resp = Danbooru::Http.cache(1.minute).get("#{XML_API}/theme/info?id=#{@work_id}") resp = http.cache(1.minute).get("#{XML_API}/theme/info?id=#{@work_id}")
return {} if resp.blank? || resp.code.to_i == 404 return {} if resp.blank? || resp.code.to_i == 404
api_response = Hash.from_xml(resp.to_s)["response"]["theme"] api_response = Hash.from_xml(resp.to_s)["response"]["theme"]
end end
@@ -70,7 +74,7 @@ class NicoSeigaApiClient
end end
def user_api_response(user_id) def user_api_response(user_id)
resp = Danbooru::Http.cache(1.minute).get("#{XML_API}/user/info?id=#{user_id}") resp = http.cache(1.minute).get("#{XML_API}/user/info?id=#{user_id}")
return {} if resp.blank? || resp.code.to_i == 404 return {} if resp.blank? || resp.code.to_i == 404
Hash.from_xml(resp.to_s)["response"]["user"] Hash.from_xml(resp.to_s)["response"]["user"]
end end
@@ -78,11 +82,11 @@ class NicoSeigaApiClient
def get(url) def get(url)
cookie_header = Cache.get("nicoseiga-cookie-header") || regenerate_cookie_header cookie_header = Cache.get("nicoseiga-cookie-header") || regenerate_cookie_header
resp = Danbooru::Http.headers({Cookie: cookie_header}).cache(1.minute).get(url) resp = http.headers({Cookie: cookie_header}).cache(1.minute).get(url)
if resp.headers["Location"] =~ %r{seiga\.nicovideo\.jp/login/}i if resp.headers["Location"] =~ %r{seiga\.nicovideo\.jp/login/}i
cookie_header = regenerate_cookie_header cookie_header = regenerate_cookie_header
resp = Danbooru::Http.headers({Cookie: cookie_header}).cache(1.minute).get(url) resp = http.headers({Cookie: cookie_header}).cache(1.minute).get(url)
end end
resp resp
@@ -93,7 +97,7 @@ class NicoSeigaApiClient
mail_tel: Danbooru.config.nico_seiga_login, mail_tel: Danbooru.config.nico_seiga_login,
password: Danbooru.config.nico_seiga_password password: Danbooru.config.nico_seiga_password
} }
resp = Danbooru::Http.post("https://account.nicovideo.jp/api/v1/login", form: form) resp = http.post("https://account.nicovideo.jp/api/v1/login", form: form)
cookies = resp.cookies.map { |c| c.name + "=" + c.value } cookies = resp.cookies.map { |c| c.name + "=" + c.value }
cookies << "accept_fetish_warning=2" cookies << "accept_fetish_warning=2"