-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarvis.py
More file actions
213 lines (177 loc) · 7.83 KB
/
starvis.py
File metadata and controls
213 lines (177 loc) · 7.83 KB
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
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
import requests # This module in Python is a popular library used for making HTTP requests to communicate with web servers. It simplifies sending and receiving data over the internet, such as retrieving data from APIs, submitting forms, or downloading files.
import yfinance as yfp
from pytube import Search # pytube module is generally used to download yt videos whereas importing Search class will help in searching for youtube results.
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# engine.setProperty('voice', voices[0].id)
def speak(audio):
engine.setProperty('voice', voices[1].id)
engine.say(audio)
engine.runAndWait()
engine.setProperty('rate', 170) # 150-200, ideal for clarity
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
speak("Good Morning!")
elif hour>=12 and hour<18:
speak("Good Afternoon!")
else:
speak("Good Evening!")
speak("Sir, This is Starvis, how may I assist you?")
def takeCommand():
r = sr.Recognizer() #Creates a new Recognizer instance, which represents a collection of speech recognition functionality.
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1.5 # means if there is a gap of 1.5sec in the input audio. The sentence will be called as completed and break the listening
audio = r.listen(source) # sirf sunta hai
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-us') # ye suna hua baat samajhta hai(process krta hai)
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('name@gmail.com', 'password')
server.sendmail('name@gmail.com', to, content)
server.close()
def getStockPrice(stock_symbol):
stock = yf.Ticker(stock_symbol)
stock_info = stock.info
current_price = stock_info.get('currentPrice', 'N/A')
return f"The current price of {stock_symbol} is {current_price}"
def showYouTubeResults(song_name):
search = Search(song_name)
results = search.results
if results:
video_url = results[0].watch_url
webbrowser.open(video_url)
speak(f"Showing results for {song_name} on YouTube")
else:
speak("Song not found on YouTube")
def getWeather(city):
api_key = "2a1b67d9c312d584b7b0bb2ab988d956"
base_url = "http://api.openweathermap.org/data/2.5/weather?"
complete_url = base_url + "q=" + city + "&appid=" + api_key #very important line (location specific)
response = requests.get(complete_url)
data = response.json()
if data["cod"] != "404":
main = data.get("main", {})
weather = data.get("weather", [{}])[0]
temperature = round(main.get("temp", "N/A") -273.15, 2)
pressure = main.get("pressure", "N/A")
humidity = main.get("humidity", "N/A")
description = weather.get("description", "N/A")
weather_report = f"Temperature: {temperature}°C\nPressure: {pressure}hPa\nHumidity: {humidity}%\nDescription: {description}"
return weather_report
else:
return "City not found."
if __name__ == "__main__":
wishMe()
while True:
query = takeCommand().lower()
if 'wikipedia' in query:
speak("what do you want to search?")
query = takeCommand().lower()
print(f"Search term: {query}") # Debugging statement
speak('Searching Wikipedia...')
if query:
try:
results = wikipedia.summary(query, sentences=2)
speak("According to Wikipedia")
print(results)
speak(results)
except wikipedia.exceptions.DisambiguationError as e:
speak("There are multiple results for your query, please be more specific.")
print(e.options)
except wikipedia.exceptions.PageError:
speak("The page does not exist.")
except wikipedia.exceptions.WikipediaException as e:
speak("An error occurred while searching Wikipedia.")
print(e)
else:
speak("Please provide a search term.")
elif 'open youtube' in query:
webbrowser.open("youtube.com")
elif 'open google' in query:
webbrowser.open("google.com")
elif 'help' in query or 'what can you do' in query:
help_text = """
I can do the following:
- Search Wikipedia
- Open websites like YouTube, Google, Instagram
- Search YouTube videos
- Tell time and weather
- Send emails
- Get stock prices
- And more!
"""
speak(help_text)
print(help_text)
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 'search google' in query or 'search on google' in query:
if query == 'search google' or 'search on google':
speak("what do you want to search?")
search_term = takeCommand().lower()
webbrowser.open(f"https://www.google.com/search?q={search_term}")
speak(f"Searching Google for {search_term}")
elif 'for' in query:
search_term = query.replace("search google for", "").strip()
webbrowser.open(f"https://www.google.com/search?q={search_term}")
speak(f"Searching Google for {search_term}")
elif ('open'or'play') and 'youtube' and 'video' in query:
speak("Which YouTube video?")
song_name = takeCommand().lower()
showYouTubeResults(song_name)
elif 'what is the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir, the time is {strTime}")
elif 'weather' in query:
speak('Which city?')
city = takeCommand().lower()
weather_report = getWeather(city)
speak(weather_report)
print(weather_report)
elif 'open code' in query:
codePath = os.path.join(os.environ['LOCALAPPDATA'], "Programs", "Microsoft VS Code", "Code.exe")
os.startfile(codePath)
elif 'send email' in query:
try:
speak("To whom should I send the email?")
to = takeCommand().lower()
speak("What should I say?")
content = takeCommand()
sendEmail(to, content)
speak("Email has been sent!")
except Exception as e:
print(e)
speak("Sorry SIR. I am not able to send this email")
elif 'stock price' in query:
speak("Which stock?")
stock_symbol = takeCommand().upper()
stock_price = getStockPrice(stock_symbol)
speak(stock_price)
print(stock_price)
elif any(keyword in query for keyword in ["end", "stop", "break", "terminate"]):
print("User ended the program")
speak("user terminated the program")
break
else:
print("No query matched")
# break