Reputation: 101
I am currently doing research into extracting specific items from a song in Spotify. I wanted to grab the specific release date and genres from an artist. I am pulling information from a pandas datagrams that looks like this:.
I've tried to do most of this by hand, but being a dataframe of about 1100 songs it became tedious. So I have looking into APIs to help with this, specifically spotipy.
I have an idea that I would start with:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials #To access authorised Spotify data
client_id = 'client_id'
client_secret = 'client_secret'
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) #spotify object to access API
name = "AJR" #chosen artist
result = sp.search(name) #search query
result['tracks']['items'][0]['artists']
But I am at a loss from where to go from that. I have tried looking at the documentation and while there is information on how to get genre and date from the Spotify For Developers page, I can't seem to find it in spotipy. Any Suggestions on where to go from here or how to implement an algorithm to get the desired details would be great. Thanks.
Upvotes: 6
Views: 9538
Reputation: 2827
Spotify does not expose track genre as of yet. Instead it exposes a list of genres an artist or an album is associated with.
Similarly, it does not expose release-date of a track, but only exposes a single release-date for an entire album. So, while I'm not sure it might be possible for an artist to add new tracks to an album after it has been released meaning an album release-date may not always indicate the correct release-date for all the tracks in corresponding album.
I have tried looking at the documentation and while there is information on how to get genre and date from the Spotify For Developers page, I can't seem to find it in spotipy.
You need access to full-paging object to access genre information from artist or album.
result = sp.search(name)
result['tracks']['items'][0]['artists']
What you got from this piece of code is not a full-paging object of the artist which means it is missing some additional details while genre being one of them.
You need to make a separate call to the appropriate endpoint to access the full-paging object. For example, this code:
result = sp.search("AJR")
track = result['tracks']['items'][0]
artist = sp.artist(track["artists"][0]["external_urls"]["spotify"])
print("artist genres:", artist["genres"])
album = sp.album(track["album"]["external_urls"]["spotify"])
print("album genres:", album["genres"])
print("album release-date:", album["release_date"])
outputs:
artist genres: ['modern rock']
album genres: []
album release-date: 2020-02-12
Spotify may not know the genres for some albums (as in above output) or artists.
Upvotes: 4