Deprecate old endpoints

This commit is contained in:
Stephane Bruckert 2020-01-16 23:14:52 +00:00
parent 960ee3cc8f
commit 3d22cf29ce
3 changed files with 95 additions and 49 deletions

View File

@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [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 ## [2.6.3] - 2020-01-16
### Fixed ### Fixed

View File

@ -15,6 +15,7 @@ import time
import requests import requests
import six import six
import warnings
class SpotifyException(Exception): class SpotifyException(Exception):
@ -371,44 +372,21 @@ class Spotify(object):
""" """
return self._get("me/playlists", limit=limit, offset=offset) 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): def playlist(self, playlist_id, fields=None, market=None):
""" Gets playlist by id """ Gets playlist by id.
Parameters: Parameters:
- playlist - the id of the playlist - playlist - the id of the playlist
- fields - which fields to return - fields - which fields to return
- market - An ISO 3166-1 alpha-2 country code or the string - market - An ISO 3166-1 alpha-2 country code or the
from_token. string from_token.
""" """
plid = self._get_id('playlist', playlist_id) plid = self._get_id('playlist', playlist_id)
return self._get("playlists/%s" % (plid), fields=fields) return self._get("playlists/%s" % (plid), fields=fields, market=market)
def playlist_tracks(self, playlist_id=None, fields=None, def playlist_tracks(self, playlist_id, fields=None,
limit=100, offset=0, market=None): limit=100, offset=0, market=None):
''' Get full details of the tracks of a playlist. """ Get full details of the tracks of a playlist.
Parameters: Parameters:
- playlist_id - the id of the playlist - playlist_id - the id of the playlist
@ -416,15 +394,35 @@ class Spotify(object):
- limit - the maximum number of tracks to return - limit - the maximum number of tracks to return
- offset - the index of the first track to return - offset - the index of the first track to return
- market - an ISO 3166-1 alpha-2 country code. - market - an ISO 3166-1 alpha-2 country code.
''' """
plid = self._get_id('playlist', playlist_id) 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, limit=limit, offset=offset, fields=fields,
market=market) 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): 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. """ Get full details of the tracks of a playlist owned by a user.
Parameters: Parameters:
@ -435,10 +433,19 @@ class Spotify(object):
- offset - the index of the first track to return - offset - the index of the first track to return
- market - an ISO 3166-1 alpha-2 country code. - market - an ISO 3166-1 alpha-2 country code.
""" """
plid = self._get_id('playlist', playlist_id) return self.playlist_tracks(playlist_id, limit=limit, offset=offset,
return self._get("users/%s/playlists/%s/tracks" % (user, plid), fields=fields, market=market)
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=''): def user_playlist_create(self, user, name, public=True, description=''):
""" Creates a playlist for a user """ Creates a playlist for a user

View File

@ -23,6 +23,7 @@ from spotipy import (
import os import os
import sys import sys
import unittest import unittest
import warnings
sys.path.insert(0, os.path.abspath(os.pardir)) sys.path.insert(0, os.path.abspath(os.pardir))
@ -61,6 +62,10 @@ class AuthTestSpotipy(unittest.TestCase):
@classmethod @classmethod
def setUpClass(self): def setUpClass(self):
warnings.filterwarnings(
"ignore",
category=ResourceWarning,
message="unclosed.*<ssl.SSLSocket.*>")
missing = list(filter(lambda var: not os.getenv(CCEV[var]), CCEV)) 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) results = self.spotify.user_playlist_tracks(user, pid)
self.assertTrue(len(results['items']) >= 0) 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): def test_current_user_saved_albums(self):
# List # List
albums = self.spotify.current_user_saved_albums() albums = self.spotify.current_user_saved_albums()
@ -281,6 +279,17 @@ class AuthTestSpotipy(unittest.TestCase):
pl = self.spotify.playlist(self.playlist) pl = self.spotify.playlist(self.playlist)
self.assertTrue(pl["tracks"]["total"] > 0) 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): def test_user_follows_and_unfollows_artist(self):
# Initially follows 1 artist # Initially follows 1 artist
res = self.spotify.current_user_followed_artists() res = self.spotify.current_user_followed_artists()
@ -307,6 +316,24 @@ class AuthTestSpotipy(unittest.TestCase):
# Unfollow these 2 users # Unfollow these 2 users
self.spotify.user_unfollow_users(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__': if __name__ == '__main__':
unittest.main() unittest.main()