-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (75 loc) · 2.8 KB
/
main.py
File metadata and controls
97 lines (75 loc) · 2.8 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
import speech_recognition as sr
import webbrowser
import pyttsx3
import musicLibrary
import requests
# firstly we have to make recogizer object to recognize what we tell
recognizer = sr.Recognizer()
# next we have to initialize engine and by init it will be initialized
engine = pyttsx3.init()
# newsapi = "0aefa14556b74b60949627f71f442813"
# now we have to make a function which takes text and speaks it
def speak(text):
import pyttsx3
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
engine.stop()
del engine
# creating function for processing command
def processCommand(c):
if "open google" in c.lower():
speak("opening")
webbrowser.open("https://www.google.com")
elif "open facebook" in c.lower():
speak("opening")
webbrowser.open("https://www.facebook.com")
elif "open youtube" in c.lower():
speak("opening")
webbrowser.open("https://www.youtube.com")
elif "open linkedin" in c.lower():
speak("opening")
webbrowser.open("https://www.linkedin.com")
elif c.lower().startswith("play"):
song=c.lower().split(" ")[1]
link=musicLibrary.music[song]
webbrowser.open(link)
elif "headlines" in c.lower():
r = requests.get(f"https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey={newsapi}")
if r.status_code == 200:
# Parse the JSON response
data = r.json()
# Extract the articles
articles = data.get('articles', [])
# Print the headlines
for article in articles:
speak(article['title'])
print(article['title'])
else:
pass
if __name__ == "__main__":
speak("Initializing Alexa....")
while True:
# listen for wake word Alexa
# obtain audio from the microphone
r = sr.Recognizer()
print("recognizing..")
# recognize speech using sphinx
try:
with sr.Microphone() as source:
print("Listening for wake word Alexa...")
audio = r.listen(source, timeout=2, phrase_time_limit=1)
word = r.recognize_google(audio)
print(f"You said: {word}")
if(word.lower()=="alexa"):
speak("Ya, how can i help you")
# listen for command
with sr.Microphone() as source:
print("Alexa Active....")
print("Listening for command...")
audio = r.listen(source)
command=r.recognize_google(audio)
print(f"Command: {command}")
processCommand(command)
except Exception as e:
print("Error: {0}".format(e))