fix: handle Spotify API rename of 'track'/'tracks' keys to 'item'/'items' - #57
Open
MojtabaTajik wants to merge 1 commit into
Open
Conversation
…ems'
Spotify's Web API changed the key name used in playlist track objects
and playlist envelopes from 'tracks'/'track' to 'items'/'item'. This
caused KeyError crashes when fetching any playlist contents.
Changes:
- utils.parse_tracks: use item.get('item') or item.get('track') fallback
- utils.parse_playlist: use items_info = playlist.get('items') or playlist.get('tracks')
for track envelope; use item.get('item') or item.get('track') for individual tracks;
safe .get() on owner fields to avoid KeyError on partial responses
- spotify_api.get_playlist_tracks: use playlist.get('items') or playlist.get('tracks')
fallback instead of hardcoded playlist['tracks']['items']
- spotify_api.get_current_user_playlists: filter None results from parse_playlist;
add missing @utils.ensure_username decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Spotify's Web API silently renamed keys in playlist response objects:
tracksitemstrackitemThis caused
KeyErrorcrashes on any call that reads playlist contents —get_playlist_tracks,parse_playlist, andparse_tracksall hard-coded the old key names.Fix
Use a
.get()fallback that accepts both key names so the code works regardless of which version the API returns:Same pattern applied consistently in:
utils.parse_tracks—item['track']→item.get('item') or item.get('track')utils.parse_playlist— track envelope + individual track + owner field access made safespotify_api.get_playlist_tracks— top-level playlist dict accessAlso adds the missing
@utils.ensure_usernamedecorator toget_current_user_playlistsand filtersNoneresults fromparse_playlistto avoid downstream errors on partial API responses.Testing
Verified against a live Spotify account with 20+ playlists and 100–130 track playlists. All playlist listing, track fetching, add, and remove operations work correctly after this change.