Reputation: 95
I am using Spotipy to interact with Spotify API. I have succesfully get information from an artist, its album and the tracks of each album. I can get a track id, name and popularity. However, when I try to use the audio features to get the valence, it doesn't work.
Here is my code:
for a in artists_array
artist_search = sp.search(a,1,0,"artist")
artist = artist_search['artists']['items'][0]
artists.append({'name': artist['name'], 'popularity': artist['popularity']})
albums = []
albums_ids = []
spotify_albums = sp.artist_albums(artist['id'], album_type='album')
for i in range(len(spotify_albums['items'])):
album_id = spotify_albums['items'][i]['id']
albums_ids.append(spotify_albums['items'][i]['id'])
album_name = spotify_albums['items'][i]['name']
albums.append({'id': album_id,'name': album_name })
albums_songs = []
albumIndex = 0;
#For each album
for id in albums_ids:
albums_songs.append([])
spotify_songs = sp.album_tracks(id)
for n in range(len(spotify_songs['items'])):
song_id = spotify_songs['items'][n]['id']
song_name = spotify_songs['items'][n]['name']
song_popularity = sp.track(song_id)['popularity']
song_valence = sp.audio_features([song_id])['valence']
albums_songs[albumIndex].append({'id': song_id, 'name': song_name, 'album_id': id, 'popularity': song_popularity, 'valence': song_valence })
The problem is in:
song_valence = sp.audio_features(song_id)['valence']
That throws:
Traceback (most recent call last):
File "spotifyAPI/server.py", line 110, in <module>
song_valence = sp.audio_features(list_song_id[0])['valence']
TypeError: list indices must be integers or slices, not str
I know that I have the correct song_id because it works for the popularity. The program works perfectly if I remove the valence part.
I dont understand the type error. According to spotipy I should give:
audio_features(tracks=[]) Get audio features for one or multiple tracks based upon their Spotify IDs Parameters: tracks - a list of track URIs, URLs or IDs, maximum: 50 ids
That is the reason why I put it like:
song_valence = sp.audio_features([song_id])['valence']
But also it doesn't work for:
song_valence = sp.audio_features(song_id)['valence']
Upvotes: 0
Views: 898
Reputation: 95
I found the solution! Leaving it here in case someone has the same problem. It expects a list, which can be just the song_id you want and then you need to access at index 0. Hope this helps
list_song_ids = [song_id]
song_valence = sp.audio_features(list_song_ids)[0]['valence']
Upvotes: 0