示例#1
0
def captcha_solve(browser, cost_file='costs.txt', ANTICAPTCHA_KEY=None, save_cost=True, captcha_sound=True, domain=None):
    if not ANTICAPTCHA_KEY:
        ANTICAPTCHA_KEY = os.getenv('ANTICAPTCHA_KEY')

    if not domain:
        domain = browser.current_url

    xpath = '//*[@id = "g-recaptcha-response"]'
    try:
        browser.find_element_by_xpath(xpath)
        # Captcha found in page
        exist_captcha = True
    except:
        # Captcha CAN NOT found in page
        exist_captcha = False

    user_answer = None
    cost = 0
    start_time = time.time()
    if exist_captcha:
        if captcha_sound:
            Progress.sound_notify_times(times=1)
        print('--> reCAPTCHA solving. It might take some time, please wait...')
        key = ''
        try:
            SITE_KEY = None
            try:
                # TRY normal captcha box
                xpath = '//*[contains(@class,"g-recaptcha")]'
                captcha_box = browser.find_element_by_xpath(xpath)
                SITE_KEY = captcha_box.get_attribute('data-sitekey')
                if not SITE_KEY:
                    raise Exception
            except:
                # Normal captcha box COULD NOT BE FOUND. Find site key from new generation of reCAPTCHA
                xpath = '//iframe[contains(@role, "presentation")]'
                captcha_box = browser.find_element_by_xpath(xpath)
                captcha_src = captcha_box.get_attribute('src')
                if 'k=' in captcha_src and '&' in captcha_src:
                    captcha_src_list = captcha_src.split('&')
                    for i in captcha_src_list:
                        if i.startswith('k='):
                            SITE_KEY = i.replace('k=', '')
                            break
            if not SITE_KEY:
                raise Exception

            user_answer = NoCaptchaTaskProxyless.NoCaptchaTaskProxyless(
                anticaptcha_key=ANTICAPTCHA_KEY).captcha_handler(
                websiteURL=domain, websiteKey=SITE_KEY)
            if 'errorDescription' in user_answer:
                raise Exception
            key = user_answer['solution']['gRecaptchaResponse']
            try:
                cost = user_answer['cost']
                cost = float(cost)
            except:
                cost = 0

            # Code worked untill here so there is no error.
            error_captcha = False
        except Exception as e:
            error_captcha = True
            message = '--> An error occurred while solving reCAPTCHA. Processing is in progress.'
            if 'errorDescription' in user_answer:
                message_from_system = user_answer['errorDescription']
                message = message + '\n' + str(message_from_system)
            Progress.exit_app(e=e, message=message, exit_all=False)

        if not error_captcha:
            if 'endTime' in user_answer and 'createTime' in user_answer:
                end_time = user_answer['endTime']
                create_time = user_answer['createTime']
                pass_time = end_time - create_time
            else:
                pass_time = time.time() - start_time

            print('\nCalculation time: %s' % Progress.time_definition(pass_time))
            print('reCAPTCHA solved. Price: $%s' % cost)

        if save_cost:
            if cost != 0 and isinstance(cost, (int, float)):
                read_record = File.read_records_to_list(txt_file=cost_file, file_not_found_error=False, exit_all=False)
                try:
                    balance = float(read_record[0])
                except:
                    balance = 0
                balance += cost
                File.save_records_list(txt_file=cost_file, records_list=[balance], overwrite=True, exit_all=False)

        # ADD SOLUTION TO THE PAGE.
        try:
            browser.execute_script('document.getElementById("g-recaptcha-response").innerHTML = "%s"' % key)
        except:
            pass

    return exist_captcha
            print(str(count) + ') ' + follower)
        """

        txt_file_followers = "insta-you-to-not-follow-back_%s.txt" % control_user
        File.save_records_list(txt_file=txt_file_followers,
                               records_list=followers_list,
                               overwrite=True,
                               exit_all=False)

        txt_file_followings = "insta-not-folow-back-to-you_%s.txt" % control_user
        File.save_records_list(txt_file=txt_file_followings,
                               records_list=followings_list_final,
                               overwrite=True,
                               exit_all=False)

        Progress.sound_notify_times(3)

        message = " %s people found who doesn't follow you back.\n" \
                  " %s people found who you don't follow back.\n" \
                  "NOTE: Besides, please check txt files in the folder of program.\n" \
                  " - %s\n" \
                  " - %s" % (len(followings_list_final), len(followers_list), txt_file_followers, txt_file_followings)

        Progress.exit_app(message=message, exit_all=True)
    except Exception as e:
        try:
            browser.quit()
        except:
            pass
        message = 'An error occurred. Will start again.'
        Progress.exit_app(e=e, message=message, exit_all=False)