mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-26 16:15:51 +03:00
[GH-ISSUE #197] artist search error #100
Labels
No labels
api-bug
bug
dependencies
documentation
duplicate
enhancement
external-ide
headless-mode
implicit-grant-flow
invalid
missing-endpoint
pr-welcome
private-api
pull-request
question
spotipy3
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/spotipy#100
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @lohriialo on GitHub (Jun 9, 2017).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/197
I'm trying to search for artist like so but it's throwing KeyError:
query = 'year:2017'
results = sp.search(query, limit=50, type='artist')
artists = results['artists']
while artists:
for i, artist in enumerate(artists['items']):
print("%4d %s %s" % (i + 1 + artists['offset'], artist['id'], artist['name']))
if artists['next']:
artists = sp.next(artists)
else:
artists = None
The error I get is:
for i, artist in enumerate(artists['items']):
KeyError: 'items'
While the same type of call for "user_playlist" works fine.
For example:
playlists = sp.user_playlists('spotify')
while playlists:
for i, playlist in enumerate(playlists['items']):
print("%4d %s %s" % (i + 1 + playlists['offset'], playlist['uri'], playlist['name']))
if playlists['next']:
playlists = sp.next(playlists)
else:
playlists = None
Anyone got any idea?
@kotutuloro commented on GitHub (Jun 9, 2017):
Using
sp.next()returns search results in a format similar to the original results.So just like you had to retrieve the artists from the results in the initial search with
artists = results['artists']before you could useartists['items'], you have to do the same when you're reassigning artists.You could use this instead and it should work:
The structure of the response for playlists is different. The items key is immediately accessible from the results, so using
playlists = sp.next(playlists)and then usingplaylist['items']on the next iteration doesn't result in an error.@lohriialo commented on GitHub (Jun 16, 2017):
Thanks, I got this working like so:
query = 'year:2017' results = sp.search(query, limit=50, type='artist') while results: artists = results['artists'] for i, artist in enumerate(artists['items']): artist_ids.append(artist['id']) if results['next']: results = sp.next(artists) else: results = None