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.
This commit is contained in:
@@ -178,54 +178,18 @@ module Sources
|
|||||||
def page
|
def page
|
||||||
return nil if page_url.blank?
|
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?
|
response = http.cookies(R18: 1).cache(1.minute).get(page_url)
|
||||||
# Session cache is invalid, clear it and log in normally.
|
return nil unless response.status == 200
|
||||||
Cache.delete("nijie-session")
|
|
||||||
doc = agent.get(page_url)
|
|
||||||
end
|
|
||||||
|
|
||||||
doc
|
response&.parse
|
||||||
rescue Mechanize::ResponseCodeError => e
|
|
||||||
return nil if e.response_code.to_i == 404
|
|
||||||
raise
|
|
||||||
end
|
end
|
||||||
|
|
||||||
memoize :page
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -187,8 +187,6 @@ module Sources
|
|||||||
desc = <<-EOS.strip_heredoc.chomp
|
desc = <<-EOS.strip_heredoc.chomp
|
||||||
foo [b]bold[/b] [i]italics[/i] [s]strike[/s] red
|
foo [b]bold[/b] [i]italics[/i] [s]strike[/s] red
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<http://nijie.info/view.php?id=218944>
|
<http://nijie.info/view.php?id=218944>
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user