def crawl_songsear(self, text, audio): # Find song title and artist by the lyrics or title song_url = 'https://songsear.ch/q/' + text page = requests.get(song_url) soup = BeautifulSoup(page.content, 'html.parser') song_by_name = soup.find(class_='by-name') if song_by_name: engine_say("songs with the title " + text) for name in song_by_name.find_all('a'): engine_say(name.text) print(name.text) other_songs = soup.find(class_='results') if other_songs: engine_say("Top 10 songs with the lyrics " + text) x = 0 for song in other_songs.find_all(class_='head'): x += 1 if x > 10: break h2 = song.find('h2') engine_say(h2.text) h3 = song.find('h3') engine_say(h3.text) print(h2.text, " - ", h3.text) return True return False
def run(self, text, audio): if self.shazam_it(audio): return if self.crawl_songsear(text): return engine_say("I could not find the song title.")
def run(self, text, audio): spelled = [] words = text.split(" ") for w in words: if w == '' or w in spelled: continue engine_say(w) for l in w: engine_say(l) spelled.append(w)
def other_answer(self, results): # Provide other answers for res in r_json['results']: if res['description'] != '': arr = res['title'].split(' - ') print(arr) engine_say(arr[0]) arr2 = res['description'].split('.') print(arr2) engine_say(arr2[0])
def run(self, text, audio): d = "The date is " + date.today().strftime("%A %B %d, %Y") # Get year in the form of two numbers so voice assistant can say it the two numbers that make up the year lst = d.split() year1 = lst[-1][0:2] year2 = lst[-1][2:] lst.pop() lst.append(year1) lst.append(year2) d = " ".join(lst) engine_say(d) engine_say("The time is " + datetime.now().strftime("%I %M %p"))
def say_parsed_info(self, info): # Say useful information for i in info: if i and isinstance(i, list): for ii in i: print(ii.text) engine_say(ii.text) return True if i: print(i.text) engine_say(i.text) return True return False
def run(self, text, audio): # Get google results r_json = self.crawl_google(text) useful_info = self.parse_google(r_json) # Check if answer was provided if self.say_parsed_info(useful_info): return # Check for results section of response if not r_json['results']: engine_say("Sorry Google does not know the answer") return # Check if wikipedia answer was provided if self.say_wikipedia(r_json['results']): return # Provide another answer self.other_answer(r_json['results'])
def shazam_it(self, text, audio): # Find song matches through shazam url = "https://shazam.p.rapidapi.com/songs/detect" raw_data = audio.get_wav_data() byteslist = bytearray(raw_data) data = base64.b64encode(byteslist) payload = data headers = { 'content-type': "text/plain", 'x-rapidapi-key': os.getenv("API_KEY"), 'x-rapidapi-host': "shazam.p.rapidapi.com" } response = requests.request("POST", url, data=payload, headers=headers) if response.status_code != 200: return False r = response.json() if not r["matches"]: engine_say( "Shazam found no matches for the tune. I will try searching for lyrics." ) return False engine_say(r["track"]["title"]) engine_say(r["track"]["subtitle"]) print(r["track"]["title"], ' - ', r["track"]["subtitle"]) return True
def say_wikipedia(self, results): # Say the wikipedia result first answer_provided = False for res in results: if 'wikipedia' in res['title'].lower(): engine_say("From wikipedia") arr = res['title'].split(' - ') print(arr[0]) engine_say(arr[0]) arr2 = res['description'].split('.') print(arr2[0]) engine_say(arr2[0]) answer_provided = True return answer_provided
import speech_recognition as sr from util import fix_transcription, identify_task from talking_engine import engine_say # Introduce kevin engine_say("Hello, I am KEVIN. How can I help you?") # obtain audio from the microphone s = sr.Recognizer() with sr.Microphone() as source: audio = s.listen(source, timeout=5) # recognize speech using Google Speech Recognition try: # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")` transcribed = fix_transcription(s.recognize_google(audio)) task_words = transcribed.split(" ") task = identify_task(task_words[0]) main_text = " ".join(task_words[1:]) try: task.run(main_text, audio) except Exception as e: print(e) engine_say("Sorry I am not programmed to do that yet.") except sr.UnknownValueError: engine_say("Google Speech Recognition could not understand audio") except sr.RequestError as e: engine_say(