-
Notifications
You must be signed in to change notification settings - Fork 2
/
jarvis.py
executable file
·130 lines (119 loc) · 3.69 KB
/
jarvis.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
from gtts import gTTS #for text to speech
import os
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import vlc
from vlc import Instance
from random import shuffle
import time
language = 'en'
def speak(audio):
myobj = gTTS(text=audio, lang=language, slow=False)
myobj.save("welcome.mp3")
os.system("mpg123 welcome.mp3")
def wishme():
hour = int(datetime.datetime.now().hour)
wishmsg =""
if hour>=0 and hour<12:
wishmsg = "Good Morning Sir, "
elif hour>=12 and hour<18:
wishmsg = "Good Afternoon Sir, "
else:
wishmsg = "Good Evening Sir "
wishmsg = wishmsg +"I am Jarvis How may I help you!"
speak(wishmsg)
def takeCommand():
'''
It takes microphone voice as an input and returns string as an Output
'''
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recongnizing....")
query = r.recognize_google(audio, language='en-in')
print('User said: ', query)
except Exception as e:
print(e)
print("Say it again please...")
speak("Say it again please...")
return "None"
return query
# this class is used for playing song
music_dir = '/home/adarsh/Adi/music'
class testVLC:
def __init__(self):
self.list1 = playlist
self.Player = Instance('--loop')
def addPlaylist(self):
self.mediaList = self.Player.media_list_new()
for music in self.list1:
self.mediaList.add_media(self.Player.media_new(music_dir+'/'+music))
self.listPlayer = self.Player.media_list_player_new()
self.listPlayer.set_media_list(self.mediaList)
def playPlaylist(self):
self.listPlayer.play()
def nextPlay(self):
self.listPlayer.next()
def pausePlaylist(self):
self.listPlayer.pause()
def stopPlaylist(self):
self.listPlayer.stop()
playlist = os.listdir(music_dir)
shuffle(playlist)
test = testVLC()
test.addPlaylist()
if __name__ == "__main__":
wishme()
while True:
query = takeCommand().lower()
#logic for executing task based on query
if 'wikipedia' in query:
speak('Searching Wikipedia..')
query = query.replace("wikipedia", "")
results = "According to wikipedia " + wikipedia.summary(query, sentences=2)
print(results)
speak(results)
elif 'youtube' in query:
if 'open' in query:
webbrowser.open("https://youtube.com")
else:
query = query.replace("youtube", "")
query = query.replace("search", "")
query = query.replace(" ", "+")
print("search: ", query)
webbrowser.open("https://www.youtube.com/results?search_query="+query)
elif 'google' in query:
if 'open' in query:
webbrowser.open("https://google.com")
else:
query = query.replace("google", "")
query = query.replace("search", "")
query = query.replace(" ", "+")
webbrowser.open("https://google.com/search?q="+query)
elif ('quit' in query) or ('bye' in query):
speak('Ok bye sir')
break
elif ('music' in query) or ('song' in query):
if 'play' in query:
test.playPlaylist()
elif 'pause' in query:
test.pausePlaylist()
elif 'stop' in query:
test.stopPlaylist()
elif 'next' in query:
test.nextPlay()
elif 'resume' in query:
test.pausePlaylist()
elif 'time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
print(strTime)
speak("Sir, the time is "+strTime)
elif 'open code' in query:
os.system("code")
elif 'hello' in query:
speak("Hello, Sir!")