-
Notifications
You must be signed in to change notification settings - Fork 1
/
jarvis_mlh.py
208 lines (175 loc) · 6.72 KB
/
jarvis_mlh.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
import pyttsx3 #pip install pyttsx3
import speech_recognition as sr #pip install SpeechRecognition
import datetime
import wikipedia #pip install wikipedia
import smtplib
import webbrowser as wb
import os
import requests
import pyautogui #pip install pyautogui
import psutil #pip install psutil
import pyjokes #pip install pyjokes #Using pyjokes is another way to say jokes. Here we have just used an api
engine=pyttsx3.init()
voices=engine.getProperty('voices')
engine.setProperty('voice',voices[0].id)
newVoiceRate=190
def speak(audio_text):
engine.say(audio_text)
engine.runAndWait()
def set_whichvoice(audio_text):
for i in range(3):
#voice=engine.getProperty('voices')
engine.setProperty('voice',voices[i].id)
speak(audio_text)
def set_VoiceRate(n):
#default is 200
newVoiceRate=n
engine.setProperty('rate',newVoiceRate)
def time():
Time=datetime.datetime.now().strftime("%H:%M:%S")
speak(Time)
def date():
Date=datetime.datetime.now().strftime("%Y-%m-%d") #We can also do this as year=int(datetime.datetime.now().year), and same with others and print them individually
speak("Current date is")
speak(Date)
def wishme():
hour=datetime.datetime.now().hour
speak("Any thing else?")
def takeCommand():
r=sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold=0.5
audio=r.listen(source)
try:
print("Recognizing...")
query=r.recognize_google(audio)
print(query)
except Exception as e:
print(e)
speak("What?")
return None
return query
def sendmail(to,content):
server=smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.login("[email protected]","123test")
server.sendmail("[email protected]",to,content)
server.close()
if __name__ == "__main__":
wishme()
while True:
take_command=takeCommand()
if take_command==None:
continue
query=take_command.lower().split()
print(query)
print(type(query))
if "time" in query:
time()
elif "date" in query:
date()
elif "wiki" in query:
wiki_search_text=""
wiki_start=0
for i in query:
wiki_start+=1
if i=="wiki":
for j in range(wiki_start,len(query)):
wiki_search_text+=query[j]
break;
speak("Searching ...")
result=wikipedia.summary(wiki_search_text,sentences=2)
speak(result)
print(query[wiki_start:])
elif "mail" in query:
try:
speak("What are the contents of the mail?")
content=take_command()
to="[email protected]"
sendmail(to,content)
speak("Khat mene daal diya")
except Exception as e:
speak("Unable to send the mail")
print(e)
elif "chrome" in query:
speak("What link do you want me to open?")
chromepath="C:/Program Files/Google/Chrome/Application/chrome.exe %s"
while(True):
take_command=takeCommand()
if take_command==None:
continue
break
search=take_command.lower()
if search[-3:]!='com':
search+=".com"
wb.get(chromepath).open_new_tab(search)
continue
elif "logout" in query:
pass
#os.system("shutdown - l")
elif "shutdown" in query:
pass
#os.system("shutdown /s /t 1")
elif "restart" in query:
pass
#os.system("shutdown /r /t 1")
elif "songs" in query:
pass
# songs_dir="F:\musics\music"
# songs=os.listdir(songs_dir)
# os.startfile(os.path.join(songs_dir,songs[0]))
elif "remember" in query or "write" in query:
speak("What do you want me to remember?")
while(True):
take_command=takeCommand()
if take_command==None:
continue
break
text_to_remember=take_command.lower()
with open("remember.txt","a+") as f:
f.write(text_to_remember)
elif "remembered" in query or "read" in query or ("open" in query and "diary" in query):
with open("remember.txt","r") as f:
speak(f.read())
elif "empty" in query or ("diary" in query and "clear" in query):
print("Clearing")
with open("remember.txt","w") as f:
print("Clear")
speak("Emptied")
continue
elif "screenshot" in query or ("capture" in query and "screen" in query):
img=pyautogui.screenshot()
img.save("ss.png")
speak("Done")
elif ("machine" in query or "cpu" in query) and ("report" in query or "status" in query):
usage=str(psutil.cpu_percent())
speak("CPU is at" + usage)
battery=psutil.sensors_battery()
speak("Battery is "+battery.percent)
elif "joke" in query or "jokes" in query:
url="https://v2.jokeapi.dev/joke/Programming?format=txt"
a=requests.get(url)
print(a.text)
print(type(a.text))
speak(a.text)
pass
elif "tata" in query:
speak("Goodnight")
quit()
elif "weather" in query:
response = requests.get("https://ipgeolocation.abstractapi.com/v1/?api_key=170186c6c1db43f1854d9690835e0778")
print(response.status_code)
city=response.json()['city']
respons=requests.get("http://api.openweathermap.org/data/2.5/weather?appid=351861fb0a871db7957069e9ae033277&q="+"Vadodara")
weather_details=respons.json()
speak("Temperature in " +city + " is " + str(weather_details["main"]["temp"])+" Kelvin, with "+str(weather_details["main"]["pressure"])+" hPascals")
elif "news" in query:
country = "in"
query_news = "idea"
response = requests.get("https://newsapi.org/v2/top-headlines?country="+ country +"&q="+ query_news +"&apiKey=4a733a4e0a2846acbe7ec32d8d1e2761")
speak(response.title)
pass
else:
speak("I didn't get you. Say that again")