Type in your question below and we'll check to see what answers we can find...
Loading article...
Submitting...
If you couldn't find any answers in the previous step then we need to post your question in the community and wait for someone to respond. You'll be notified when that happens.
Simply add some detail to your question and refine the title if needed, choose the relevant category, then post.
Before we can post your question we need you to quickly make an account (or sign in if you already have one).
Don't worry - it's quick and painless! Just click below, and once you're logged in we'll bring you right back here and post your question. We'll remember what you've already typed in so you won't have to do it again.
My Question or Issue
I'm writing a scritpt to try to retrieve the genre from a track using the metadata of that song. I am not well versed in the spotify API, so i do need some information 😅.
btw i am writing it in Python and I'm using spotipy to access the API.
My question would be:
Is it even possible to retrieven the genre of a certian track? (bc it doesn't seem to work for me lol). If yes, how?
Thanks!
Solved! Go to Solution.
At Spotify, tracks don't have genres, but artists do have them.
Here is code to get genre information of a single track:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
CLIENT_ID = "your_client_id"
CLIENT_SECRET = "your_client_secret"
track_id = "TRACK_ID"
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=CLIENT_ID,client_secret=CLIENT_SECRET))
track_data = sp.track(track_id)
artist_ids = []
for artist in track_data["artists"]:
artist_ids.append(artist["id"])
artists_data = sp.artists(artist_ids)
genres = []
for artist in artists_data["artists"]:
genres += artist["genres"]
genres = set(genres) # removes duplicates
print(genres)
At Spotify, tracks don't have genres, but artists do have them.
Here is code to get genre information of a single track:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
CLIENT_ID = "your_client_id"
CLIENT_SECRET = "your_client_secret"
track_id = "TRACK_ID"
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=CLIENT_ID,client_secret=CLIENT_SECRET))
track_data = sp.track(track_id)
artist_ids = []
for artist in track_data["artists"]:
artist_ids.append(artist["id"])
artists_data = sp.artists(artist_ids)
genres = []
for artist in artists_data["artists"]:
genres += artist["genres"]
genres = set(genres) # removes duplicates
print(genres)
Thanks for your help!
Hello, I have implemented this into my code and I don't know why, it always gets stuck in the line:
Hi TionEye,
Do you get any errors?
This is extremely frustrating because this issue has existed since atleast 2015 and the docs still show in their examples that you can get genres from tracks and albums. But that's a flat out lie. After dozens of github issue requests and dozens more on this newer platform for issue tracking, nobody has gave a single care to address this in almost 10 years. All that has to be done is to remove 1 line from the example requests for tracks and albums and no more questions about this topic will come up.
Hey there you, Yeah, you! 😁 Welcome - we're glad you joined the Spotify Community! While you here, let's have a fun game and get…