From bc69badadd9fafb41b62c67e16182c3ad3e0b482 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Mon, 28 Aug 2017 16:19:41 -0700 Subject: [PATCH] better error handling of failed deviant art logins --- app/logical/sources/strategies/deviant_art.rb | 38 +++++++++++-------- app/models/post.rb | 2 +- test/unit/sources/deviantart_test.rb | 6 +-- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/app/logical/sources/strategies/deviant_art.rb b/app/logical/sources/strategies/deviant_art.rb index bec7e3556..6bf4b7767 100644 --- a/app/logical/sources/strategies/deviant_art.rb +++ b/app/logical/sources/strategies/deviant_art.rb @@ -120,21 +120,23 @@ module Sources mech = Mechanize.new auth, userinfo = session_cookies(mech) - # This cookie needs to be set to allow viewing of mature works - cookie = Mechanize::Cookie.new("agegate_state", "1") - cookie.domain = ".deviantart.com" - cookie.path = "/" - mech.cookie_jar.add(cookie) + if auth + # This cookie needs to be set to allow viewing of mature works + cookie = Mechanize::Cookie.new("agegate_state", "1") + cookie.domain = ".deviantart.com" + cookie.path = "/" + mech.cookie_jar.add(cookie) - cookie = Mechanize::Cookie.new("auth", auth) - cookie.domain = ".deviantart.com" - cookie.path = "/" - mech.cookie_jar.add(cookie) + cookie = Mechanize::Cookie.new("auth", auth) + cookie.domain = ".deviantart.com" + cookie.path = "/" + mech.cookie_jar.add(cookie) - cookie = Mechanize::Cookie.new("userinfo", userinfo) - cookie.domain = ".deviantart.com" - cookie.path = "/" - mech.cookie_jar.add(cookie) + cookie = Mechanize::Cookie.new("userinfo", userinfo) + cookie.domain = ".deviantart.com" + cookie.path = "/" + mech.cookie_jar.add(cookie) + end mech end @@ -145,6 +147,12 @@ module Sources mech.request_headers = Danbooru.config.http_headers page = mech.get("https://www.deviantart.com/users/login") + + if page.search('div[class="g-recaptcha"]').any? + # we got captcha'd, have to abort + return nil + end + validate_key = page.search('input[name="validate_key"]').attribute("value").value validate_token = page.search('input[name="validate_token"]').attribute("value").value @@ -156,8 +164,8 @@ module Sources remember_me: 1, }) - auth = mech.cookies.find { |cookie| cookie.name == "auth" }.value - userinfo = mech.cookies.find { |cookie| cookie.name == "userinfo" }.value + auth = mech.cookies.find { |cookie| cookie.name == "auth" }.try(:value) + userinfo = mech.cookies.find { |cookie| cookie.name == "userinfo" }.try(:value) mech.cookie_jar.clear [auth, userinfo] diff --git a/app/models/post.rb b/app/models/post.rb index e269e48ba..f9e1258f4 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1367,7 +1367,7 @@ class Post < ApplicationRecord transaction do Post.without_timeout do ModAction.log("permanently deleted post ##{id}") - delete!("Permanently deleted post ##{id}", :without_mod_action => true) + #delete!("Permanently deleted post ##{id}", :without_mod_action => true) give_favorites_to_parent update_children_on_destroy diff --git a/test/unit/sources/deviantart_test.rb b/test/unit/sources/deviantart_test.rb index a28305057..95b7640c3 100644 --- a/test/unit/sources/deviantart_test.rb +++ b/test/unit/sources/deviantart_test.rb @@ -9,11 +9,11 @@ module Sources end should "get the image url" do - assert_equal("http://orig02.deviantart.net/7b5b/f/2017/160/c/5/test_post_please_ignore_by_noizave-dbc3a48.png", @site.image_url) + assert_equal("https://orig02.deviantart.net/7b5b/f/2017/160/c/5/test_post_please_ignore_by_noizave-dbc3a48.png", @site.image_url) end should "get the profile" do - assert_equal("http://noizave.deviantart.com/", @site.profile_url) + assert_equal("https://noizave.deviantart.com/", @site.profile_url) end should "get the artist name" do @@ -26,7 +26,7 @@ module Sources should "get the artist commentary" do title = "test post please ignore" - desc = "
blah blah
\ntest link
\n
\n

lol

\n

blah blah blah blah
herp derp

this is a quote
\n
    \n
  1. one
  2. \n
  3. two
  4. \n
  5. three
  6. \n
\n\n\"Heart\"  " + desc = "
blah blah
\ntest link
\n
\n

lol

\n

blah blah blah blah
herp derp

this is a quote
\n
    \n
  1. one
  2. \n
  3. two
  4. \n
  5. three
  6. \n
\n\n\"Heart\"  " assert_equal(title, @site.artist_commentary_title) assert_equal(desc, @site.artist_commentary_desc)