You are on page 1of 9

1.

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

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 Perosnal Voice Dictionary")
speak.Speak("Ask me anything")
print("\n# Start talking to RIO")

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)

# recognize speech using Sphinx/Google


try:
response = r.recognize_google(audio)
print("I think you want to know: " + response)
speak.Speak("I think you want to know" + response)
str = response
i=i+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))

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)

# recognize speech using Sphinx/Google


try:
response = r.recognize_google(audio)
if "wake up" in response or "rio" in response:
print("I am ready, Boss!")
speak.Speak("I am ready boss!")
count=0
break

except sr.UnknownValueError:
print("Sleeping...")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))

mean1 = "tell me the meaning of "


mean2 = "what is the meaning of "
mean3 = "find the meaning of "
mean4 = "what is "
mean5 = "what do you mean by "
while True:
count=0
command()

if 'stop' in str or 'quit' in str or 'exit' in str or 'close' in str:


if hr>19:
print("Closing Program. Good Night Boss!!!")
speak.Speak("Closing Program. Good Night Boss")
else:
print("Sure Boss. Closing Program")
speak.Speak("Sure boss. Closing Program")
break
elif 'hey' in str or 'time' in str:
print("Hello boss,Time is: "+localtime)
speak.Speak("Hello boss,Time is "+localtime[-13:-5])

elif str == mean1 + str[23:]:


print("hhhhh")
res = requests.get('https://www.google.com/search?q='+str[23:]+'
meaning').text
soup = BeautifulSoup(res,'html.parser')
soup.encode('utf-8')
print(soup)
print("Meaning of "+str[23:]+" is: ",end="")
speak.Speak("Meaning of "+str[23:]+" is: ")
for link in soup.find_all("div", {"class": "v9i61e"}):
if link.text.strip()[0]=="'":
print("\nFor Example:")
speak.Speak("For Example")
print(link.text.strip())
speak.Speak(link.text.strip())
print("Or: ")
speak.Speak("Or")

elif str ==mean2 + str[23:]:


res = requests.get('https://www.google.com/search?q='+str[23:]+'
meaning').text
soup = BeautifulSoup(res,'html.parser')
soup.encode('utf-8')
print("Meaning of "+str[23:]+" is: ",end="")
speak.Speak("Meaning of "+str[23:]+" 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("Or: ")
speak.Speak("Or")

elif str ==mean3 + str[20:]:


res = requests.get('https://www.google.com/search?q='+str[20:]+'
meaning').text
soup = BeautifulSoup(res,'html.parser')
soup.encode('utf-8')
print("Meaning of "+str[20:]+" is: ",end="")
speak.Speak("Meaning of "+str[20:]+" 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("Or: ")
speak.Speak("Or")

elif str ==mean4 + str[8:]:


res = requests.get('https://www.google.com/search?q='+str[7:]+'
meaning').text
soup = BeautifulSoup(res,'html.parser')
soup.encode('utf-8')
print("Meaning of "+str[7:]+" is: ",end="")
speak.Speak("Meaning of "+str[7:]+" 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("Or: ")
speak.Speak("Or")

elif str ==mean5 + str[20:]:


res = requests.get('https://www.google.com/search?q='+str[20:]+'
meaning').text
soup = BeautifulSoup(res,'html.parser')
soup.encode('utf-8')
print("Meaning of "+str[20:]+" is: ",end="")
speak.Speak("Meaning of "+str[20:]+" 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("Or: ")
speak.Speak("Or")

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)

# recognize speech using Sphinx/Google


try:
response = r.recognize_google(audio)
print("I think you want to : " + response)
speak.Speak("I think you want to " + response)
str = response
i=i+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))

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)

# recognize speech using Sphinx/Google


try:
response = r.recognize_google(audio)
if "wake up" in response or "rio" in response:
print("I am ready, Boss!")
speak.Speak("I am ready boss!")
count=0
break

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("\nHere are the lyrics for ",titlename,"...")


speak.Speak("\nHere are the lyrics for "+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))

mean1 = "play "


mean2 = "play the song "

while True:
count=0
command()

if 'stop' in str or 'quit' in str or 'exit' in str or 'close' in str:


if hr>19:
print("Closing Program. Good Night Boss!!!")
speak.Speak("Closing Program. Good Night Boss")
else:
print("Sure Boss. Closing Program")
speak.Speak("Sure boss. Closing Program")
break
elif 'time' in str:
print("Hello boss,Time is: "+localtime)
speak.Speak("Hello boss,Time is "+localtime[-13:-5])

elif str == mean1 + str[5:]:


playSongFromYoutube(str[5:])

elif str ==mean2 + str[14:]:


playSongFromYoutube(str[14:])

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()

if 'yes' in str1 or 'ofcourse' in str1:


#speak.Speak("Sure boss")
continue
elif 'no' in str1 or 'na' in str1:
speak.Speak("OK. Closing Program")
break

==========================================================================

3. Chegg

import speech_recognition as sr
import win32com.client as wincl
from selenium import webdriver

import urllib3

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC


from selenium.webdriver.common.by import By

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()

=======================================================================

You might also like