-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpotiPyVoz.py
More file actions
74 lines (43 loc) · 1.79 KB
/
SpotiPyVoz.py
File metadata and controls
74 lines (43 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import speech_recognition as sr
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import pyttsx3
import os
listener = sr.Recognizer()
sr.Microphone.list_microphone_names()
def ouvir():
try:
with sr.Microphone(device_index=1) as source:
print('Listening...')
voice = listener.listen(source)
command = listener.recognize_google(voice,language='pt-PT')
print(command)
return command
except:
return 'No Sound'
os.environ['SPOTIPY_CLIENT_ID'] = '<Client_ID>'
os.environ['SPOTIPY_CLIENT_SECRET'] = 'Client_Secret'
os.environ['SPOTIPY_REDIRECT_URI'] = 'https://example.com/callback'
scope = "user-read-playback-state,user-modify-playback-state"
sp = spotipy.Spotify(client_credentials_manager=SpotifyOAuth(scope=scope))
engine = pyttsx3.init()
engine.say('This is a new test message!')
engine.runAndWait()
while True:
command = ouvir()
if 'spotify play' in command.lower():
query = command.lower().replace('spotify play','').strip()
results = sp.search(query,1,0,"track")
nome_artista = results['tracks']['items'][0]['artists'][0]['name']
nome_musica = results['tracks']['items'][0]['name']
track_uri = results['tracks']['items'][0]['uri']
engine.say(f'Playing {nome_musica} by {nome_artista}')
engine.runAndWait()
sp.start_playback(uris=[track_uri])
elif 'spotify pause' in command.lower():
sp.pause_playback()
elif 'spotify continue' in command.lower():
sp.start_playback()
elif 'spotify change volume to' in command.lower():
volume = int(command.lower().replace('spotify change volume to','').strip())
sp.volume(volume)