Toda vez que tento executar com o else aparece este erro, e se tento chamar a função ultimas_noticias, com mais de 2 noticias, aparece erro semelhante.
joice.py
from gtts import gTTS
import speech_recognition as sr
from playsound import playsound
from requests import get
from bs4 import BeautifulSoup
##### Configurações #####
hotword = 'joice'
with open('joyce-python-assistente-e417193a940f.json') as credenciais_google:
credenciais_google = credenciais_google.read()
##### Funções Principais #####
def monitora_audio():
microfone = sr.Recognizer()
with sr.Microphone() as source:
while True:
print("Aguardando o comando: ")
audio = microfone.listen(source)
try:
trigger = (microfone.recognize_google_cloud(
audio, credentials_json=credenciais_google, language='pt-BR'))
trigger = trigger.lower()
# executrar os comandos
if hotword in trigger:
print('Comando: ', trigger)
responde('feedback')
executa_comandos(trigger)
break
except sr.UnknownValueError:
print("Google Cloud Speech could not understand audio")
except sr.RequestError as e:
print(
"Could not request results from Google Cloud Speech service; {0}".format(e))
return trigger
def responde(arquivo):
playsound(
'C:/Users/mateu/OneDrive/Documents/Python-AssistenteVirtual/Joice/audios/' + arquivo + '.mp3')
def cria_audio(mensagem):
tts = gTTS(mensagem, lang='pt-br')
tts.save(
'C:/Users/mateu/OneDrive/Documents/Python-AssistenteVirtual/Joice/audios/mensagem.mp3')
print('Joice: ', mensagem)
playsound(
'C:/Users/mateu/OneDrive/Documents/Python-AssistenteVirtual/Joice/audios/mensagem.mp3')
def executa_comandos(trigger):
if 'notícias' in trigger:
ultimas_noticias()
else:
mensagem = trigger.strip(hotword)
cria_audio(mensagem)
print('C. Inválido', mensagem)
responde('comando_invalido')
##### Funções de comandos #####
def ultimas_noticias():
site = get('https://news.google.com/news/rss?ned=pt_br&gl=BR&hl=pt')
noticias = BeautifulSoup(site.text, 'html.parser')
for item in noticias.findAll('item')[:2]:
mensagem = item.title.text
cria_audio(mensagem)
if __name__ == '__main__':
while True:
monitora_audio()
Erro que me aparece:
[Running] python -u "c:\Users\mateu\OneDrive\Documents\Python-AssistenteVirtual\Joice\joice.py"
Aguardando o comando:
Comando: joice me responda
Joice: me responda
C. Inv�lido me responda
Error 310 for command:
open C:/Users/mateu/OneDrive/Documents/Python-AssistenteVirtual/Joice/audios/comando_invalido1.mp3
Especifique um nome de dispositivo ou driver com menos de 79 caracteres.
Error 263 for command:
close C:/Users/mateu/OneDrive/Documents/Python-AssistenteVirtual/Joice/audios/comando_invalido1.mp3
O dispositivo especificado n�o est� aberto ou o MCI n�o o reconhece.
Failed to close the file: C:/Users/mateu/OneDrive/Documents/Python-AssistenteVirtual/Joice/audios/comando_invalido1.mp3
Traceback (most recent call last):
File "c:\Users\mateu\OneDrive\Documents\Python-AssistenteVirtual\Joice\joice.py", line 76, in <module>
monitora_audio()
File "c:\Users\mateu\OneDrive\Documents\Python-AssistenteVirtual\Joice\joice.py", line 29, in monitora_audio
executa_comandos(trigger)
File "c:\Users\mateu\OneDrive\Documents\Python-AssistenteVirtual\Joice\joice.py", line 61, in executa_comandos
responde('comando_invalido1')
File "c:\Users\mateu\OneDrive\Documents\Python-AssistenteVirtual\Joice\joice.py", line 41, in responde
playsound(
File "C:\Users\mateu\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 72, in _playsoundWin
winCommand(u'open {}'.format(sound))
File "C:\Users\mateu\AppData\Local\Programs\Python\Python39\lib\site-packages\playsound.py", line 64, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 310 for command:
open C:/Users/mateu/OneDrive/Documents/Python-AssistenteVirtual/Joice/audios/comando_invalido1.mp3
Especifique um nome de dispositivo ou driver com menos de 79 caracteres.
[Done] exited with code=1 in 18.283 seconds