Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle FANBOX Cloudflare check failures. #1345

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions PixivBrowserFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,16 @@ def open_with_retry(self, url, data=None, timeout=60, retry=0):
try:
res = self.open(url, data, timeout)
return res
except urllib.error.HTTPError:
except urllib.error.HTTPError as fanboxError:
if res is not None:
print(f"Error Code: {res.code}")
print(f"Response Headers: {res.headers}")
if res.code == '302':
print(f"Redirect to {res.headers['location']}")
else:
# Issue #1342
if "challenge_basic_security_FANBOX" in str(fanboxError.get_data()) and fanboxError.getcode() == 403:
return fanboxError
raise
except BaseException:
exc_value = sys.exc_info()[1]
Expand Down Expand Up @@ -382,6 +386,7 @@ def fanboxLoginUsingCookie(self, login_cookie=None):
if login_cookie is None or len(login_cookie) == 0:
login_cookie = self._config.cookieFanbox

# Issue #1342
if self._config.cf_clearance != "":
ck1 = http.cookiejar.Cookie(version=0, name='cf_clearance', value=self._config.cf_clearance, port=None,
port_specified=False, domain='fanbox.cc', domain_specified=False,
Expand Down Expand Up @@ -409,7 +414,7 @@ def fanboxLoginUsingCookie(self, login_cookie=None):
try:
res = self.open_with_retry(req)
parsed = BeautifulSoup(res, features="html5lib")
PixivHelper.get_logger().info('Logging in with cookit to Fanbox, return url: %s', res.geturl())
PixivHelper.get_logger().info('Logging in with cookie to Fanbox, return url: %s', res.geturl())
res.close()
except BaseException:
PixivHelper.get_logger().error('Error at fanboxLoginUsingCookie(): %s', sys.exc_info())
Expand All @@ -418,6 +423,13 @@ def fanboxLoginUsingCookie(self, login_cookie=None):
if '"user":{"isLoggedIn":true' in str(parsed.decode('utf-8')):
result = True
self._is_logged_in_to_FANBOX = True
# Issue #1342
elif "challenge_basic_security_FANBOX" in str(parsed.decode('utf-8')):
fanboxErrorPage = parsed.decode('utf-8')
parsed.decompose()
del parsed
raise PixivException("Failed FANBOX Cloudflare CAPTCHA challenge, please check your cookie and user-agent settings.",
errorCode=PixivException.CANNOT_LOGIN, htmlPage=fanboxErrorPage)
parsed.decompose()
del parsed

Expand Down