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
def turn_off_all_alerts(browser, accept=True, show_error=False, sound_for_error=False, exit_all=False): # options.add_argument("--disable-popup-blocking") # This is argument for selenium browser to block everything try: alert = browser.switch_to.alert if accept: alert.accept() else: alert.dismiss() except Exception as e: if sound_for_error: Progress.sound_notify() if show_error: Progress.exit_app(e=e, exit_all=exit_all)
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
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
def send_email(message, subject, recipients, attach_file_name=None, attach_file_text=None, login_mail=None, pwd=None, sender='Email Sender', sound_error=True, show_error=True, exit_all=False, debug_mode=0): if not login_mail: login_mail = os.getenv('login_mail') if not pwd: pwd = os.getenv('pwd') try: msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = sender recipient = ", ".join(recipients) msg['To'] = recipient msg.attach(MIMEText(message)) if attach_file_name and attach_file_text: f = StringIO() # write some content to 'f' f.write(attach_file_text) f.seek(0) attach = MIMEBase('application', "octet-stream") attach.set_payload(f.read()) encoders.encode_base64(attach) attach.add_header('Content-Disposition', 'attachment', filename=attach_file_name) msg.attach(attach) server = smtplib.SMTP('smtp.gmail.com', 587) server.set_debuglevel(debug_mode) # Prints all process if debug == 1 server.ehlo() server.starttls() server.ehlo() server.login(login_mail, pwd) # Send the email server.sendmail(sender, recipients, msg.as_string()) server.close() return True except Exception as e: if sound_error: Progress.sound_notify() if show_error: message = '--> An error occurred while sending email.' Progress.exit_app(e=e, message=message, exit_all=exit_all) else: Progress.exit_app(e=e, exit_all=exit_all) return False