-
Notifications
You must be signed in to change notification settings - Fork 0
/
assistant.py
262 lines (236 loc) · 8.41 KB
/
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
import datetime
import speech_recognition as sr
import requests
from bs4 import BeautifulSoup
import webbrowser
import pyautogui
import wikipedia
import os
from dotenv import load_dotenv
import psutil
import wolframalpha
from time import sleep
import logging
import platform
# Setup logging
logging.basicConfig(level=logging.INFO)
# Initialize text-to-speech engine
engine = pyttsx3.init()
# Load environment variables from .env file
load_dotenv()
# User-specific data placeholders
USER_NAME = os.getenv("USER_NAME", "user")
USER_EMAIL = os.getenv("USER_EMAIL", "your_email")
USER_PASSWORD = os.getenv("USER_PASSWORD", "your_password")
WOLFRAM_APP_ID = os.getenv("WOLFRAM_APP_ID", "your_wolfram_app_id")
USER_AGENT = os.getenv("USER_AGENT", "your_user_agent")
WEATHER_URL = "https://weather.com/weather/today/l/26.62,87.36?par=google&temp=c"
def speak(audio):
"""Speaks out the given audio text."""
print(audio)
engine.say(audio)
engine.runAndWait()
def click():
"""Simulates a mouse click."""
pyautogui.click()
def get_username():
"""Gets the username of the current user."""
usernames = psutil.users()
for user in usernames:
speak(f"Sir, this computer is signed in as {user.name}.")
def take_screenshot():
"""Takes a screenshot and saves it to the Desktop."""
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop", "screenshot.png")
pyautogui.screenshot(desktop_path)
speak(f"Screenshot saved to {desktop_path}")
def get_battery_status():
"""Gets the current battery status."""
battery = psutil.sensors_battery()
if battery:
battery_percentage = battery.percent
is_plugged = battery.power_plugged
speak(f"Sir, it is {battery_percentage} percent.")
if is_plugged:
speak("and It is charging.")
elif battery_percentage <= 95:
speak("Sir, plug in the charger.")
else:
speak("Could not retrieve battery status.")
def shut_down():
"""Shuts down the computer."""
speak("Initializing shutdown protocol.")
if platform.system() == "Windows":
os.system("shutdown /s /t 1")
else:
os.system("sudo shutdown now")
def restart():
"""Restarts the computer."""
speak("Restarting your computer.")
if platform.system() == "Windows":
os.system("shutdown /r /t 1")
else:
os.system("sudo shutdown -r now")
def sleep_mode():
"""Puts the computer into sleep mode."""
speak("Initializing sleep mode.")
if platform.system() == "Windows":
os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")
else:
os.system("pmset sleepnow")
def get_weather():
"""Fetches and speaks out the current weather details."""
speak("Checking the details for weather...")
headers = {"User-Agent": USER_AGENT}
try:
page = requests.get(WEATHER_URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
temperature = soup.find(class_="CurrentConditions--tempValue--3KcTQ")
description = soup.find(class_="CurrentConditions--phraseValue--2xXSr")
if temperature and description:
temp = temperature.get_text()
desc = description.get_text()
speak(f"Sir, the temperature is {temp} Celsius and it is {desc} outside.")
else:
speak("Could not retrieve weather information.")
except requests.RequestException as e:
logging.error("Weather request failed: %s", e)
speak("Failed to retrieve weather information.")
def check_messages():
"""Checks Facebook messages."""
speak("Checking for messages...")
try:
client = Client(USER_EMAIL, USER_PASSWORD, user_agent=USER_AGENT)
if client.isLoggedIn():
threads = client.fetchUnread()
if threads:
speak(f"Sir, you have {len(threads)} new message(s).")
for thread in threads:
info = client.fetchThreadInfo(thread.uid)[thread.uid]
speak(f"Message from {info.name}")
messages = client.fetchThreadMessages(thread.uid, limit=1)
for message in messages:
speak(f"Message: {message.text}")
else:
speak("Sir, you have no new messages.")
else:
speak("Failed to log in to Facebook.")
except Exception as e:
logging.error("Failed to check messages: %s", e)
speak("Failed to check messages.")
def get_time():
"""Speaks out the current time."""
current_time = datetime.datetime.now().strftime('%I:%M %p')
speak(f"Sir, the current time is {current_time}.")
def get_date():
"""Speaks out the current date."""
now = datetime.datetime.now()
speak(f"Sir, the current date is {now.strftime('%B %d, %Y')}.")
def google_search(query):
"""Performs a Google search."""
url = f"https://www.google.com/search?q={query}"
webbrowser.open(url)
speak(f"Here are the search results for {query} on Google.")
def youtube_search(query):
"""Performs a YouTube search."""
url = f"https://www.youtube.com/results?search_query={query}"
webbrowser.open(url)
speak(f"Here are the search results for {query} on YouTube.")
def calculate(expression):
"""Calculates the given expression using WolframAlpha."""
client = wolframalpha.Client(WOLFRAM_APP_ID)
try:
res = client.query(expression)
answer = next(res.results).text
speak(f"The answer is {answer}.")
except Exception as e:
logging.error("Calculation failed: %s", e)
speak("Failed to compute the answer.")
def recognize_command():
"""Recognizes voice commands."""
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
recognizer.adjust_for_ambient_noise(source, duration=1)
audio = recognizer.listen(source)
try:
query = recognizer.recognize_google(audio)
print(f"Recognized: {query}")
return query.lower()
except sr.UnknownValueError:
speak("Sorry, I did not catch that.")
except sr.RequestError as e:
logging.error("Recognition error: %s", e)
speak("Sorry, I'm having trouble understanding you.")
return ""
def handle_command(query):
"""Handles the recognized command."""
if 'jarvis' in query:
speak("Yes, Sir")
elif 'date' in query:
get_date()
elif 'time' in query:
get_time()
elif 'thank you' in query:
speak('No problem, Sir.')
elif 'google' in query:
speak('What do you want to search?')
search_query = recognize_command()
google_search(search_query)
elif 'youtube' in query:
speak('What do you want to search?')
search_query = recognize_command()
youtube_search(search_query)
elif 'facebook' in query:
webbrowser.open("https://www.facebook.com")
speak("Opening Facebook.")
elif 'gmail' in query:
webbrowser.open("https://mail.google.com/")
speak("Opening Gmail.")
elif 'maps' in query:
webbrowser.open("https://www.google.com/maps")
speak("Opening Google Maps.")
elif 'calculate' in query:
speak('Tell me the expression.')
expression = recognize_command()
calculate(expression)
elif 'weather' in query:
get_weather()
elif 'screenshot' in query:
take_screenshot()
elif 'wikipedia' in query:
search_query = query.replace("wikipedia", "")
results = wikipedia.summary(search_query, sentences=2)
speak(f"According to Wikipedia, {results}")
elif 'close window' in query:
pyautogui.hotkey('alt', 'f4')
speak('Closed the current window.')
elif 'battery' in query:
get_battery_status()
elif 'shutdown' in query:
shut_down()
elif 'restart' in query:
restart()
elif 'sleep' in query:
sleep_mode()
elif 'message' in query:
check_messages()
elif 'username' in query:
get_username()
elif 'click' in query:
click()
else:
speak("I did not understand that command.")
def greeting():
"""Gives a greeting message."""
speak('Welcome back, Sir.')
get_time()
get_date()
if __name__ == '__main__':
greeting()
get_weather()
speak("Getting battery information...")
get_battery_status()
while True:
command_query = recognize_command()
if command_query:
handle_command(command_query)