How do I make Python’s speech recognition module take my YouTube search query

I’m working on a voice assistant using Python. I would like to make Python take my search query and automatically type it in the YouTube search bar. I’m able to open YouTube but I’m not able to make Python take my search query. Microsoft keeps sending these messages.

Look for an app in the Microsoft store

I’ve been trying this for days now and all the tutorials/questions don’t help me out.

My code…

import datetime
import webbrowser
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
import speech_recognition as sr
import pyttsx3
import pyaudio
import os
import random
import gtts

new=2

service = Service('C:UsersAdminPycharmProjectspythonProjectchromedriver.exe')
chrome_options = Options()
chrome_options.add_argument(r'--user-data-dir=C:UsersAdminAppDataLocalGoogleChromeUser DataDefault')
chrome_options.add_argument(r'--profile-directory=Rohan')
driver = webdriver.Chrome(service=service, options=chrome_options)


r1 = sr.Recognizer()
r2 = sr.Recognizer()
r3 = sr.Recognizer()

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)

with sr.Microphone() as source:
 print('Listening...')
 engine.say("Hey I'm your bot, Trevor! What can I do for you today?")
 engine.runAndWait()
 audio = r3.listen(source)

# From here

if 'YouTube' in r2.recognize_google(audio):
    r2 = sr.Recognizer()

    with sr.Microphone() as source:

        print("Type what you want to see.", end='')
        audio = r2.listen(source)
        keyword = audio

        search = input()
        url = ('https://www.youtube.com/?q=')
        result = search+url
        webbrowser.open_new_tab(result)
        driver.quit()

    try:
        get = r2.recognize_google(audio)
        print(get)
    except sr.UnknownValueError:
        print('Error on your side')
    except sr.RequestError:
        print('Error on my side')

# Till here is the code to run a YouTube vid

PS: The code is indented as per Pycharm’s indentation

pls, help me out and give me a solution.

Source: Python-3x Questions

LEAVE A COMMENT