Example #1
0
def check_run(program_code, program='', reload_time=30, sound_error=True):
    # This def checks API and get the value of 'program_code'.
    # if program_code is True, def returns
    # else, def stucks untill program_code comes as True.

    message_pasted = False
    length_of_last_message = 0
    length_of_last_message_MAX = 0
    while True:
        try:
            run = connect_api(
                code=program_code, program=program
            )  # Mostly returns True or False Boolean up to what you set on API
            if run != True:  # run only if "run" is True.
                run = None
        except Exception as e:
            run = None

        if run:
            if message_pasted:
                # If there was "False calistir" and 'count down' printed,
                # paste space as much as pasted text before.
                # Because after pyinstaller execution, print flush prints in bad view.
                message = 'Program is working now.'
                length_of_new_message = len(message)
                if length_of_last_message - length_of_new_message > 0:
                    message = message + ' ' * (length_of_last_message_MAX -
                                               length_of_new_message)
                print("\r%s" % message, flush=True, end="")
                print()
                print('-' * 40)
                print()
            # Continue to run main program (RETURN DEF)
            break
        else:
            if not message_pasted:
                print()
                print('-' * 10)
                print()
                if sound_error:
                    Progress.sound_notify()
            message_pasted = True

            now = time.time()
            time.sleep(0.01)
            message = ''
            while time.time() - now < reload_time:
                remaining_time = int(reload_time - (time.time() - now))
                net_time_string = Progress.time_definition(remaining_time)
                message = '--> An error occurred while running program. Trying again in %s.' % net_time_string
                print("\r%s" % message, flush=True, end="")
                time.sleep(1)
            length_of_last_message = len(message)
            if length_of_last_message_MAX < length_of_last_message:
                length_of_last_message_MAX = length_of_last_message
Example #2
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
Example #3
0
def internet_connection(timeout=4,
                        reload_time=30,
                        wait_for_network=True,
                        sound_error=True):
    user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'
    header = {"User-Agent": user_agent}

    url_list = [
        # 'https://api.myip.com/',
        # 'https://api.ipify.org/',
        'https://www.yahoo.com/',
        'https://www.bing.com/',
        'https://www.google.com/',
        'https://www.amazon.com/',
        'https://www.amazon.com.tr/',
        'https://www.microsoft.com/',
        'https://www.apple.com/',
    ]

    message_pasted = False
    length_of_last_message = 0
    length_of_last_message_MAX = 0
    while True:
        try:
            url = random.choice(url_list)

            network = True
            response = requests.get(url, timeout=timeout, headers=header)

            if response.status_code != 200:
                raise Exception
        except:
            # There is no internet connection.
            network = False

        if not wait_for_network and network:
            # if there is internet connection OR def overwritten to not wait for network, RETURN
            return network

        if network:
            if message_pasted:
                # If there was "False network" and 'count down' printed,
                # paste space as much as pasted text before.
                # Because after pyinstaller execution, print flush, prints in bad view.
                message = 'Connection established.'
                length_of_new_message = len(message)
                if length_of_last_message - length_of_new_message > 0:
                    message = message + ' ' * (length_of_last_message_MAX -
                                               length_of_new_message)
                print("\r%s" % message, flush=True, end="")
                print()
                print()
                print('-' * 40)
                print()
            # Continue to run main program (RETURN DEF)
            break
        else:
            if not message_pasted:
                print()
                print('-' * 10)
                print()
                if sound_error:
                    Progress.sound_notify()
                message_pasted = True
                print(url)  # DEBUG

            now = time.time()
            time.sleep(0.01)
            message = ''
            while time.time() - now < reload_time:
                remaining_time = int(reload_time - (time.time() - now))
                net_time_string = Progress.time_definition(remaining_time)
                message = '--> Error on internet connection. Trying again in %s.' % net_time_string
                print("\r%s" % message, flush=True, end="")
                time.sleep(1)
            length_of_last_message = len(message)
            if length_of_last_message_MAX < length_of_last_message:
                length_of_last_message_MAX = length_of_last_message
Example #4
0
def connect_api(https=True,
                domain=None,
                endpoint='api/external_program/',
                code='all',
                program='',
                inform_user_periodically=False,
                show_error=False,
                sound_error=False,
                exit_all=False,
                extra_data={}):
    if not domain:
        domain = os.getenv("domain")

    start = time.time()
    time.sleep(0.01)
    x = 0
    db_settings_dict = {}
    if https:
        url_first = 'https'
    else:
        url_first = 'http'

    user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'

    while True:
        try:
            x += 1
            url = '%s://%s/%s' % (url_first, domain, endpoint)

            # Define, if needed (User-Agent, Accept, Referer etc.)
            headers = {
                "User-Agent": user_agent,
                # 'accept': '*/*',
                # 'accept-encoding': 'gzip, deflate, br',
                # 'accept-language': 'en-US,en;q=0.9,tr;q=0.8,pl;q=0.7',
            }
            if endpoint == 'api/external_program/':
                data = {
                    'key': code,
                    'program': program,
                }
                data.update(extra_data)
            else:
                data = {}

            response = requests.request("GET",
                                        url,
                                        headers=headers,
                                        data=data,
                                        timeout=10)
            response.encoding = 'UTF-8'
            response = response.json()

            # My API returns a dictionary which have 'ayar' and 'parametre' in keys.
            if code == 'all':
                if endpoint == 'api/external_program/':
                    for setting in response:
                        parameter = setting['parametre']
                        parameter = String.from_string_to_type(
                            parameter.lower, 'try_all')

                        db_settings_dict[setting['ayar']] = parameter
                else:
                    db_settings_dict = response

                return db_settings_dict
            else:
                if len(response) and response:
                    response = String.from_string_to_type(response, 'try_all')

                return response

        except Exception as e:
            if inform_user_periodically:
                if x % 2 == 0:
                    message = '\nAn error occurred while running, trying again...'
                    print()
                    print('-' * 40)
                    print(message)
                    print()
            if x >= 3:
                end = time.time()
                passed_time = end - start
                message = 'An error occurred while running program. Please try again.\n' \
                        '(Trying time: %s)' % Progress.time_definition(passed_time)
                if sound_error:
                    Progress.sound_notify()
                if show_error:
                    Progress.exit_app(message=message, exit_all=exit_all)
                    print()
                else:
                    if exit_all:
                        Progress.exit_app(exit_all=exit_all)
                break