Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated functionality of playlist_add_items() #914

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Pin Github Actions Runner to Ubuntu 20 for Py27
- Fixed potential error where `found` variable in `test_artist_related_artists` is undefined if for loop never evaluates to true
- Fixed false positive test `test_new_releases` which looks up the wrong property of the JSON response object and always evaluates to true
- Fixed playlist_add_items() to accept only URIs and URLs and not IDs, since 'track' and 'episode' cannot be inferred from ID only

## [2.21.0] - 2022-09-26

Expand Down
4 changes: 2 additions & 2 deletions examples/add_tracks_to_playlist.py
Expand Up @@ -11,7 +11,7 @@

def get_args():
parser = argparse.ArgumentParser(description='Adds track to user playlist')
parser.add_argument('-t', '--tids', action='append',
parser.add_argument('-u', '--uris', action='append',
required=True, help='Track ids')
parser.add_argument('-p', '--playlist', required=True,
help='Playlist to add track to')
Expand All @@ -22,7 +22,7 @@ def main():
args = get_args()

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
sp.playlist_add_items(args.playlist, args.tids)
sp.playlist_add_items(args.playlist, args.uris)


if __name__ == '__main__':
Expand Down
35 changes: 32 additions & 3 deletions spotipy/client.py
Expand Up @@ -846,8 +846,27 @@ def user_playlist_add_tracks(
- tracks - a list of track URIs, URLs or IDs
- position - the position to add the tracks
"""
tracks = [self._get_uri("track", tid) for tid in tracks]
return self.playlist_add_items(playlist_id, tracks, position)

def user_playlist_add_episodes(
self, user, playlist_id, episodes, position=None
):
warnings.warn(
"You should use `playlist_add_items(playlist_id, episodes)` instead",
DeprecationWarning,
)
""" Adds episodes to a playlist

Parameters:
- user - the id of the user
- playlist_id - the id of the playlist
- episodes - a list of track URIs, URLs or IDs
- position - the position to add the episodes
"""
episodes = [self._get_uri("episode", tid) for tid in episodes]
return self.playlist_add_items(playlist_id, episodes, position)

def user_playlist_replace_tracks(self, user, playlist_id, tracks):
""" Replace all tracks in a playlist for a user

Expand Down Expand Up @@ -1032,14 +1051,17 @@ def playlist_add_items(

Parameters:
- playlist_id - the id of the playlist
- items - a list of track/episode URIs, URLs or IDs
- items - a list of track/episode URIs or URLs
- position - the position to add the tracks
"""
for item in items:
if not self._is_uri(item) and not self._is_url(item):
raise RuntimeError("playlist_add_items() only accepts URIs and URLs.")
plid = self._get_id("playlist", playlist_id)
ftracks = [self._get_uri("track", tid) for tid in items]
items = [self._url_to_uri(item) if self._is_url(item) else item for item in items]
return self._post(
"playlists/%s/tracks" % (plid),
payload=ftracks,
payload=items,
position=position,
)

Expand Down Expand Up @@ -1945,6 +1967,13 @@ def _get_uri(self, type, id):
def _is_uri(self, uri):
return uri.startswith("spotify:") and len(uri.split(':')) == 3

def _is_url(self, url):
return url.startswith("http")

def _url_to_uri(self, url):
splitted = url.split("/")
return "spotify:" + splitted[-2] + ":" + splitted[-1]

def _search_multiple_markets(self, q, limit, offset, type, markets, total):
if total and limit > total:
limit = total
Expand Down
10 changes: 9 additions & 1 deletion tests/integration/user_endpoints/test.py
Expand Up @@ -35,6 +35,14 @@ def setUpClass(cls):
"spotify:episode:7cRcsGYYRUFo1OF3RgRzdx",
]

cls.tracks_and_episodes = [
"spotify:track:3F5CgOj3wFlRv51JsHbxhe",
"http://open.spotify.com/track/5mCPDVBb16L4XQwDdbRUpz",

"spotify:episode:7AY0yaj2k0W3obOSCfm6m4",
"https://open.spotify.com/episode/5AJB2BTp7RExSGpbQlIsXI"
]

scope = (
'playlist-modify-public '
'user-library-read '
Expand Down Expand Up @@ -101,7 +109,7 @@ def test_current_user_follow_playlist(self):
def test_playlist_replace_items(self):
# add tracks to playlist
self.spotify.playlist_add_items(
self.new_playlist['id'], self.four_tracks)
self.new_playlist['id'], self.tracks_and_episodes)
playlist = self.spotify.playlist(self.new_playlist['id'])
self.assertEqual(playlist['tracks']['total'], 4)
self.assertEqual(len(playlist['tracks']['items']), 4)
Expand Down