From 6e6ce6e62f79f0c97251d4e1012009f8c138e021 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 20 Jun 2020 23:08:38 -0500 Subject: [PATCH] nijie: replace Mechanize with Danbooru::Http. The Nijie login process works like this: * First we submit our `email` and `password` to `https://nijie.info/login_int.php`. * Then we save the NIJIEIEID session cookie from the response. * We optionally retry if login failed. Nijie returns 429 errors with a `Retry-After: 5` header if we send too many login requests. This can happen during parallel testing. * We cache the login cookies for only 1 hour so we don't have to worry about them becoming invalid if we cache them too long. Cookies and retrying errors on failure are handled transparently by Danbooru::Http. --- app/logical/sources/strategies/nijie.rb | 52 ++++--------------------- test/unit/sources/nijie_test.rb | 2 - 2 files changed, 8 insertions(+), 46 deletions(-) diff --git a/app/logical/sources/strategies/nijie.rb b/app/logical/sources/strategies/nijie.rb index b24605168..f3a15b576 100644 --- a/app/logical/sources/strategies/nijie.rb +++ b/app/logical/sources/strategies/nijie.rb @@ -178,54 +178,18 @@ module Sources def page return nil if page_url.blank? - doc = agent.get(page_url) + http = Danbooru::Http.new + form = { email: Danbooru.config.nijie_login, password: Danbooru.config.nijie_password } + response = http.cache(1.hour).post("https://nijie.info/login_int.php", form: form) + return nil unless response.status == 200 - if doc.search("div#header-login-container").any? - # Session cache is invalid, clear it and log in normally. - Cache.delete("nijie-session") - doc = agent.get(page_url) - end + response = http.cookies(R18: 1).cache(1.minute).get(page_url) + return nil unless response.status == 200 - doc - rescue Mechanize::ResponseCodeError => e - return nil if e.response_code.to_i == 404 - raise + response&.parse end + memoize :page - - def agent - mech = Mechanize.new - - session = Cache.get("nijie-session") - if session - cookie = Mechanize::Cookie.new("NIJIEIJIEID", session) - cookie.domain = ".nijie.info" - cookie.path = "/" - mech.cookie_jar.add(cookie) - else - mech.get("https://nijie.info/login.php") do |page| - page.form_with(:action => "/login_int.php") do |form| - form['email'] = Danbooru.config.nijie_login - form['password'] = Danbooru.config.nijie_password - end.click_button - end - session = mech.cookie_jar.cookies.select {|c| c.name == "NIJIEIJIEID"}.first - Cache.put("nijie-session", session.value, 1.day) if session - end - - # This cookie needs to be set to allow viewing of adult works while anonymous - cookie = Mechanize::Cookie.new("R18", "1") - cookie.domain = ".nijie.info" - cookie.path = "/" - mech.cookie_jar.add(cookie) - - mech - rescue Mechanize::ResponseCodeError => e - raise unless e.response_code.to_i == 429 - sleep(5) - retry - end - memoize :agent end end end diff --git a/test/unit/sources/nijie_test.rb b/test/unit/sources/nijie_test.rb index 796bcfd46..7ca695c90 100644 --- a/test/unit/sources/nijie_test.rb +++ b/test/unit/sources/nijie_test.rb @@ -187,8 +187,6 @@ module Sources desc = <<-EOS.strip_heredoc.chomp foo [b]bold[/b] [i]italics[/i] [s]strike[/s] red - - EOS