diff --git a/spotipy/spotipy.py b/spotipy/spotipy.py index a4d6405..6dbf1c3 100644 --- a/spotipy/spotipy.py +++ b/spotipy/spotipy.py @@ -265,13 +265,15 @@ class Spotify(object): ''' return self._get('users/' + user) - def user_playlists(self, user): + 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) + 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 @@ -283,7 +285,8 @@ class Spotify(object): ''' if playlist_id == None: return self._get("users/%s/starred" % (user), fields=fields) - return self._get("users/%s/playlists/%s" % (user, playlist_id), + plid = self._get_id('playlist', playlist_id) + return self._get("users/%s/playlists/%s" % (user, plid), fields=fields) def user_playlist_create(self, user, name, public=True): diff --git a/tests/authtests.py b/tests/authtests.py new file mode 100644 index 0000000..4555690 --- /dev/null +++ b/tests/authtests.py @@ -0,0 +1,142 @@ +# -*- coding: latin-1 -*- + +import spotipy +from spotipy import util +import unittest +import pprint +import sys + +''' + Since these tests require authentication they are maintained + separately from the other tests. + + These tests try to be benign and leave your collection and + playlists in a relatively stable state. +''' + +class AuthTestSpotipy(unittest.TestCase): + ''' + These tests require user authentication + ''' + + + playlist = "spotify:user:plamere:playlist:2oCEWyyAPbZp9xhVSxZavx" + four_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp", + "spotify:track:7IHOIqZUUInxjVkko181PB", + "4VrWlk8IQxevMvERoX08iC", + "http://open.spotify.com/track/3cySlItpiPiIAzU3NyHCJf"] + + two_tracks = ["spotify:track:6RtPijgfPKROxEzTHNRiDp", + "spotify:track:7IHOIqZUUInxjVkko181PB"] + + other_tracks=["spotify:track:2wySlB6vMzCbQrRnNGOYKa", "spotify:track:29xKs5BAHlmlX1u4gzQAbJ", + "spotify:track:1PB7gRWcvefzu7t3LJLUlf"] + + bad_id = 'BAD_ID' + + def test_track_bad_id(self): + try: + track = spotify.track(self.bad_id) + self.assertTrue(False) + except spotipy.SpotifyException: + self.assertTrue(True) + + + def test_basic_user_profile(self): + user = spotify.user(username) + self.assertTrue(user['id'] == username) + + def test_current_user(self): + user = spotify.current_user() + self.assertTrue(user['id'] == username) + + def test_me(self): + user = spotify.me() + self.assertTrue(user['id'] == username) + + + def test_user_playlists(self): + playlists = spotify.user_playlists(username, limit=5) + self.assertTrue('items' in playlists) + + # known API issue currently causes this test to fail + self.assertTrue(len(playlists['items']) == 5) + + def test_current_user_saved_tracks(self): + tracks = spotify.current_user_saved_tracks() + self.assertTrue(len(tracks['items']) > 0) + + def test_current_user_save_and_unsave_tracks(self): + tracks = spotify.current_user_saved_tracks() + total = tracks['total'] + + spotify.current_user_saved_tracks_add(self.four_tracks) + + tracks = spotify.current_user_saved_tracks() + new_total = tracks['total'] + self.assertTrue(new_total - total == len(self.four_tracks)) + + tracks = spotify.current_user_saved_tracks_delete(self.four_tracks) + tracks = spotify.current_user_saved_tracks() + new_total = tracks['total'] + self.assertTrue(new_total == total) + + def test_user_playlist_ops(self): + # create empty playlist + if False: + # this creates a new playlist every time, so for now + # use a known id + playlist = spotify.user_playlist_create(username, 'spotipy-testing-playlist') + playlist_id = playlist['uri'] + else: + playlist_id = "spotify:user:plamere:playlist:4csW04zSLNZYFsH7qk5X3t" + + # remove all tracks from it + + spotify.user_playlist_replace_tracks(username, playlist_id,[]) + + playlist = spotify.user_playlist(username, playlist_id) + self.assertTrue(playlist['tracks']['total'] == 0) + self.assertTrue(len(playlist['tracks']['items']) == 0) + + # add tracks to it + + spotify.user_playlist_add_tracks(username, playlist_id, self.four_tracks) + playlist = spotify.user_playlist(username, playlist_id) + self.assertTrue(playlist['tracks']['total'] == 4) + self.assertTrue(len(playlist['tracks']['items']) == 4) + + # remove two tracks from it + + spotify.user_playlist_remove_all_occurrences_of_tracks (username, + playlist_id, self.two_tracks) + + playlist = spotify.user_playlist(username, playlist_id) + self.assertTrue(playlist['tracks']['total'] == 2) + self.assertTrue(len(playlist['tracks']['items']) == 2) + + # replace with 3 other tracks + spotify.user_playlist_replace_tracks(username, + playlist_id, self.other_tracks) + + playlist = spotify.user_playlist(username, playlist_id) + self.assertTrue(playlist['tracks']['total'] == 3) + self.assertTrue(len(playlist['tracks']['items']) == 3) + + +if __name__ == '__main__': + if len(sys.argv) > 0: + username = sys.argv[1] + del sys.argv[1] + + scope = 'playlist-modify-public ' + scope += 'user-library-read ' + scope += 'user-library-modify ' + scope += 'user-read-private' + + token = util.prompt_for_user_token(username, scope) + spotify = spotipy.Spotify(auth=token) + spotify.trace = False + unittest.main() + else: + print "Usage: %s username" % (sys.argv[0],) diff --git a/tests/tests.py b/tests/tests.py index 87779a3..63befc8 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -119,18 +119,6 @@ class TestSpotipy(unittest.TestCase): - next - previous - - artist_related_artists - - user_playlists - - user_playlist - - user_playlist_create - - user_playlist_add_tracks - - user_playlist_replace_tracks - - user_playlist_remove_all_occurrences_of_tracks - - user_playlist_remove_specific_occurrences_of_tracks - - me - - current_user - - current_user_saved_tracks_delete - - current_user_saved_tracks_add ''' if __name__ == '__main__':