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

Merged
merged 5 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Rebasing master onto v3 doesn't require a changelog update.
- Replaced the `set_auth` and `auth_manager` properties with standard attributes.
- Replaced string concatenations and `str.format()` with f-strings

### Fixed

- Fixed playlist_add_items() to accept only URIs and URLs and not IDs, since 'track' and 'episode' cannot be inferred from ID only

### Removed

- Removed the following deprecated methods from `Spotify`:
Expand All @@ -52,7 +56,7 @@ Rebasing master onto v3 doesn't require a changelog update.
### Added

- Added examples for audiobooks, shows and episodes methods to examples directory
- Use newer string formatters (https://pyformat.info)
- Use newer string formatters ([https://pyformat.info](https://pyformat.info))

### Fixed

Expand Down
16 changes: 14 additions & 2 deletions spotipy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,10 +787,15 @@ def playlist_add_items(
- 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(
f"playlists/{plid}/tracks", payload=ftracks, position=position
f"playlists/{plid}/tracks",
payload=items,
position=position,
)

def playlist_replace_items(self, playlist_id, items):
Expand Down Expand Up @@ -1717,6 +1722,13 @@ def _get_uri(self, type, id):
def _is_uri(self, uri):
return re.search(Spotify._regex_spotify_uri, uri) is not None

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
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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 @@ -112,7 +120,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
Loading