Skip to content

Commit

Permalink
Release 1.2.8
Browse files Browse the repository at this point in the history
Fixed an issue with reCaptcha where if urllib3 < 1.25.1 and content was brotli compressed, it was not decompressing the brotli content.
  • Loading branch information
VeNoMouS committed Nov 12, 2019
1 parent 91240cd commit f92a322
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions cloudscraper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

# ------------------------------------------------------------------------------- #

__version__ = '1.2.7'
__version__ = '1.2.8'

# ------------------------------------------------------------------------------- #

Expand Down Expand Up @@ -135,6 +135,24 @@ def debugRequest(req):
except ValueError as e:
print("Debug Error: {}".format(getattr(e, 'message', e)))

# ------------------------------------------------------------------------------- #
# Decode Brotli on older versions of urllib3 manually
# ------------------------------------------------------------------------------- #

def decodeBrotli(self, resp):
if requests.packages.urllib3.__version__ < '1.25.1' and resp.headers.get('Content-Encoding') == 'br':
if self.allow_brotli and resp._content:
resp._content = brotli.decompress(resp.content)
else:
logging.warning(
'You\'re running urllib3 {}, Brotli content detected, '
'Which requires manual decompression, '
'But option allow_brotli is set to False, '
'We will not continue to decompress.'.format(requests.packages.urllib3.__version__)
)

return resp

# ------------------------------------------------------------------------------- #
# construct a cipher suite of ciphers the system actually supports
# ------------------------------------------------------------------------------- #
Expand Down Expand Up @@ -167,14 +185,9 @@ def request(self, method, url, *args, **kwargs):
if kwargs.get('proxies') and kwargs.get('proxies') != self.proxies:
self.proxies = kwargs.get('proxies')

resp = super(CloudScraper, self).request(method, url, *args, **kwargs)

if requests.packages.urllib3.__version__ < '1.25.1' and resp.headers.get('Content-Encoding') == 'br':
if self.allow_brotli and resp._content:
resp._content = brotli.decompress(resp.content)
else:
logging.warning('Brotli content detected, But option is disabled, we will not continue.')
return resp
resp = self.decodeBrotli(
super(CloudScraper, self).request(method, url, *args, **kwargs)
)

# ------------------------------------------------------------------------------- #
# Debug request
Expand All @@ -192,7 +205,9 @@ def request(self, method, url, *args, **kwargs):
# ------------------------------------------------------------------------------- #

self.request('GET', resp.url, *args, **kwargs)
resp = super(CloudScraper, self).request(method, url, *args, **kwargs)
resp = self.decodeBrotli(
super(CloudScraper, self).request(method, url, *args, **kwargs)
)
else:
# ------------------------------------------------------------------------------- #
# Try to solve the challenge and send it back
Expand Down Expand Up @@ -322,7 +337,10 @@ def Challenge_Response(self, resp, **kwargs):
# if cfuid is populated before issuing reCaptcha.
# ------------------------------------------------------------------------------- #

resp = super(CloudScraper, self).request(resp.request.method, resp.url, **kwargs)
resp = self.decodeBrotli(
super(CloudScraper, self).request(resp.request.method, resp.url, **kwargs)
)

if not self.is_reCaptcha_Challenge(resp):
return resp

Expand Down

0 comments on commit f92a322

Please sign in to comment.