added make playlist

This commit is contained in:
aj 2019-05-14 13:29:40 +01:00
parent fa7748b0ae
commit 9ba05eb7a7
2 changed files with 27 additions and 5 deletions

View File

@ -11,6 +11,8 @@ if __name__ == '__main__':
network.getPlaylist('000Eh2vXzYGgrEFlgcWZj3') network.getPlaylist('000Eh2vXzYGgrEFlgcWZj3')
network.makePlaylist('new playlist')
#network.getPlayer() #network.getPlayer()
# playlists = network.getUserPlaylists() # playlists = network.getUserPlaylists()

View File

@ -25,15 +25,15 @@ class network:
return None return None
def _makePostRequest(self, method, url, params=None, headers={}): def _makePostRequest(self, method, url, params=None, json=None, headers={}):
headers['Authorization'] = 'Bearer ' + self.user.access_token headers['Authorization'] = 'Bearer ' + self.user.access_token
req = requests.post(const.api_url + url, params=params, headers=headers) req = requests.post(const.api_url + url, params=params, json=json, headers=headers)
if 200 <= req.status_code < 300: if 200 <= req.status_code < 300:
log.log(method, 'post', str(req.status_code)) log.log(method, 'post', str(req.status_code))
return req.text return req
else: else:
log.log(method, 'post', str(req.status_code), req.text) log.log(method, 'post', str(req.status_code), req.text)
@ -47,7 +47,7 @@ class network:
if 200 <= req.status_code < 300: if 200 <= req.status_code < 300:
log.log(method, 'put', str(req.status_code)) log.log(method, 'put', str(req.status_code))
return req.text return req
else: else:
log.log(method, 'put', str(req.status_code), req.text) log.log(method, 'put', str(req.status_code), req.text)
@ -112,7 +112,7 @@ class network:
params = {'offset': offset, 'limit': limit} params = {'offset': offset, 'limit': limit}
resp = self._makeGetRequest('getPlaylistTracks', 'playlists/{}/tracks'.format(playlistid), params) resp = self._makeGetRequest('getPlaylistTracks', 'playlists/{}/tracks'.format(playlistid), params=params)
tracks += resp['items'] tracks += resp['items']
@ -195,3 +195,23 @@ class network:
else: else:
log.log("setVolume", volume, "not allowed") log.log("setVolume", volume, "not allowed")
def makePlaylist(self, name, description=None, public=True, collaborative=False):
log.log("makePlaylist", name, 'description:{}'.format(description), 'public:{}'.format(public), 'collaborative:{}'.format(collaborative))
headers = {"Content-Type": "application/json"}
json = {"name": name, "public": public, "collaborative": collaborative}
if description is not None:
json["description"] = description
resp = self._makePostRequest('makePlaylist', 'users/{}/playlists'.format(self.user.username), json=json, headers=headers).json()
if resp is not None:
playlist = playlistclass(resp["id"], uri=resp['uri'], name=resp['name'], userid=resp['owner']['id'])
return playlist
return None