-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
179 lines (165 loc) · 7.03 KB
/
main.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
import speech_recognition as sr
import webbrowser
from gtts import gTTS
import os
import musicLibrary
import requests
import pygame
import tempfile
from googlesearch import search
# Initialize pygame mixer
pygame.mixer.init()
newsapi = "af4397d2094a4ec9bb22cc028beb84e1"
weather_api_key = "2e18e4ef351fba6f290c44d99284aa3c&units=metric"
def speak(text):
"""Converts text to speech using gTTS and plays the audio"""
try:
tts = gTTS(text=text, lang='en')
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
temp_filename = temp_file.name
tts.save(temp_filename) # Save the audio
pygame.mixer.music.load(temp_filename) # Load the audio after saving
pygame.mixer.music.play()
while pygame.mixer.music.get_busy(): # Wait until the audio finishes playing
pygame.time.Clock().tick(10) # Check every 10 milliseconds
os.remove(temp_filename) # Remove the temporary file after playing
except Exception as e:
print(f"Error in speak function: {e}")
def open_application(app_name):
"""Opens the specified application."""
app_mapping = {
"notepad": "notepad.exe",
"calculator": "calc.exe",
"paint": "mspaint.exe",
"file": "explorer.exe"
}
if app_name in app_mapping:
os.system(app_mapping[app_name])
speak(f"{app_name.capitalize()} is now opened.")
else:
speak(f"Sorry, I don't know how to open {app_name}.")
def get_news():
"""Fetches and speaks the latest news headlines"""
try:
r = requests.get(f"https://newsapi.org/v2/top-headlines?country=in&apiKey={newsapi}")
r.raise_for_status() # Raise an exception for HTTP errors
data = r.json()
articles = data.get('articles', [])
for article in articles[:5]:
speak(article['title'])
except Exception as e:
speak(f"An error occurred while fetching news: {e}")
def get_weather(city):
"""Fetches and speaks the weather for a specified city"""
try:
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={weather_api_key}"
r = requests.get(url)
r.raise_for_status() # Raise an exception for HTTP errors
weather_data = r.json()
temperature = weather_data["main"]["temp"]
speak(f"The current temperature in {city} is {temperature} degrees Celsius.")
except Exception as e:
speak(f"An error occurred while fetching weather: {e}")
def search_google(query):
"""Performs a Google search and opens the first result"""
try:
search_results = list(search(query, num=1, stop=1))
if search_results:
webbrowser.open(search_results[0])
speak(f"I have opened the page in your browser.")
else:
speak("I'm sorry, I couldn't find any results for your query.")
except Exception as e:
speak(f"An error occurred during the search: {e}")
def process_command(command):
"""Processes the given command and performs the appropriate action"""
try:
command = command.lower()
if "open google" in command:
webbrowser.open("https://google.com")
speak("I have opened Google in your browser.")
elif "open facebook" in command:
webbrowser.open("https://facebook.com")
speak("I have opened Facebook in your browser.")
elif "open youtube" in command:
webbrowser.open("https://youtube.com")
speak("I have opened YouTube in your browser.")
elif "open linkedin" in command:
webbrowser.open("https://linkedin.com")
speak("I have opened LinkedIn in your browser.")
elif "open github" in command:
webbrowser.open("https://github.com/")
speak("I have opened GitHub in your browser.")
elif "open twitter" in command:
webbrowser.open("https://x.com/")
speak("I have opened Twitter in your browser.")
elif "open chat" in command:
webbrowser.open("https://chatgpt.com/")
speak("I have opened ChatGPT in your browser.")
elif command.startswith("play"):
song = command.split("play", 1)[1].strip()
link = musicLibrary.music.get(song)
if link:
webbrowser.open(link)
speak(f"Song {song} is ready to be played.")
else:
speak("Sorry, I couldn't find the song in your music library.")
elif "news" in command:
get_news()
elif "weather in" in command:
city = command.split("weather in", 1)[1].strip()
get_weather(city)
elif "open" in command:
app_name = command.split("open", 1)[1].strip()
open_application(app_name)
else:
search_google(command)
except Exception as e:
speak(f"An error occurred: {str(e)}")
# Main loop to keep listening for commands
if __name__ == "__main__":
speak("Initializing Pogo...")
recognizer = sr.Recognizer()
recognizer.energy_threshold = 3000
recognizer.dynamic_energy_threshold = True
def listen_for_wake_word():
"""Listens for the wake word 'Pogo'"""
while True:
try:
print("Listening for wake word...")
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source, duration=1) # Adjust for ambient noise
audio = recognizer.listen(source, timeout=3)
try:
word = recognizer.recognize_google(audio)
if "pogo" in word.lower():
speak("How may I help you?")
return
except sr.UnknownValueError:
continue
except sr.RequestError as e:
speak(f"Could not request results from Google Speech Recognition service; {str(e)}")
except Exception as e:
speak(f"An unexpected error occurred: {str(e)}")
except sr.WaitTimeoutError:
continue
except Exception as e:
speak(f"An unexpected error occurred: {str(e)}")
while True:
listen_for_wake_word()
try:
print("Pogo Active, listening for command...")
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source, duration=1) # Adjust for ambient noise
audio = recognizer.listen(source)
command = recognizer.recognize_google(audio)
if "stop" in command.lower():
speak("Signing off. Goodbye!")
break
process_command(command)
except sr.UnknownValueError:
speak("Sorry, I didn't catch that. Please try again.")
except sr.RequestError as e:
speak(f"Could not request results from Google Speech Recognition service; {str(e)}")
except Exception as e:
speak(f"An unexpected error occurred: {str(e)}")