diff --git a/CHANGELOG.md b/CHANGELOG.md index bbd8107..1cb4f5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + + - Support for `playlist_tracks` + +### Changed + + - `user_playlist_tracks` doesn't require a user anymore + +### Deprecated + + - Deprecated `user_playlist` and `user_playlist_tracks` + ## [2.6.3] - 2020-01-16 ### Fixed diff --git a/spotipy/client.py b/spotipy/client.py index ea468e7..1befef9 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -15,6 +15,7 @@ import time import requests import six +import warnings class SpotifyException(Exception): @@ -371,44 +372,21 @@ class Spotify(object): """ return self._get("me/playlists", limit=limit, offset=offset) - def user_playlists(self, user, limit=50, offset=0): - """ Gets playlists of a user - - Parameters: - - user - the id of the usr - - limit - the number of items to return - - offset - the index of the first item to return - """ - return self._get("users/%s/playlists" % user, limit=limit, - offset=offset) - - def user_playlist(self, user, playlist_id=None, fields=None): - """ Gets playlist of a user - Parameters: - - user - the id of the user - - playlist_id - the id of the playlist - - fields - which fields to return - """ - if playlist_id is None: - return self._get("users/%s/starred" % (user), fields=fields) - plid = self._get_id('playlist', playlist_id) - return self._get("users/%s/playlists/%s" % (user, plid), fields=fields) - def playlist(self, playlist_id, fields=None, market=None): - """ Gets playlist by id + """ Gets playlist by id. Parameters: - - playlist - the id of the playlist - - fields - which fields to return - - market - An ISO 3166-1 alpha-2 country code or the string - from_token. - """ + - playlist - the id of the playlist + - fields - which fields to return + - market - An ISO 3166-1 alpha-2 country code or the + string from_token. + """ plid = self._get_id('playlist', playlist_id) - return self._get("playlists/%s" % (plid), fields=fields) - - def playlist_tracks(self, playlist_id=None, fields=None, - limit=100, offset=0, market=None): - ''' Get full details of the tracks of a playlist. + return self._get("playlists/%s" % (plid), fields=fields, market=market) + + def playlist_tracks(self, playlist_id, fields=None, + limit=100, offset=0, market=None): + """ Get full details of the tracks of a playlist. Parameters: - playlist_id - the id of the playlist @@ -416,15 +394,35 @@ class Spotify(object): - limit - the maximum number of tracks to return - offset - the index of the first track to return - market - an ISO 3166-1 alpha-2 country code. - ''' - + """ plid = self._get_id('playlist', playlist_id) - return self._get("playlists/%s/tracks" % (playlist_id), + return self._get("playlists/%s/tracks" % (plid), limit=limit, offset=offset, fields=fields, market=market) - def user_playlist_tracks(self, user, playlist_id=None, fields=None, + def user_playlist(self, user, playlist_id=None, + fields=None, market=None): + warnings.warn( + "You should use `playlist(playlist_id)` instead", + DeprecationWarning) + + """ Gets playlist of a user + + Parameters: + - user - the id of the user + - playlist_id - the id of the playlist + - fields - which fields to return + """ + if playlist_id is None: + return self._get("users/%s/starred" % user) + return self.playlist(playlist_id, fields=fields, market=market) + + def user_playlist_tracks(self, user=None, playlist_id=None, fields=None, limit=100, offset=0, market=None): + warnings.warn( + "You should use `playlist_tracks(playlist_id)` instead", + DeprecationWarning) + """ Get full details of the tracks of a playlist owned by a user. Parameters: @@ -435,10 +433,19 @@ class Spotify(object): - offset - the index of the first track to return - market - an ISO 3166-1 alpha-2 country code. """ - plid = self._get_id('playlist', playlist_id) - return self._get("users/%s/playlists/%s/tracks" % (user, plid), - limit=limit, offset=offset, fields=fields, - market=market) + return self.playlist_tracks(playlist_id, limit=limit, offset=offset, + fields=fields, market=market) + + def user_playlists(self, user, limit=50, offset=0): + """ Gets playlists of a user + + Parameters: + - user - the id of the usr + - limit - the number of items to return + - offset - the index of the first item to return + """ + return self._get("users/%s/playlists" % user, limit=limit, + offset=offset) def user_playlist_create(self, user, name, public=True, description=''): """ Creates a playlist for a user diff --git a/tests/test_auth.py b/tests/test_auth.py index a0b8237..ac3e3c6 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -23,6 +23,7 @@ from spotipy import ( import os import sys import unittest +import warnings sys.path.insert(0, os.path.abspath(os.pardir)) @@ -61,6 +62,10 @@ class AuthTestSpotipy(unittest.TestCase): @classmethod def setUpClass(self): + warnings.filterwarnings( + "ignore", + category=ResourceWarning, + message="unclosed.*") missing = list(filter(lambda var: not os.getenv(CCEV[var]), CCEV)) @@ -119,13 +124,6 @@ class AuthTestSpotipy(unittest.TestCase): results = self.spotify.user_playlist_tracks(user, pid) self.assertTrue(len(results['items']) >= 0) - # known API issue currently causes this test to fail - # the issue is that the API doesn't currently respect the - # limit parameter - # def user_playlist_tracks(self, user, playlist_id=None, fields=None, - # limit=100, offset=0): - # self.assertTrue(len(playlists['items']) == 5) - def test_current_user_saved_albums(self): # List albums = self.spotify.current_user_saved_albums() @@ -281,6 +279,17 @@ class AuthTestSpotipy(unittest.TestCase): pl = self.spotify.playlist(self.playlist) self.assertTrue(pl["tracks"]["total"] > 0) + def test_playlist_tracks(self): + # New playlist ID + pl = self.spotify.playlist_tracks(self.playlist_new_id, limit=2) + self.assertTrue(len(pl["items"]) == 2) + self.assertTrue(pl["total"] > 0) + + # Old playlist ID + pl = self.spotify.playlist_tracks(self.playlist, limit=2) + self.assertTrue(len(pl["items"]) == 2) + self.assertTrue(pl["total"] > 0) + def test_user_follows_and_unfollows_artist(self): # Initially follows 1 artist res = self.spotify.current_user_followed_artists() @@ -307,6 +316,24 @@ class AuthTestSpotipy(unittest.TestCase): # Unfollow these 2 users self.spotify.user_unfollow_users(users) + def test_deprecated_starred(self): + pl = self.spotify.user_playlist(self.username) + self.assertTrue(pl["tracks"] is None) + self.assertTrue(pl["owner"] is None) + + def test_deprecated_user_playlist(self): + # Test without user due to change from + # https://developer.spotify.com/community/news/2018/06/12/changes-to-playlist-uris/ + pl = self.spotify.user_playlist(None, self.playlist) + self.assertTrue(pl["tracks"]["total"] > 0) + + def test_deprecated_user_playlis(self): + # Test without user due to change from + # https://developer.spotify.com/community/news/2018/06/12/changes-to-playlist-uris/ + pl = self.spotify.user_playlist_tracks(None, self.playlist, limit=2) + self.assertTrue(len(pl["items"]) == 2) + self.assertTrue(pl["total"] > 0) + if __name__ == '__main__': unittest.main()