def _get_client(api_token): solver = TwoCaptcha(api_token) try: balance = solver.balance() if not balance: raise InvalidOrNoBalanceApiToken('No balance left on the token.') except ApiException as msg: raise InvalidOrNoBalanceApiToken(msg) from None return solver
def get_recaptcha_response(api_key: str) -> str: """ Solve the recaptcha and return the response. This takes a minute or two. NOTE: This is configured to use 2Captcha as the captcha solving service. If you want to use a different service you'll have to modify this function. """ solver = TwoCaptcha(apiKey=api_key) balance = solver.balance() print(f"2Captcha current balance: ${balance}...") if balance < 0.1: warnings.warn(f"2Captcha balance is running low") r = solver.recaptcha(sitekey=config.RECAPTCHA_SITE_KEY, url=config.WEBSITE_URL) return r["code"]
def captcha_solver(self, captcha_service, **kwargs): """Function to solve captcha using specific captcha solving service Type of captcha depends on image path was specified or not. """ try: # initialization sitekey = kwargs.get('sitekey') captcha_url = kwargs.get('captcha_url') captcha_img = kwargs.get('captcha_img') captcha_type = kwargs.get('captcha_type', 4) captcha_action = kwargs.get('captcha_action') balance = None if captcha_service == "DBC": dbc_token = config['DBC_TOKEN'] client = deathbycaptcha.HttpClient(None, None, dbc_token) try: balance = client.get_balance() print("current balance:", balance) except Exception as exc: error_msg = { "error_type": "CAPTCHA_SOLVER", "details": str(exc) } self.logger.warning(error_msg) if sitekey and captcha_url: print("CAPTCHA key:", sitekey) captcha_dict = { 'googlekey': sitekey, 'pageurl': captcha_url } if captcha_type == 5: captcha_dict.update({ 'action': captcha_action, 'min_score': 0.3 }) json_captcha = json.dumps(captcha_dict) captcha = client.decode(type=captcha_type, token_params=json_captcha) elif captcha_img: captcha = client.decode(captcha_img) if captcha: if captcha['is_correct']: print("CAPTCHA %s solved" % captcha["captcha"]) return (captcha['captcha'], captcha['text']) else: print("CAPTCHA %s solved incorrectly" % captcha["captcha"]) client.report(captcha["captcha"]) else: print("CAPTCHA doesn't exist.") elif captcha_service == "2Captcha": api_key = config['2CAPTCHA'] client = TwoCaptcha(api_key) try: balance = client.balance() print("current balance:", balance) except Exception as exc: error_msg = { "error_type": "CAPTCHA_SOLVER", "details": str(exc) } self.logger.warning(error_msg) if sitekey and captcha_url: captcha = client.recaptcha(sitekey=sitekey, url=captcha_url) elif captcha_img: captcha = client.normal(captcha_img, numeric=4, language=2, lang='en') return captcha['captchaId'], captcha['code'] except Exception as exc: error_msg = { "error_type": "CAPTCHA_FAILED", "captcha_service": captcha_service, "balance": balance, "details": str(exc) } raise Exception(error_msg) return (None, None)
def solveCaptcha(driver): global RECAPTCHA_APPEARED st = time() print("Solving Captcha...") solver = TwoCaptcha(API_KEY) try: balance = solver.balance() if balance == 0: print("Account Balance is 0") driver.quit() sys.exit() except twocaptcha.api.ApiException: print("Invalid 2Captcha API_KEY") driver.quit() sys.exit() def get_sitekey(driver): sitekey = None found_captcha = False print("Find captcha") wait_5_secs = 0 while not found_captcha: if wait_5_secs == 5: print("Could not find captcha") break if is_logged_in(driver): print("Break coz is_logged_in") break try: recaptcha = driver.find_element_by_class_name("g-recaptcha") found_captcha = True sitekey = recaptcha.get_attribute("data-sitekey") print(f"Got sitekey {sitekey}") except: sleep(1) wait_5_secs += 1 print(wait_5_secs) return sitekey def form_submit(driver, token): # driver.execute_script("token='" + token + "'") # driver.execute_script("document.getElementById(\"g-recaptcha-response\").innerHTML=token") # driver.execute_script("_grecaptchaCallback(token)") driver.execute_script( f'document.querySelector(".g-recaptcha-response").innerText="{token}";' ) sleep(1) sitekey = get_sitekey(driver) if sitekey is not None and not is_logged_in(driver): print("Solving Captcha.....") result = solver.recaptcha(sitekey=sitekey, url=driver.current_url, version="v2") # if not Debug: print(result) form_submit(driver, result["code"]) RECAPTCHA_APPEARED = True print("Solved captcha!! Takes " + str(time() - st) + " seconds") # elif Debug: # print("debug return") return result else: print("Exit solveCaptcha")