diff --git a/app/logical/sources/strategies/deviant_art.rb b/app/logical/sources/strategies/deviant_art.rb index 5376ffeab..2e9ea50b0 100644 --- a/app/logical/sources/strategies/deviant_art.rb +++ b/app/logical/sources/strategies/deviant_art.rb @@ -1,6 +1,8 @@ module Sources module Strategies class DeviantArt < Base + DEVIANTART_SESSION_CACHE_KEY = "deviantart-session" + def self.url_match?(url) url =~ /^https?:\/\/(?:.+?\.)?deviantart\.(?:com|net)/ end @@ -152,16 +154,49 @@ module Sources def agent @agent ||= begin 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) + 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) + mech end end + + def session_cookies(mech) + Cache.get(DEVIANTART_SESSION_CACHE_KEY, 2.hours) do + page = mech.get("https://www.deviantart.com/users/login") + validate_key = page.search('input[name="validate_key"]').attribute("value").value + validate_token = page.search('input[name="validate_token"]').attribute("value").value + + mech.post("https://www.deviantart.com/users/login", { + username: Danbooru.config.deviantart_login, + password: Danbooru.config.deviantart_password, + validate_key: validate_key, + validate_token: validate_token, + remember_me: 1, + }) + + auth = mech.cookies.find { |cookie| cookie.name == "auth" }.value + userinfo = mech.cookies.find { |cookie| cookie.name == "userinfo" }.value + mech.cookie_jar.clear + + [auth, userinfo] + end + end end end end diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 63bbf297a..662f79b71 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -359,6 +359,14 @@ module Danbooru nil end + def deviantart_login + nil + end + + def deviantart_password + nil + end + def enable_dimension_autotagging true end diff --git a/test/unit/sources/deviantart_test.rb b/test/unit/sources/deviantart_test.rb index ce7ead7f5..ba5aedc68 100644 --- a/test/unit/sources/deviantart_test.rb +++ b/test/unit/sources/deviantart_test.rb @@ -71,7 +71,7 @@ module Sources @site.get end - should_eventually "get the image url" do + should "get the image url" do assert_equal("http://orig14.deviantart.net/cb25/f/2017/160/1/9/hidden_work_by_noizave-dbc3r29.png", @site.image_url) end end