-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesearch.py
143 lines (117 loc) · 4.92 KB
/
filesearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import speech_recognition as sr
import pyttsx3
import os
import subprocess
import sys
# Initialize recognizer and text-to-speech engine
recognizer = sr.Recognizer()
tts_engine = pyttsx3.init()
# Paths to search in
paths = {
"desktop": os.path.join(os.path.expanduser("~"), "Desktop"),
"documents": os.path.join(os.path.expanduser("~"), "Documents"),
"music": os.path.join(os.path.expanduser("~"), "Music"),
"videos": os.path.join(os.path.expanduser("~"), "Videos"),
"downloads": os.path.join(os.path.expanduser("~"), "Downloads"),
"pictures": os.path.join(os.path.expanduser("~"), "Pictures"),
'videos': os.path.join(os.path.expanduser('~'), 'Videos'),
'favorites': os.path.join(os.path.expanduser('~'), 'Favorites'),
'appdata': os.path.join(os.environ['APPDATA']),
'local_appdata': os.path.join(os.environ['LOCALAPPDATA']),
'program_files': os.path.join(os.environ['PROGRAMFILES']),
'program_files_x86': os.path.join(os.environ['PROGRAMFILES(X86)']),
'start_menu': os.path.join(os.environ['APPDATA'], 'Microsoft', 'Windows', 'Start Menu'),
'startup': os.path.join(os.environ['APPDATA'], 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup'),
'system32': os.path.join(os.environ['WINDIR'], 'System32'),
'windows': os.environ['WINDIR'],
'new volume d': 'D:\\'
}
def speak(text):
tts_engine.say(text)
tts_engine.runAndWait()
def listen_command():
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
print("listening")
try:
command = recognizer.recognize_google(audio).lower()
print(f"Command received: {command}")
return command
except sr.UnknownValueError:
speak("Sorry, I did not understand that.")
except sr.RequestError:
speak("Could not request results from the speech recognition service.")
return ""
def search_file(directory, filename):
for root, _, files in os.walk(directory):
for file in files:
if filename.lower() in file.lower():
return os.path.join(root, file)
return None
def searchfile():
location_prompt = "Where do you want to search? file "
speak(location_prompt)
location = listen_command()
if location in paths:
directory = paths[location]
file_prompt = f"What is the name of the file you are looking for in {location}?"
speak(file_prompt)
filename = listen_command()
if filename:
speak(f"Searching for {filename} in {location}. Please wait.")
found_file = search_file(directory, filename)
if found_file:
speak(f"Opening {filename}")
if os.name == 'nt': # Windows
os.startfile(found_file)
else:
speak("File not found.")
else:
speak("No file name provided.")
else:
speak("Invalid location specified.")
def search_folder(directory, foldername):
for root, dirs, _ in os.walk(directory):
for dir in dirs:
if foldername.lower() in dir.lower():
return os.path.join(root, dir)
return None
def searchfoler():
while True:
command = "search folder"
if "search folder" in command:
location_prompt = "Where do you want to search? "
location = listen_command()
if location in paths:
directory = paths[location]
elif os.path.exists(location):
directory = location
else:
speak("Invalid location specified. Please try again.")
continue
folder_prompt = f"What is the name of the folder you are looking for in {location}?"
speak(folder_prompt)
foldername = listen_command()
if foldername:
speak(
f"Searching for {foldername} in {location}. Please wait.")
found_folder = search_folder(directory, foldername)
if found_folder:
speak(f"Opening {foldername}")
if os.name == 'nt': # Windows
os.startfile(found_folder)
elif os.name == 'posix': # macOS or Linux
subprocess.call(('open', found_folder) if sys.platform == 'darwin' else (
'xdg-open', found_folder))
else:
speak("Folder not found.")
else:
speak("No folder name provided.")
else:
speak("No search command detected. Please say 'search folder' followed by the location and folder name.")
speak("Do you want to search for another folder? Say 'yes' to continue or 'no' to exit.")
retry_command = listen_command()
if 'no' in retry_command:
speak("okay SiR!")
break