-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
75 lines (52 loc) · 2.08 KB
/
code.py
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
from spotipy import Spotify
class InvalidSearchError(Exception):
pass
def get_album_uri(spotify: Spotify, name: str) -> str:
"""
:param spotify: Spotify object to make the search from
:param name: album name
:return: Spotify uri of the desired album
"""
# Replace all spaces in name with '+'
original = name
name = name.replace(' ', '+')
results = spotify.search(q=name, limit=1, type='album')
if not results['albums']['items']:
raise InvalidSearchError(f'No album named "{original}"')
album_uri = results['albums']['items'][0]['uri']
return album_uri
def get_artist_uri(spotify: Spotify, name: str) -> str:
"""
:param spotify: Spotify object to make the search from
:param name: album name
:return: Spotify uri of the desired artist
"""
# Replace all spaces in name with '+'
original = name
name = name.replace(' ', '+')
results = spotify.search(q=name, limit=1, type='artist')
if not results['artists']['items']:
raise InvalidSearchError(f'No artist named "{original}"')
artist_uri = results['artists']['items'][0]['uri']
print(results['artists']['items'][0]['name'])
return artist_uri
def get_track_uri(spotify: Spotify, name: str) -> str:
"""
:param spotify: Spotify object to make the search from
:param name: track name
:return: Spotify uri of the desired track
"""
# Replace all spaces in name with '+'
original = name
name = name.replace(' ', '+')
results = spotify.search(q=name, limit=1, type='track')
if not results['tracks']['items']:
raise InvalidSearchError(f'No track named "{original}"')
track_uri = results['tracks']['items'][0]['uri']
return track_uri
def play_album(spotify=None, device_id=None, uri=None):
spotify.start_playback(device_id=device_id, context_uri=uri)
def play_artist(spotify=None, device_id=None, uri=None):
spotify.start_playback(device_id=device_id, context_uri=uri)
def play_track(spotify=None, device_id=None, uri=None):
spotify.start_playback(device_id=device_id, uris=[uri])