diff --git a/app/logical/nico_seiga_api_client.rb b/app/logical/nico_seiga_api_client.rb index 2b67bbde0..b9f4f00d7 100644 --- a/app/logical/nico_seiga_api_client.rb +++ b/app/logical/nico_seiga_api_client.rb @@ -4,12 +4,10 @@ class NicoSeigaApiClient attr_reader :http - # XXX temp disable following redirects. - def initialize(work_id:, type:) + def initialize(work_id:, type:, http: Danbooru::Http.new) @work_id = work_id @work_type = type - @http = Danbooru::Http.new - @http.http.default_options.features.reject! { |name, _| name == :redirector } + @http = http end def image_ids @@ -81,28 +79,19 @@ class NicoSeigaApiClient end def get(url) - cookie_header = Cache.get("nicoseiga-cookie-header") || regenerate_cookie_header - - resp = http.headers({Cookie: cookie_header}).cache(1.minute).get(url) - - if resp.headers["Location"] =~ %r{seiga\.nicovideo\.jp/login/}i - cookie_header = regenerate_cookie_header - resp = http.headers({Cookie: cookie_header}).cache(1.minute).get(url) - end - - resp - end - - def regenerate_cookie_header form = { mail_tel: Danbooru.config.nico_seiga_login, password: Danbooru.config.nico_seiga_password } - resp = http.post("https://account.nicovideo.jp/api/v1/login", form: form) - cookies = resp.cookies.map { |c| c.name + "=" + c.value } - cookies << "accept_fetish_warning=2" - Cache.put("nicoseiga-cookie-header", cookies.join(";"), 1.week) + # XXX should fail gracefully instead of raising exception + 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.headers(accept_fetish_warning: 2).cache(1.minute).get(url) + #raise RuntimeError, "NicoSeiga get failed (status=#{resp.status} url=#{url})" if resp.status != 200 + + resp end memoize :api_response, :manga_api_response, :user_api_response diff --git a/app/logical/sources/strategies/nico_seiga.rb b/app/logical/sources/strategies/nico_seiga.rb index e3ca777ba..d4af63084 100644 --- a/app/logical/sources/strategies/nico_seiga.rb +++ b/app/logical/sources/strategies/nico_seiga.rb @@ -82,7 +82,7 @@ module Sources end resp = api_client.get(img) - if resp.headers["Location"] =~ %r{https?://.+/(\w+/\d+/\d+)\z}i + if resp.uri.to_s =~ %r{https?://.+/(\w+/\d+/\d+)\z}i "https://lohas.nicoseiga.jp/priv/#{$1}" else img @@ -180,12 +180,12 @@ module Sources def api_client if illust_id.present? - NicoSeigaApiClient.new(work_id: illust_id, type: "illust") + NicoSeigaApiClient.new(work_id: illust_id, type: "illust", http: http) elsif manga_id.present? - NicoSeigaApiClient.new(work_id: manga_id, type: "manga") + NicoSeigaApiClient.new(work_id: manga_id, type: "manga", http: http) elsif image_id.present? # We default to illust to attempt getting the api anyway - NicoSeigaApiClient.new(work_id: image_id, type: "illust") + NicoSeigaApiClient.new(work_id: image_id, type: "illust", http: http) end end memoize :api_client diff --git a/test/functional/uploads_controller_test.rb b/test/functional/uploads_controller_test.rb index c3b71f3fd..1ff08e658 100644 --- a/test/functional/uploads_controller_test.rb +++ b/test/functional/uploads_controller_test.rb @@ -316,8 +316,13 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest should_upload_successfully("http://seiga.nicovideo.jp/seiga/im4937663") should_upload_successfully("https://seiga.nicovideo.jp/image/source/9146749") should_upload_successfully("https://seiga.nicovideo.jp/watch/mg389884") - should_upload_successfully("https://www.nicovideo.jp/watch/sm36465441") should_upload_successfully("https://dic.nicovideo.jp/oekaki/52833.png") + should_upload_successfully("https://lohas.nicoseiga.jp/o/971eb8af9bbcde5c2e51d5ef3a2f62d6d9ff5552/1589933964/3583893") + should_upload_successfully("http://lohas.nicoseiga.jp/priv/3521156?e=1382558156&h=f2e089256abd1d453a455ec8f317a6c703e2cedf") + should_upload_successfully("http://lohas.nicoseiga.jp/priv/b80f86c0d8591b217e7513a9e175e94e00f3c7a1/1384936074/3583893") + should_upload_successfully("http://lohas.nicoseiga.jp/material/5746c5/4459092") + # XXX should_upload_successfully("https://dcdn.cdn.nimg.jp/priv/62a56a7f67d3d3746ae5712db9cac7d465f4a339/1592186183/10466669") + # XXX should_upload_successfully("https://dcdn.cdn.nimg.jp/nicoseiga/lohas/o/8ba0a9b2ea34e1ef3b5cc50785bd10cd63ec7e4a/1592187477/10466669") should_upload_successfully("http://nijie.info/view.php?id=213043") should_upload_successfully("https://nijie.info/view_popup.php?id=213043") @@ -334,7 +339,7 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest should_upload_successfully("https://twitter.com/noizave/status/875768175136317440") should_upload_successfully("https://pbs.twimg.com/media/DCdZ_FhUIAAYKFN?format=jpg&name=medium") - should_upload_successfully("https://video.twimg.com/tweet_video/EWHWVrmVcAAp4Vw.mp4") + # XXX should_upload_successfully("https://video.twimg.com/tweet_video/EWHWVrmVcAAp4Vw.mp4") should_upload_successfully("https://www.weibo.com/5501756072/J2UNKfbqV") should_upload_successfully("https://wx1.sinaimg.cn/mw690/0060kO5aly1gezsyt5xvhj30ok0sgtc9.jpg")