Professional Documents
Culture Documents
Speaking dictionary
import speech_recognition as sr
import win32com.client as wincl
import urllib3
import time
import datetime
import requests
from bs4 import BeautifulSoup
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
localtime = time.asctime(time.localtime(time.time()))
now = datetime.datetime.now()
hr = now.hour
speak = wincl.Dispatch("SAPI.SpVoice")
str = ''
count=0
def command():
global str
i = 0
global count
while i==0:
if count>1:
awake()
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
# print("Please wait. Calibrating microphone...")
# listen for 1 second and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=1)
print("\nSpeak Command...")
speak.Speak("Speak Command")
audio = r.listen(source, phrase_time_limit=3)
except sr.UnknownValueError:
print("Sorry Boss.I didn't understand")
speak.Speak("Sorry boss! I didn't understand")
count+=1
#print(count)
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
def awake():
global count
while True:
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
print("---")
audio = r.listen(source, phrase_time_limit=2)
except sr.UnknownValueError:
print("Sleeping...")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
else:
res = requests.get('https://www.google.com/search?q='+str+' meaning').text
soup = BeautifulSoup(res,'html.parser')
soup.encode('utf-8')
print("Meaning of "+str+" is: ",end="")
speak.Speak("Meaning of "+str+" is: ")
for link in soup.find_all("div", {"class": "v9i61e"}):
if link.text.strip()[0]=="'":
print("\nFor Example:")
speak.Speak("For Example")
if '<b>' in link.text:
txt=link.text.strip()
txt = txt.replace("<b>", "")
txt = txt.replace("</b>", "")
print(txt)
speak.Speak(txt)
else:
print(link.text.strip())
speak.Speak(link.text.strip())
print("\nOr: ")
speak.Speak("Or")
===================================================================================
=====
2. Speaking song
import speech_recognition as sr
import win32com.client as wincl
from selenium import webdriver
import sys
#from selenium.webdriver.common.keys import Keys
import urllib3
import time
import datetime
import requests
from bs4 import BeautifulSoup
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
localtime = time.asctime(time.localtime(time.time()))
now = datetime.datetime.now()
hr = now.hour
speak = wincl.Dispatch("SAPI.SpVoice")
str = ''
str1=''
count=0
songCount=0
time1=0
if hr >= 0 and hr < 12:
print("Good Morning Boss!\n")
speak.Speak("Good Morning boss!")
elif hr >= 12 and hr < 16:
print("Good Afternoon Boss!\n")
speak.Speak("Good Afternoon boss!")
elif hr >=16 and hr <= 24:
print("Good Evening Boss!\n")
speak.Speak("Good Evening boss!")
speak.Speak("I'm RIO, Your Voice Music Player ")
print("\n# Start talking to RIO")
def command():
global str
i = 0
global count
while i==0:
if count>2:
awake()
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
# print("Please wait. Calibrating microphone...")
# listen for 1 second and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=1)
print("\nSpeak Song Name")
speak.Speak("Speak song name")
audio = r.listen(source, phrase_time_limit=3)
except sr.UnknownValueError:
print("Sorry Boss.I didn't understand")
speak.Speak("Sorry boss! I didn't understand")
count+=1
#print(count)
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
def awake():
global count
while True:
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
print("---")
audio = r.listen(source, phrase_time_limit=2)
except sr.UnknownValueError:
print("Sleeping...")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
def playSongFromYoutube(str):
global time1
driver=webdriver.Chrome("C:/Users/HP/Downloads/chromedriver.exe")
driver.maximize_window()
driver.get('https://www.youtube.com/results?search_query='+str)
playSong=driver.find_element_by_xpath(".//*[@id='video-title']/yt-formatted-
string").click()
try:
time.sleep(10)
add=driver.find_element_by_xpath("//*[@id='skip-button:s']/span/button")
if (add):
add.click()
print("Add skipped")
speak.Speak("Add skipped")
time.sleep(2)
title=driver.find_element_by_xpath("//*[@id='container']/h1/yt-formatted-
string")
titlename=title.text
print("------Now Playing-->'",titlename,"'")
speak.Speak("------Now Playing-->'"+titlename+"'")
except Exception:
print("Enjoy add free music")
speak.Speak("Enjoy add free music")
title=driver.find_element_by_xpath("//*[@id='container']/h1/yt-formatted-
string")
titlename=title.text
print("------Now Playing-->'",titlename,"'")
speak.Speak("------Now Playing-->'"+titlename+"'")
print("-----------------------\n")
res = requests.get('https://www.google.com/search?q='+str+' lyrics').text
soup = BeautifulSoup(res,'html.parser')
#print(res)
soup.encode('utf-8')
lyrics = soup.find("div", {"class": "xpc"}).get_text().strip()
print(lyrics)
#ti=driver.find_element_by_class_name("ytp-time-duration").text
time1 = driver.execute_script(
"return document.getElementById('movie_player').getDuration()")
print(time1)
def afterSong():
global str1
j = 0
global count
while j==0:
if count>2:
awake()
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
# print("Please wait. Calibrating microphone...")
# listen for 1 second and create the ambient noise energy level
r.adjust_for_ambient_noise(source, duration=1)
print("\nWant to play another")
speak.Speak("Want to play another ?")
audio = r.listen(source, phrase_time_limit=3)
# recognize speech using Sphinx/Google
try:
response = r.recognize_google(audio)
print("Sure Boss ")
speak.Speak("Sure boss ")
str1 = response
j=j+1
except sr.UnknownValueError:
print("Sorry Boss.I didn't understand")
speak.Speak("Sorry boss! I didn't understand")
count+=1
#print(count)
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
while True:
count=0
command()
else:
playSongFromYoutube(str)
#time.sleep(time1)
time1=int(time1)
time1=time1-27
for i in range(time1, 0, -1):
sys.stdout.write("\r")
sys.stdout.write("{:2d} seconds remaining.".format(i))
sys.stdout.flush()
time.sleep(1)
afterSong()
==========================================================================
3. Chegg
import speech_recognition as sr
import win32com.client as wincl
from selenium import webdriver
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
driver=webdriver.Chrome("C:/Users/HP/git/repository/SeleniumJavaProject/src/test/
resources/drivers/chromedriver.exe")
driver.maximize_window()
driver.get("https://www.google.com/")
driver.get("https://www.chegg.com/my/expertqa")
driver.find_element_by_id("emailForSignIn").send_keys( "mahajanrishav2@gmail.com" )
driver.find_element_by_id("passwordForSignIn").send_keys( "12Maths34" )
driver.find_element_by_name("login").click()
WebDriverWait(webdriver.Chrome(),
5).until(EC.presence_of_element_located((By.xpath,
"//button[contains(text(),'Start')]")))
driver.find_element_by_xpath("//button[contains(text(),'Start')]").click()
WebDriverWait(webdriver.Chrome(),
5).until(EC.presence_of_element_located((By.xpath,
"//span[contains(text(),'Start')]")))
driver.find_element_by_xpath("//span[contains(text(),'Start')]").click()
=======================================================================