-
Notifications
You must be signed in to change notification settings - Fork 1
/
pixy.py
329 lines (276 loc) · 10.2 KB
/
pixy.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
'''
Author : Harsh Vishwakarma
Project : Virtual Assistant (Jarvis)
Version : 0.4.0
Desciption : This is version 0.1.0 and it can Perform some basic tasks like
doesn't run not connected to internet,
says command before every task,
search in wikipedia,
open youtube, google, stackoverflow, hackerrank, facebook, instagram
plays song which was asked,
plays random music,
tell about time,
open sublime, visual code studio,
close itself on command
take screenshot on telling
notify a new word after every 30 min
'''
import pyttsx3 # pip install pyttsx3
import datetime # built-in module
import speech_recognition as sr # pip install speechRecognition
import wikipedia # pip install wikipedia
import webbrowser # built-in module
import os # built-in module
import time # built-in module
import random # built-in module
import socket # built-in module
import pyautogui # pip install pyautogui
import english_words # pip install english_words
import PyDictionary # pip install PyDictionary
import plyer # pip install plyer
# Initializing Engine and Microsoft Speech API
engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[1].id)
# speak function
def speak(audio):
engine.say(audio)
engine.runAndWait()
# tell to both speak & print
def tell(audio):
print(audio)
speak(audio)
# To close the File
def isclose(x):
if 'close' in x or 'stop' in x or 'exit' in x:
tell("Bye Harsh")
tell("Its time to go now")
exit()
# Take command to Execute Something
def takecommand():
r = sr.Recognizer()
with sr.Microphone() as source:
tell("Listening....")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing....")
query = r.recognize_google(audio, language="en-in")
tell(f"You Said : {query}\n")
except Exception as e:
tell("Can't Recognize..")
tell("Please say that again")
takecommand()
return "None"
return query
# Checks if Device is connected to Internet or Not
def is_connected():
try:
# connect to the host -- tells us if the host is actually
# reachable
socket.create_connection(("1.1.1.1", 53))
return True
except OSError:
pass
return False
# Listen only to get voice data
def listen():
r = sr.Recognizer()
with sr.Microphone() as source:
print(".")
r.pause_threshold = 1
audio = r.listen(source)
try:
query = r.recognize_google(audio, language="en-in")
print(f"You said: {query}")
except Exception as e:
return "None"
return query
# Tasks to be Performed by Jarvis
def dotask(query):
if 'wikipedia' in query:
query = query.replace('wikipedia', '')
result = wikipedia.summary(query, sentences=2)
tell("According to Wikipedia....")
tell(result)
elif 'search' in query:
query = query.replace('search for', "")
query = query.replace('search', "")
if 'youtube' in query:
query = query.replace('in youtube', "")
query = query.replace('on youtube', "")
query = query.replace('youtube', "")
tell("Searching on YouTube...")
webbrowser.open(f"https://www.youtube.com/search?q={query}")
else:
query = query.replace('in google', "")
query = query.replace('on google', "")
query = query.replace('google', "")
tell("Searching on Google...")
webbrowser.open(f"https://www.google.com/search?q={query}")
elif 'open' in query:
query = query.replace('open', '')
if 'youtube' in query:
tell("Opening Youtube...")
webbrowser.open('youtube.com')
elif 'google' in query:
tell("Opening Google")
webbrowser.open("google.com")
elif 'facebook' in query:
tell("Opening Facebook")
webbrowser.open("facebook.com")
elif 'instagram' in query:
tell("Opening Instagram")
webbrowser.open("instagram.com")
elif 'stackoverflow' in query or 'stack overflow' in query:
tell("Opening Stack Overflow")
webbrowser.open("stackoverflow.com")
elif 'hacker rank' in query or 'hackerrank' in query:
tell("Opening Hacker Rank")
webbrowser.open("hackerrank.com")
elif 'code' in query:
tell("Opening Visual Code Studio")
os.startfile(
r'C:\Users\Lenovo\AppData\Local\Programs\Microsoft VS Code\Code.exe')
elif 'sublime' in query:
tell("Opening Sublime Text 3")
os.startfile(r"C:\Program Files\Sublime Text 3\sublime_text.exe")
elif 'blog' in query:
tell("Opening Your Blog Website")
webbrowser.open("blog.harshvishwakarma.xyz")
elif 'website' in query:
tell("Opening Your Website")
webbrowser.open("harshvishwakarma.xyz")
else:
tell(f"Can't Open {query}")
elif 'play music' in query or 'play song' in query:
query = query.replace('play music', '')
query = query.replace('play song', '')
query = query.strip()
music_dir = r"F:\song listen"
songs = os.listdir(music_dir)
# print(f"Query :{query}")
x = 0
if query == "":
randmusic = songs[random.randint(0, len(songs))]
tell(f"PLaying {randmusic}")
os.startfile(os.path.join(music_dir, randmusic))
x = 1
else:
for i in songs:
# print(f"Song : {i}")
if query in i.lower():
tell(f"Playing Song {query}")
os.startfile(os.path.join(music_dir, i))
x = 1
break
if x == 0:
tell(f"No Song name {query} in your Directory")
elif 'who are you' in query:
tell("I am Pixy")
tell("Virtual Assistant of Harsh Vishwakarma")
tell("I am on Version 0.4.0")
# For telling the time
elif 'time' in query:
time = datetime.datetime.now().strftime("%H:%M:%S")
tell(f"Time is {time}")
# For taking Screenshots
elif 'screenshot' in query:
takeScreenshot()
else:
isclose(query)
# if 'wikipedia' in query:
# query = query.replace('wikipedia', '')
# result = wikipedia.summary(query, sentences=2)
# tell("According to Wikipedia....")
# tell(result)
# elif 'open youtube' in query :
# webbrowser.open("youtube.com")
# elif 'open google' in query:
# webbrowser.open("google.com")
# elif 'open facebook' in query:
# webbrowser.open("facebook.com")
# elif 'open instagram' in query:
# webbrowser.open("instagram.com")
# elif 'open stackoverflow' in query:
# webbrowser.open("stackoverflow.com")
# elif 'open hacker rank' in query:
# webbrowser.open("hackerrank.com")
# elif 'open code' in query:
# os.startfile(r'C:\Users\Lenovo\AppData\Local\Programs\Microsoft VS Code\Code.exe')
# elif 'open sublime' in query:
# os.startfile(r"C:\Program Files\Sublime Text 3\sublime_text.exe")
# elif 'open hacker rank' in query:
# webbrowser.open("hackerrank.com")
# elif 'open hacker rank' in query:
# webbrowser.open("hackerrank.com")
# elif 'who are you' in query:
# tell("I am Jarvis")
# tell("Virtual Assistant of Harsh Vishwakarma")
# tell("I am on Version 0.2.0")
# elif 'play music' in query:
# music_dir = r"F:\song listen"
# songs = os.listdir(music_dir)
# os.startfile(os.path.join(music_dir, songs[random.randint(0,len(songs))]))
# elif 'time' in query:
# time = datetime.datetime.now().strftime("%H:%M:%S")
# tell(f"Time is {time}")
# else:
# isclose(query)
#To give Notification Every 30 min
def notifyWord():
dictionary = PyDictionary.PyDictionary()
word = list(english_words.english_words_set)[random.randint(1, 100)]
meaning = dictionary.meaning(str(word))
tell("Word is "+word)
plyer.notification.notify(
title=str(word),
message=str(meaning),
timeout=5
)
#To take Screenshot
def takeScreenshot():
#It Takes Screenshot and SAve file in YYYY-DD-MM--HH-MM-SS.jpg
tell("Taking Screenshot")
myScreenshot = pyautogui.screenshot()
filename = datetime.datetime.now().strftime("%Y-%m-%d--%H-%M-%S")
myScreenshot.save(f'screenshots\\{filename}.png')
tell("Screenshot Saved")
# To wish me Everytime when the Application start
def wishme():
hour = int(datetime.datetime.now().hour)
if hour >= 4 and hour < 12:
speak("Good Morning Harsh")
elif hour >= 12 and hour < 17:
speak("Good Afternoon Harsh")
elif hour >= 17 and hour < 20:
speak("Good Evening Harsh")
else:
speak("Heyyy Harsh")
tell("I am Pixy")
tell("Your Assistant")
tell("How may I help you")
if __name__ == '__main__':
wishme()
tell("Presenting your First Word")
notifyWord()
if(is_connected()):
dotask(takecommand().lower())
init = time.time()
while 1:
sound = listen().lower()
if 'pixie' in sound or 'sexy' in sound or 'bakrid' in sound or 'fixing' in sound:
query = takecommand().lower()
dotask(query)
elif 'screenshot' in sound:
takeScreenshot()
else:
isclose(sound)
if(int(time.time()-init) >= 10*60):
notifyWord()
init = time.time()
else:
tell("Your System is Not Connected to Internet")
tell("Hence I am Unable to Recognize your Voice")
tell("Please Connect to Internet")
isclose("exit")