|
| 1 | +import re |
| 2 | +import time |
| 3 | +from urllib.parse import urlparse, quote |
| 4 | +import os |
| 5 | +from os import environ |
| 6 | +import gzip |
| 7 | + |
| 8 | +from python_anticaptcha import AnticaptchaClient, FunCaptchaProxylessTask |
| 9 | +from selenium.webdriver.common.by import By |
| 10 | + |
| 11 | +api_key = environ["KEY"] |
| 12 | +site_key_pattern = 'public_key: "(.+?)",' |
| 13 | +url = "https://client-demo.arkoselabs.com/solo-animals" |
| 14 | +EXPECTED_RESULT = "Solved!" |
| 15 | +client = AnticaptchaClient(api_key) |
| 16 | + |
| 17 | +DIR = os.path.dirname(os.path.abspath(__file__)) |
| 18 | + |
| 19 | +with open(os.path.join(DIR, "callback_sniffer.js"), "rb") as fp: |
| 20 | + wrapper_code = fp.read() |
| 21 | + |
| 22 | + |
| 23 | +def get_token(url, site_key): |
| 24 | + task = FunCaptchaProxylessTask(website_url=url, website_key=site_key) |
| 25 | + job = client.createTask(task) |
| 26 | + job.join(maximum_time=60 * 15) |
| 27 | + return job.get_token_response() |
| 28 | + |
| 29 | + |
| 30 | +def process(driver): |
| 31 | + driver.get(url) |
| 32 | + site_key = get_sitekey(driver) |
| 33 | + print("Found site-key", site_key) |
| 34 | + token = get_token(url, site_key) |
| 35 | + print("Found token", token) |
| 36 | + form_submit(driver, token) |
| 37 | + return driver.page_source |
| 38 | + |
| 39 | + |
| 40 | +def form_submit(driver, token): |
| 41 | + script = """ |
| 42 | + document.getElementById('FunCaptcha-Token').value = decodeURIComponent('{0}'); |
| 43 | + document.getElementById('verification-token').value = decodeURIComponent('{0}'); |
| 44 | + document.getElementById('submit-btn').disabled = false; |
| 45 | + """.format( |
| 46 | + quote(token) |
| 47 | + ) |
| 48 | + time.sleep(1) |
| 49 | + # as example call callback - not required in that example |
| 50 | + driver.execute_script("ArkoseEnforcement.funcaptchaCallback[0]('{}')".format(token)) |
| 51 | + driver.find_element(By.ID, "submit-btn").click() |
| 52 | + time.sleep(1) |
| 53 | + |
| 54 | + |
| 55 | +def get_sitekey(driver): |
| 56 | + return re.search(site_key_pattern, driver.page_source).group(1) |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + from seleniumwire import webdriver # Import from seleniumwire |
| 61 | + |
| 62 | + def custom(req, req_body, res, res_body): |
| 63 | + if not req.path: |
| 64 | + return |
| 65 | + if not "arkoselabs" in req.path: |
| 66 | + return |
| 67 | + if not res.headers.get("Content-Type", None) in [ |
| 68 | + "text/javascript", |
| 69 | + "application/javascript", |
| 70 | + ]: |
| 71 | + print( |
| 72 | + "Skip invalid content type", |
| 73 | + req.path, |
| 74 | + res.headers.get("Content-Type", None), |
| 75 | + ) |
| 76 | + return |
| 77 | + if res.headers["Content-Encoding"] == "gzip": |
| 78 | + del res.headers["Content-Encoding"] |
| 79 | + res_body = gzip.decompress(res_body) |
| 80 | + return res_body + wrapper_code |
| 81 | + |
| 82 | + driver = webdriver.Firefox(seleniumwire_options={"custom_response_handler": custom}) |
| 83 | + |
| 84 | + try: |
| 85 | + assert EXPECTED_RESULT in process(driver) |
| 86 | + finally: |
| 87 | + driver.close() |
0 commit comments