from base64 import b64encode, b64decode from requests.adapters import HTTPAdapter from requests.exceptions import RequestException from requests.models import Response from requests.sessions import Session from urllib3.util.ssl_ import create_urllib3_context import logging import random import re import ssl import time import js2py try: from urlparse import urlparse from urlparse import urlunparse except ImportError: # noinspection PyCompatibility from urllib.parse import urlparse # noinspection PyCompatibility from urllib.parse import urlunparse DEFAULT_USER_AGENTS = [ 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)' ' Chrome/41.0.2228.0 Safari/537.36', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko)' ' Chrome/50.0.2661.102 Safari/537.36', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)' ' Chrome/52.0.2743.116 Safari/537.36', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0)' ' Gecko/20100101 Firefox/46.0', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0)' ' Gecko/20100101 Firefox/41.0' ] class CloudflareError(RequestException): pass class CloudflareScraper(Session): def __init__(self, **kwargs): super(CloudflareScraper, self).__init__() if 'requests' in self.headers['User-Agent']: # Set a random User-Agent if no custom User-Agent has been set self.headers['User-Agent'] = random.choice(DEFAULT_USER_AGENTS) self.cf_ua = self.headers['User-Agent'] self.default_delay = 8 self.delay = kwargs.pop('delay', self.default_delay) self.start_time = None self.cipher_suite = self.load_cipher_suite() self.mount('https://', CloudflareAdapter(self.cipher_suite)) def request(self, method, url, *args, **kwargs): resp = super(CloudflareScraper, self).request(method, url, *args, **kwargs) # Check if anti-bot is on if (isinstance(resp, type(Response())) and resp.status_code in (503, 429, 403)): self.start_time = time.time() if (re.search('(?i)cloudflare', resp.headers.get('Server', '')) and 'jschl_vc' in resp.content and 'jschl_answer' in resp.content): resp = self.solve_cf_challenge(resp, **kwargs) elif 'ddgu' in resp.content: resp = self.solve_ddg_challenge(resp, **kwargs) # Otherwise, no anti-bot detected return resp def wait(self): delay = self.delay - (time.time() - self.start_time) time.sleep((0, delay)[0 < delay]) # required delay before solving the challenge def solve_ddg_challenge(self, resp, **original_kwargs): parsed_url = urlparse(resp.url) try: submit_url = parsed_url.scheme + ':' + re.findall('"frm"[^>]+?action="([^"]+)"', resp.text)[0] kwargs = {k: v for k, v in original_kwargs.items() if k not in ['hooks']} kwargs.setdefault('headers', {}) kwargs.setdefault('data', dict( h=b64encode('%s://%s' % (parsed_url.scheme, parsed_url.hostname)), u=b64encode(parsed_url.path), p=b64encode(parsed_url.port or '') )) self.wait() resp = self.request('POST', submit_url, **kwargs) except(Exception, BaseException): pass return resp def solve_cf_challenge(self, resp, **original_kwargs): body = resp.text parsed_url = urlparse(resp.url) domain = parsed_url.netloc if '/cdn-cgi/l/chk_captcha' in body: raise CloudflareError( 'Cloudflare captcha presented for %s, please notify SickGear for an update, ua: %s' % (domain, self.cf_ua), response=resp) submit_url = '%s://%s/cdn-cgi/l/chk_jschl' % (parsed_url.scheme, domain) cloudflare_kwargs = {k: v for k, v in original_kwargs.items() if k not in ['hooks']} params = cloudflare_kwargs.setdefault('params', {}) headers = cloudflare_kwargs.setdefault('headers', {}) headers['Referer'] = resp.url if self.delay == self.default_delay: try: # no instantiated delay, therefore check js for hard coded CF delay self.delay = float(re.search(r'submit\(\);[^0-9]*?([0-9]+)', body).group(1)) / float(1000) except(Exception, BaseException): pass for i in re.findall(r'(]+?hidden[^>]+?>)', body): value = re.findall(r'value="([^"\']+?)["\']', i) name = re.findall(r'name="([^"\']+?)["\']', i) if all([name, value]): params[name[0]] = value[0] js = self.extract_js(body, domain) atob = (lambda s: b64decode('%s' % s).decode('utf-8')) try: # Eval the challenge algorithm params['jschl_answer'] = str(js2py.EvalJs({'atob': atob}).eval(js)) except(Exception, BaseException): try: params['jschl_answer'] = str(js2py.EvalJs({'atob': atob}).eval(js)) except(Exception, BaseException) as e: # Something is wrong with the page. This may indicate Cloudflare has changed their anti-bot technique. raise ValueError('Unable to parse Cloudflare anti-bot IUAM page: %s' % e.message) # Requests transforms any request into a GET after a redirect, # so the redirect has to be handled manually here to allow for # performing other types of requests even as the first request. method = resp.request.method cloudflare_kwargs['allow_redirects'] = False self.wait() redirect = self.request(method, submit_url, **cloudflare_kwargs) location = redirect.headers.get('Location') r = urlparse(location) if not r.netloc: location = urlunparse((parsed_url.scheme, domain, r.path, r.params, r.query, r.fragment)) return self.request(method, location, **original_kwargs) @staticmethod def extract_js(body, domain): try: js = re.search( r'''(?x) setTimeout\(function\(\){\s*?(var\s*? (?:s,t,o,p,b,r,e,a,k,i,n,g|t,r,a),f.+?[\r\n\s\S]*?a\.value\s*=.+?)[\r\n]+ ''', body).group(1) except(Exception, BaseException): raise RuntimeError('Error #1 Cloudflare anti-bots changed, please notify SickGear for an update') if not re.search(r'(?i)(toFixed|t\.length)', js): raise RuntimeError('Error #2 Cloudflare anti-bots changed, please notify SickGear for an update') js = re.sub(r'(;\s+);', r'\1', js) js = re.sub(r'([)\]];)(\w)', r'\1\n\n\2', js) js = re.sub(r'\s*\';\s*\d+\'\s*$', '', js) innerHTML = re.search(r'(?sim)