def toggle_audio_device(command, obj_manager): if OS == "Windows": # if toggle audio device is detected, then toggle audio device. if ("audio device" in command) | ("use headset" in command) | ( "use speaker" in command) | ("you speaker" in command) | ( "you headset" in command) | ("play music loud" in command): tts("toggling audio device", obj_manager) # os.system("C:\\Users\\Kallah\\Dropbox\\projects\\batch\\run_file_from_exe.exe") if ("speaker" in command) | ("loud" in command): os.system('nircmd setdefaultsounddevice "DSP" 1') os.system('nircmd setdefaultsounddevice "DSP" 2') elif ("headset" in command) | ("headphones" in command) | ( "earphones" in command): os.system('nircmd setdefaultsounddevice "China" 1') os.system('nircmd setdefaultsounddevice "China" 2') else: os.system( "C:\\Users\\Kallah\\Dropbox\\projects\\batch\\run_file_from_exe.exe" ) return True else: if ("audio device" in command) | ("use headset" in command) | ( "use speaker" in command) | ("you speaker" in command) | ( "you headset" in command) | ("play music loud" in command): thread_print( "Toggling audio devices is not yet avaliable on {}".format(OS), obj_manager)
def tts(text, obj_manager): """ Text to speech, input text outputs speech :param text: a string that will be read up :return: Null """ # if its not run from a test if obj_manager["tests"][0] == False: file_name_win = "audio\\tts.mp3" file_folder_lin = "audio" file_name_lin = "tts" file_ext_lin = "mp3" # this should always be mp3 # create a text to speech object with language English using Google API text_to_speech = gTTS(text=text, lang='en', slow=False) # save the object to an mp3 file # play the file out loud if OS == "Windows": text_to_speech.save(file_name_win) thread_print(text, obj_manager) playsound(file_name_win, block=True) os.remove(file_name_win) else: # text_to_speech.save(file_name_lin) file = "%s.{}".format(file_ext_lin) % os.path.join( file_folder_lin, file_name_lin) text_to_speech.save(file) playsound(file, block=True) os.remove(file) else: obj_manager["tests"].append(text)
def start_a_program_win(keys, command, obj_manager): # big bertha can you open notepad # starts a program from the programs folder using its name # the triggers that enables the command to fire return_value = False triggers = ["open", "start"] if "music" in command: triggers.remove("start") for key in keys: for trigger in triggers: if trigger in command: # remove the key from the command command = command.replace("{} ".format(key), "") # remove the trigger from the command and anything in front of the trigger command = command[command.find(trigger) + len(trigger) + 1:] # remove anything that occurs after the first word index = command.find(" ") if index != -1: command = command[:index] # tries to start the program try: os.startfile(r"programs\\{}.lnk".format(command)) # if the program starts, the TTS command fires tts("Starting {}".format(command), obj_manager) except FileNotFoundError: # if the program is not recognized, print to user thread_print( "{} is not recognized as a program".format(command), obj_manager) return_value = True return return_value
def start_a_program_lin(keys, command, obj_manager, error_mode=0): # big bertha can you open notepad # starts a program from the programs folder using its name # the triggers that enables the command to fire return_value = False triggers = ["open", "start"] if "music" in command: triggers.remove("start") for key in keys: for trigger in triggers: if trigger in command: # remove the key from the command command = command.replace("{} ".format(key), "") # remove the trigger from the command and anything in front of the trigger command = command[command.find(trigger) + len(trigger) + 1:] # remove anything that occurs after the first word index = command.find(" ") if index != -1: command = command[:index] # checks for known programs in a list of keywords command, tts_command = find_command_in_alias_list(command) # tries to start the program # >/dev/null 2>&1" error_mode = 1 if error_mode == 0: # voids output of command sh_command = "{} >/dev/null 2>&1 &".format(command) else: # runs the command while displaying output, useful for testing sh_command = "{} &".format(command) if os.system(sh_command) != 32512: # if the program starts, the TTS command fires if tts_command != None: tts("Starting {}".format(tts_command), obj_manager) else: tts("Starting {}".format(command), obj_manager) else: # if the program is not recognized, print to user thread_print( "{} is not recognized as a program".format(command), obj_manager) return_value = True return return_value
def _test_thread_print(self): """ Tests printing is working """ # without threads from libs.helper import create_obj_manager, thread_print obj_manager = create_obj_manager(avoid_threads=True, is_test=True) text = "something" thread_print(text, obj_manager) self.assertEqual(obj_manager["test_prints"][0], text) # with threads obj_manager = create_obj_manager(avoid_threads=False, is_test=True) text = "something else" thread_print(text, obj_manager) self.assertEqual(obj_manager["test_prints"][0], text)
from run_commands import * from libs.helper import create_obj_manager, thread_print keys = ["big bertha", "big birthday", "big brother", "big births", "big birth"] commands = [] # obj_manager = create_obj_manager() obj_manager = create_obj_manager(avoid_threads=True) thread_print("Please enter a command: ", obj_manager) for c in commands: exec_commands("{} {}".format(keys[0], c), obj_manager) while True: exec_commands("{} {}".format(keys[0], input()), obj_manager)
def set_a_timer(command, obj_manager): if ("set " in command) & ("timer" in command): # extract time from sentence if command.find("set a ") != -1: command = command[command.find("set a ") + len("set a "):command.find("timer")] else: command = command[command.find("set ") + len("set "):command.find("timer")] # streamline plural to singular command = command.replace("seconds", "second").replace( "minutes", "minute").replace("hours", "hour") command = command.replace("weeks", "week").replace("months", "month").replace( "years", "year") # convert textual numbers from two hundred and three to two-hundred-three to make one number one word command = command.replace("-", " ").replace(" and", "").replace(",", "") command = command.replace(" second", ".second.").replace( " minute", ".minute.").replace(" hour", ".hour.") command = command.replace(" week", ".week.").replace( " month", ".month.").replace(" year", ".year.") command = command.replace(" ", "-").replace(".", " ").replace(" -", " ") # convert any textual numbers to ints output = "" for word in command.split(" "): try: output += "{} ".format(w2n.word_to_num(word)) except ValueError: output += "{} ".format(word) command = output # concatenate the numerical values to its time unit I.E 1 year 2 month -> 1year 2month output = "" last = "" for i in command: try: int(last) if i != " ": output += i last = i except ValueError: output += i last = i command = output # Convert from time units in words to seconds unit_reg = pint.UnitRegistry() # set init time to 0s duration = unit_reg("0seconds") for word in command.split(" "): try: duration += unit_reg(word) except pint.errors.DimensionalityError: None timer = int(duration.magnitude) str_time = str(datetime.timedelta(seconds=timer)) # convert from 2:12:01 to 2 hours 12 minutes 1 second # and 0:00:04 to 04 seconds output_time = "" h = False m = False for i in range(len(str_time)): if (h == False) & (str_time[i] == ":"): if str_time[i - 1] == "1": output_time += " hour " else: output_time += " hours " h = True elif (m == False) & (str_time[i] == ":"): if str_time[i - 1] == "1": output_time += " minute " else: output_time += " minutes " m = True else: output_time += str_time[i] if str_time[-1] == "1": output_time += " second " else: output_time += " seconds " # remove empty values such as 0 minutes output_time = output_time.replace("0 hours ", "").replace( "0 hour ", "").replace("00 minutes ", "").replace("00 minute ", "") # remove any zeroes in the first position, IE 02 seconds -> 2 seconds while output_time[0] == "0": output_time = output_time[1:] tts("setting a {}timer".format(output_time), obj_manager) for i in range(0, timer): thread_print( "time remaning: {}s".format( str(datetime.timedelta(seconds=timer - i))), obj_manager) time.sleep(1) thread_print( "time remaning: {}s".format(str(datetime.timedelta(seconds=0))), obj_manager) thread_print( "BEEP BEEP BEEP BEEP, type something to turn the alarm off:", obj_manager) # do not sound the alarm if it is a test if not obj_manager["tests"][0]: get_user_input_in_thread(alarm) return True