-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_adder.py
45 lines (34 loc) · 1.58 KB
/
spotify_adder.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
import logging
import requests
class SpotifyAdder:
def __init__(self, spotify_user):
self._spotify_user = spotify_user
self._logger = logging.getLogger("gpm2spotify")
def add_to_library(self, song_ids):
"""Add a list of song's to the users library
:param song_ids: Array of Strings, (Max 50) List of song ids that are added to Spotify Library.
:return: Bool True if successful; False otherwise
"""
endpoint = "https://api.spotify.com/v1/me/tracks"
return self._spotify_user.make_request("PUT", endpoint, json=song_ids) is not None
def create_playlist(self, name, public=False):
"""Creates a new playlist
:param name: String, Name of the playlist
:return: JSON Object, created playlist object
"""
user_id = self._spotify_user.user_id
endpoint = f"https://api.spotify.com/v1/users/{user_id}/playlists"
json = {
"name": name,
"public": public
}
return self._spotify_user.make_request("POST", endpoint, json=json)
def add_songs_to_playlist(self, playlist_id, song_uris):
"""Add a list of song's to a playlist
:param playlist_id: Stirng,
:param song_uris: Array of Strings, (Max 50) List of spotify song uris
that need to be added to Spotify Playlist.
:return: Bool True if successful; False otherwise
"""
endpoint = f"https://api.spotify.com/v1/playlists/{playlist_id}/tracks"
return not not self._spotify_user.make_request("POST", endpoint, json=song_uris)