-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrank.py
More file actions
142 lines (133 loc) · 5.21 KB
/
frank.py
File metadata and controls
142 lines (133 loc) · 5.21 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
import speech_recognition as sr
import webbrowser
import json
import spotipy
import time
import wikipedia
from os import system
from pprint import pprint
def handleSearches(split, cmdstr):
if split[1].lower() == 'google':
system('say pulling up a google search now')
# print("I am pulling up a google search for you now")
if split[2] == 'for':
q = cmdstr.split(' ',3)[3]
url = "https://www.google.com.tr/search?q={}".format(q)
webbrowser.open(url)
else:
q = cmdstr.split(' ',2)[2]
url = "https://www.google.com.tr/search?q={}".format(q)
webbrowser.open(url)
if split [1] == 'Wikipedia':
system('say pulling the wikipedia page now')
if split[2] == 'for':
page = wikipedia.page(cmdstr.split(' ', 3)[3])
webbrowser.open(page.url)
else:
try:
page = wikipedia.page(cmdstr.split(' ', 2)[2])
webbrowser.open(page.url)
except wikipedia.exceptions.DisambiguationError as e:
system('say Disambiguation Error. Here is a list of possible searches')
print(e)
def handleCmd(str):
split = str.split()
cmdTerm = split[0]
if str == "who are you":
system('say You made me. I am not real. What else do you want to know?')
iamnotarobot()
if str == "good morning frank":
print("good morning max")
system('say Good morning max')
if str == "stop listening":
system('say Goodbye max')
print("goodbye max")
return True
if cmdTerm == "search":
handleSearches(split, str)
if cmdTerm == "play":
system('say Sure thing let me quickly pull up that dank track')
print("playing dank tunes as per your request")
if len(split) > 1:
system("sudo spotify play " + str.split(' ', 1)[1])
else:
system("sudo spotify play")
if cmdTerm.lower() == "playlist":
system('say Sure, thats a good playlist')
system("sudo spotify play list " + str.split(' ', 1)[1])
return False
def handleRequest():
with m as source:
audio = r.listen(source)
try:
obj = r.recognize_google(audio, show_all=True)
n = json.dumps(obj)
o = json.loads(n)
cmd = "" + o['alternative'][0]['transcript']
return handleCmd(cmd)
except sr.UnknownValueError:
print("Frank could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
r = sr.Recognizer()
m = sr.Microphone()
with m as source:
r.adjust_for_ambient_noise(source) # we only need to calibrate once, before we start listening
# this is called from the background thread
def startListening():
# received audio data, now we'll recognize it using Google Speech Recognition
while True:
with m as source:
audio = r.listen(source)
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
print("Frank thinks you said " + r.recognize_google(audio))
obj = r.recognize_google(audio, show_all=True)
n = json.dumps(obj)
o = json.loads(n)
cmd = "" + o['alternative'][0]['transcript']
if "hey frank" in cmd.lower():
system('say What can I do for you?')
if handleRequest():
break
if "stop listening" in cmd.lower():
system('say Goodbye')
break
except sr.UnknownValueError:
print("Frank could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
def answerSelfQuestion(cmd):
split = cmd.split()
cmdTerm = split[0]
with open('whoami.json') as data_file:
data = json.load(data_file)
pprint(data)
if str == 'what is your name':
if data['name']:
system('say My name is ' + data['name'])
else:
system('say I dont know')
def iamnotarobot():
while True:
with m as source:
audio = r.listen(source)
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
print("Frank thinks you said " + r.recognize_google(audio))
from pprint import pprint
obj = r.recognize_google(audio, show_all=True)
n = json.dumps(obj)
o = json.loads(n)
cmd = "" + o['alternative'][0]['transcript']
if answerSelfQuestion(cmd):
break
except sr.UnknownValueError:
print("Frank could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
startListening()