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

Added docstrings to several undocumented functions #430

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions cfscrape/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ def __init__(self, *args, **kwargs):

@staticmethod
def is_cloudflare_iuam_challenge(resp):
"""
:param resp: The HTTP response object
:returns: True if the response is a
Cloudflare challenge, False otherwise
"""
return (
resp.status_code in (503, 429)
and resp.headers.get("Server", "").startswith("cloudflare")
Expand All @@ -111,6 +116,14 @@ def is_cloudflare_iuam_challenge(resp):

@staticmethod
def is_cloudflare_captcha_challenge(resp):
"""
Checks if the response is a Cloudflare captcha challenge.

:param resp: The
HTTP response to check.
:returns bool: True if the response is a Cloudflare
captcha challenge, False otherwise.
"""
return (
resp.status_code == 403
and resp.headers.get("Server", "").startswith("cloudflare")
Expand Down Expand Up @@ -138,6 +151,13 @@ def cloudflare_is_bypassed(self, url, resp=None):
)

def handle_captcha_challenge(self, resp, url):
"""
Cloudflare captcha challenge presented for `urlparse(url).netloc` (cfscrape
cannot solve captchas)

If you are using OpenSSL 1.1.0 or lower, it is
recommended to upgrade your OpenSSL library and recompile Python.
"""
error = (
"Cloudflare captcha challenge presented for %s (cfscrape cannot solve captchas)"
% urlparse(url).netloc
Expand Down Expand Up @@ -245,6 +265,14 @@ def solve_cf_challenge(self, resp, **original_kwargs):


def solve_challenge(self, body, domain):
"""
Solve the Cloudflare challenge by using JavaScript to compute the
challenge.

:param body: The HTTP response body from Cloudflare.
:param
domain: The domain we are solving for (used in JS).
"""
try:
all_scripts = re.findall(r'\<script type\=\"text\/javascript\"\>\n(.*?)\<\/script\>',body, flags=re.S)
javascript = next(filter(lambda w: "jschl-answer" in w,all_scripts)) #find the script tag which would have obfuscated js
Expand Down