-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdesktop_assistant.py
264 lines (213 loc) · 9.05 KB
/
desktop_assistant.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
from PyDictionary import PyDictionary
import random
import plyer as pl
from youtubesearchpython import VideosSearch
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishMe(start):
hour = int(datetime.datetime.now().hour)
if start == True:
if hour>=0 and hour<12:
speak("Good Morning!")
elif hour>=12 and hour<18:
speak("Good Afternoon!")
else:
speak("Good Evening!")
speak("I am David Sir. Good to see you again.")
else:
speak('Goodbye sir')
if hour>=0 and hour<12:
speak("Have a good day!")
elif hour>=18 and hour<24:
speak("Have a good night!")
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"User said: {query}\n")
except Exception as e:
print("Say that again please...")
return "None"
return query
def sendEmail(to, content):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('email_id', 'password')
server.sendmail('emil_id', to, content)
server.close()
def waterReminder():
M = int(datetime.datetime.now().minute)
if M == 0 or M == 30:
pl.notification.notify(
title= 'Break Reminder.',
message= 'Hey, take a break and drink some water.',
timeout=60
)
speak("Hello sir, time to take a short break")
def setReminder(reminders):
speak("At what time do you want me to remind you?")
reminder_time = takeCommand().lower()
speak('What do you want me to remind you of?')
content = takeCommand()
try:
reminder_time = reminder_time.replace(':', ' ').split()
reminder_time[0], reminder_time[1] = int(reminder_time[0]), int(reminder_time[1])
if ('p.m.' in reminder_time) and reminder_time[0]<12:
reminder_time[0] += 12
elif 'a.m.' in reminder_time and reminder_time[0]==12:
reminder_time[0] = 0
reminder_time[2] = content
reminders.append(reminder_time)
speak('Reminder was successfully set')
except:
speak('Could not set the reminder')
def checkReminder(reminders):
H = int(datetime.datetime.now().hour)
M = int(datetime.datetime.now().minute)
for timestamp in reminders:
if H == timestamp[0] and M == timestamp[1]:
pl.notification.notify(
title= 'Reminder from David',
message= timestamp[2],
timeout= 60,
app_icon = 'david.ico'
)
speak(f'Sir, here is a reminder to {timestamp[2]}')
reminders.remove(timestamp)
def wordForTheDay():
while True:
f = open('words.txt', 'r')
wordlist = f.readlines()
f.close()
word = wordlist[random.randint(0, 10000)]
meanings = PyDictionary.meaning(word)
try:
means = '\n'.join(value[0] for value in meanings.values())
break
except:
pass
pl.notification.notify(
title = 'Word for the Day',
message = f'{word.capitalize()} {means}',
timeout = 60,
app_icon = 'david.ico'
)
def Tasks(query):
if 'search in browser' in query:
try:
query = query.replace('search in browser', '')
webbrowser.open(f'https://www.google.com/search?q={query}')
except:
speak('Could not perform the task. Please try again.')
elif 'search in youtube' in query:
try:
query = query.replace('search in youtube', '')
webbrowser.open(f'https://www.youtube.com/results?search_query={query}')
except:
speak('Could not perform the task. Please try again.')
if 'search in wikipedia' in query:
try:
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2)
speak("According to Wikipedia")
print(results)
speak(results)
except:
speak('Could not perform the task. Please try again.')
elif 'open youtube' in query:
try:
webbrowser.open("youtube.com")
except:
speak('Could not perform the task. Please try again.')
elif 'play in youtube' in query:
try:
query = query.replace('play in youtube', '')
videosSearch = VideosSearch(query, limit = 1)
url = videosSearch.result()['result'][0]['link']
webbrowser.open(url)
except:
speak('Could not perform the task. Please try again.')
elif 'open google' in query:
try:
webbrowser.open("google.com")
except:
speak('Could not perform the task. Please try again.')
elif 'open stackoverflow' in query:
try:
webbrowser.open("stackoverflow.com")
except:
speak('Could not perform the task. Please try again.')
elif 'open v i e r p' in query:
try:
webbrowser.open("learner.vierp.in/home")
except:
speak('Could not perform the task. Please try again.')
elif 'play music' in query:
music_dir = 'D:\\Music\\timepass'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))
elif 'what is the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir, the time is {strTime}")
elif 'open vs' in query:
codePath = "C:\\Users\\omkar\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
os.startfile(codePath)
elif 'open whatsapp' in query:
codePath = "C:\\Users\\omkar\\AppData\\Local\\WhatsApp\\WhatsApp.exe"
os.startfile(codePath)
elif 'email to xyz' in query:
try:
speak("What should I say?")
content = takeCommand()
to = "xyz@gmail.com"
sendEmail(to, content)
speak("Email has been sent!")
except Exception as e:
print(e)
speak("Sorry, could not send the email.")
if __name__ == "__main__":
wishMe(True)
wordForTheDay()
reminders = []
while True:
# if 1:
query = takeCommand().lower()
# Logic for executing tasks based on query
if query == 'hey david':
speak('hello sir! How can I help you?')
while True:
query = takeCommand().lower()
if 'set reminder' in query or 'set a reminder' in query:
setReminder(reminders)
Tasks(query)
if 'introduce yourself' == query:
speak('''Hello! I am DAVID. I am a desktop assistance created by Omkar Kulkarni. I can perform various tasks like opening an app, setting reminder, open websites in browser, search in wikipedia, search a video in youtube, search a query in google and so on. Before telling me task you have to say, 'hey david' to enter task-mode, and after completion you have to say, 'thank you david', to exit task mode. In the same task mode you can ask me to play music or even to play a video on youtube and I will do it immediately. I can even send emails to specified clients. I can tell you the current time whenever you ask me in task-mode. I have some in built features like water reminder that will remind you to take a short break every 30 mins and word for the day feature that will show you a random english word with its meaning. To exit from the program say "goodbye david". Thank you! Special thanks to the developers of the modules and packages that were used in my creation.''')
elif 'thank you david' in query:
speak('You are welcome sir!')
break
waterReminder()
checkReminder(reminders)
waterReminder()
checkReminder(reminders)
if 'goodbye david' in query:
wishMe(False)
exit(0)